Professional Documents
Culture Documents
Faculty:
LAB Manual
PART A
(PART A : TO BE REFFERED BY STUDENTS)
Experiment No.12
A.1 Aim:
Implement Mini Project
A.2 Prerequisite:
1. C Language.
2. Geometric Concepts.
3. Concept of 2D and 3D basic Transformations.
4. Projection Concepts.
A.3 Outcome:
After successful completion of this experiment students will be able to,
1. Implement various line, circle, ellipse drawing algorithms.
2. Implement various output and filled area primitive algorithms.
3. Apply the transformations and clipping algorithms on graphical objects.
4. Implement the curve and fractal generation.
5. Implement Parallel and Perspective projection of a 3D object on Projection Plane.
6. Apply Character Generation Techniques and OpenGL.
Function Description
initgraph It initializes the graphics system by loading the passed graphics driver
then changing the system into graphics mode.
getmaxx It returns the maximum X coordinate in current graphics mode and driver.
setcolor It changes the current drawing colour. Default colour is white. Each color
is assigned a number, like BLACK is 0 and RED is 4. Here we are using
colour constants defined inside graphics.h header file.
floodfill It is used to fill a closed area with current fill pattern and fill color. It takes
any point inside closed area and color of the boundary as input.
Function Description
cleardevice It clears the screen, and sets current position to (0, 0).
closegraph It unloads the graphics drivers and sets the screen back to text mode.
#include <stdio.h>
1 #include <conio.h>
2 #include <graphics.h>
3 #include <dos.h>
4
5 int main() {
int gd = DETECT, gm;
6
int i, x, y, flag=0;
7 initgraph(&gd, &gm, "C:\\TC\\BGI");
8
9 /* get mid positions in x and y-axis */
10 x = getmaxx()/2;
11 y = 30;
12
13
while (!kbhit()) {
14 if(y >= getmaxy()-30 || y <= 30)
15 flag = !flag;
16 /* draws the gray board */
17 setcolor(RED);
18 setfillstyle(SOLID_FILL, RED);
circle(x, y, 30);
19 floodfill(x, y, RED);
20
21 /* delay for 50 milli seconds */
22 delay(50);
23
24 /* clears screen */
cleardevice();
25 if(flag){
26 y = y + 5;
27 } else {
y = y - 5;
28 }
29 }
30
31 getch();
32 closegraph();
33 return 0;
}
34
35
36
37
38
39
40
Program
Output
Here is the screenshot of bouncing ball.
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
Program:
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
#include<time.h>
int x1,y1,x2,y2,x1old,x2old,ycenter,xcenter;
int xnewcenter,ynewcenter,radius,ballspeedx,ballspeedy;
char sym,pit[20];
int speed=10;
void ball()
{
time_t t;
xcenter=xnewcenter;
ycenter =ynewcenter;
xnewcenter=xcenter+ballspeedx;
ynewcenter=ycenter+ballspeedy;
setcolor(0);
setfillstyle(1,0);
fillellipse(xcenter,ycenter,radius,radius);
setfillstyle(1,11);
fillellipse(xnewcenter,ynewcenter,radius,radius);
setcolor(11);
if(xnewcenter-radius<0)
ballspeedx=-ballspeedx;
if(xnewcenter+radius>640)
ballspeedx=-ballspeedx;
if(ynewcenter-radius<0)
ballspeedy=-ballspeedy;
if(ynewcenter+radius>340)
{
if(xnewcenter>x1 && xnewcenter<x2)
{
ballspeedy=-ballspeedy;
srand((int)time(&t));
ballspeedx=random(3);
if(random(3)==1)
ballspeedx=-ballspeedx;
score++;
}
else
{
cleardevice();
outtextxy(300,150,"Game Over");
sprintf(pit,"Score %d",score);
outtextxy(300,170,"Press Q to Exit");
getch();
}
return;
}
delay(10);
}
void bat(char sym)
{
switch(sym)
{
case 'z' : if(x1>0)
{
x1old=x1;
x2old=x2;
x1=x1-5;
x2=x2-5;
delay(10);
setfillstyle(1,0);
bar(x1old,y1,x2old,y2);
setfillstyle(1,13);
bar(x1,y1,x2,y2);
}
if(x1==0)
{
delay(10);
setfillstyle(1,13);
bar(x1,y1,x2,y2);
}
break;
case 'x' : if(x2<640)
{
x1old=x1;
x2old=x2;
x1=x1+5;
x2=x2+5;
delay(10);
setfillstyle(1,0);
bar(x1old,y1,x2old,y2);
setfillstyle(1,13);
bar(x1,y1,x2,y2);
}
if(x2==640)
{
delay(10);
setfillstyle(1,13);
bar(x1,y1,x2,y2);
}
break;
}
}
void play()
{
while(inportb(0x60)!=16)
{
if(inportb(0x60)==44) bat('z');
if(inportb(0x60)==45) bat('x');
ball();
}
}
void initialize()
{
time_t t;
x1=rand();
x1=320;
y1=x1+30;
x2=x1+80;
y2=y1+40;
srand((int)time(&t));
xnewcenter=random(200);
ynewcenter=random(200);
ballspeedx=2;
srand((int)time(&t));
ballspeedy=random(3);
if(random(3)==0)
{
ballspeedx=-ballspeedx;
ballspeedy=-ballspeedy;
}
radius=20;
play();
}
void main()
{
int gd=0,gm;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
outtextxy(90,150,"Instractions:");
outtextxy(90,170,"Press Q to Exit");
outtextxy(90,190,"Press Z, X to move the bar");
outtextxy(90,210,"Press any key to continue.");
getch();
cleardevice();
initialize();
getch();
}
OUTPUT:
B.3 Observations and learning:
Ans-
1.outtextxy() function is used to display text or string at a specified point (x,y) on the
screen.
2.inport us used to get hold of specific keyboard key
3.random function is used for the random movement of the ball.
B.4 Conclusion:
Ans- Thus, we conclude that we have successfully implemented animation of Ping
Pong Game by which can play the game. We have used various functions of graphics in
this program and thus familiarized with the concepts of graphics in C.