-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbouncing_ball.cpp
134 lines (134 loc) · 2.84 KB
/
bouncing_ball.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include"graphics.h"
#include"dos.h"
#include"conio.h"
#include"stdlib.h"
#define DELAY 1
#define SOUND 3500
void state(int x,int y,int mode);
int i;
void *ptr1[4],*ptr2[4];
/* 3d Ball */
char ball[20][20]=
{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,12,12,12,12,12,12,12,12,0,0,0,0,0,0},
{0,0,0,0,12,12,12,12,12,12,12,12,12,12,12,12,0,0},
{0,0,0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0,0,0},
{0,0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0,0},
{0,0,12,12,12,12,12,15,15,12,12,12,12,12,12,12,12,12,0,0},
{0,12,12,12,12,12,15,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,12,15,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,15,15,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,15,15,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,15,15,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,12,15,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0,0},
{0,0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0,0},
{0,0,0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0,0,0},
{0,0,0,0,12,12,12,12,12,12,12,12,12,12,12,12,0,0,0,0},
{0,0,0,0,0,0,12,12,12,12,12,12,12,12,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
/* Condition to check out of range */
int COND(int x,int y)
{
if (x>10 && x<getmaxx()-10 && y>10 && y<getmaxy()-10)
return 1;
else
return 0;
}
/* This sub-routine will check which path to follow on stiking the wall
of
the container */
void state(int x,int y,int mode)
{
while(COND(x,y)&&!kbhit())
{
putimage(x,y,ptr1[4],COPY_PUT);
putimage(getmaxx()-x,getmaxy()-y,ptr1[4],COPY_PUT);
switch(mode)
{
case 0:
x++;
y++;
break;
case 1:
x++;
y--;
break;
case 2:
x--;
y++;
break;
case 3:
x--;
y--;
break;
}
delay(DELAY);
//nosound();
putimage(x,y,ptr2[4],COPY_PUT);
putimage(getmaxx()-x,getmaxy()-y,ptr2[4],COPY_PUT);
}
cleardevice();
if(x>=(getmaxx()-10)||x<=10)
{
//sound(SOUND);
switch(mode)
{
case 0:
state(--x,--y,2);
break;
case 1:
state(--x,++y,3);
break;
case 2:
state(++x,--y,0);
break;
case 3:
state(++x,++y,1);
break;
}
}
else
if(y>=getmaxy()-10||y<=10)
{
//sound(SOUND);
switch(mode)
{
case 0:
state(--x,--y,1);
break;
case 1:
state(--x,++y,0);
break;
case 2:
state(++x,--y,3);
break;
case 3:
state(++x,++y,2);
break;
}
}
else
exit(0);
}
int main()
{
int gm,gd=DETECT;
int i,j;
initgraph(&gd,&gm,"\tc\tc\bgi");
for(i=0;i<20;i++)
for(j=0;j<20;j++)
if(ball[i][j]!='0')
putpixel(10+i,10+j,ball[j][i]);
getimage(10,10,30,30,ptr1[4]);
cleardevice();
getimage(10,10,30,30,ptr2[4]);
/* start with (20,20) */
state(20,20,0);
getch();
return 0;
}