-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathballtouch.cpp
More file actions
384 lines (346 loc) · 6.28 KB
/
Copy pathballtouch.cpp
File metadata and controls
384 lines (346 loc) · 6.28 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* program licensed under GNU's GPL */
#include<GL/glut.h>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#define MAXR 2.0
#define MIN_THETA 0.5233 //////0.261667 // 30 degree in radian
#define WIDTH 100.0
#define HEIGHT 100.0
#define MAXSPEED 0.8
#define MAXGOOD 20
#define MAXBAD 30
void ballupdate(int k);
time_t start;
int gameover;
int scrwidth,scrheight;
int score,running; // initialized with zero
class ball
{
GLfloat cenx,ceny,radius;
GLfloat r,g,b;
float increment,velocity;
int direction,type,active,delay,waiting;
time_t start;
public:
ball();
void init(int k=-1);
void setPos(GLfloat x,GLfloat y);
void setColor(GLfloat re,GLfloat gr,GLfloat bl);
void setRadius(GLfloat r);
void setIncrement(int inc);
void setDirection(int dir);
void setVelocity(int vel);
void display();
void update(int typ=-1);
int isClash(ball b2)
{
if(active && abs(cenx-b2.cenx)<radius+b2.radius && abs(ceny-b2.ceny)<radius+b2.radius && ((cenx-b2.cenx)*(cenx-b2.cenx)+(cenx-b2.cenx)*(ceny-b2.ceny))<radius+b2.radius)
{
active=0;
running=0;
return 1;
}
return 0;
}
};
ball::ball()
{
type=-999; // type of ball not decided
waiting=0;
active=0;
running=0;
// init();
}
void ball::init(int k)
{
// if(k==-999)
// {
// printf("--\n");
// }
float prob;
//activate
if(active) return ;
if(!waiting && !active)
{
prob=((rand()%100)/100.0);
if(prob<0.5 && k!=100) // activation failed
{
active=0;
start=time(NULL);
delay=rand()%10; // random number of seconds to wait
waiting=1;
return ;
}
else active=1;
}
else // on waiting state
{
if(difftime(time(NULL),start)>=delay)
{
//end of wait
waiting=0;
active=1;
}
else
return ; // continue waiting
}
//activation successful
//active=1;
radius=((1+rand()%99)/100.0) * MAXR;
increment=MIN_THETA;
velocity=((1+rand()%99)/100.0)*MAXSPEED;
prob=((rand()%100)/100.0);
if(prob <0.25)
{
cenx=0;
ceny=((rand()%100)/100.0)*HEIGHT;
direction=0; // east
}
else if(prob <0.5)
{
ceny=0;
cenx=((rand()%100)/100.0)*WIDTH;
direction=1; // north
}
else if(prob <0.75)
{
ceny=HEIGHT;
cenx=((rand()%100)/100.0)*WIDTH;
direction=2; //south
}
else
{
ceny=((rand()%100)/100.0)*HEIGHT;
cenx=WIDTH;
direction=3; //west
}
//printf("%d\n",k);
if(k==1 || type==1)
setColor(0,1,0);
else if(k==0 || type==0)
setColor(1,0,0);
if(type==-999) type=k;
// if(k==0) printf("hi\n");
}
void ball::setPos(GLfloat x,GLfloat y)
{
if(x<=WIDTH && y<=HEIGHT && x>=0 && y>=0)
{
cenx=x;
ceny=y;
}
}
void ball::setColor(GLfloat rr,GLfloat gg,GLfloat bb)
{
r=rr;
g=gg;
b=bb;
}
void ball::setRadius(GLfloat r)
{
radius = r;
}
void ball::setIncrement(int inc)
{
if(inc<360)
increment=inc*2*3.14/360;
}
void ball::setDirection(int dir)
{
if(dir>=0 && dir<=3)
direction=dir;
}
void ball::setVelocity(int vel)
{
if(vel>0 && vel <= MAXSPEED )
velocity=vel;
}
void ball::display()
{
if(!active) return ;
float i;
GLfloat x,y;
glBegin(GL_POLYGON);
glColor3f(r,g,b);
for(i=0;i<6.28;i+=increment)
{
x=radius*cos(i);
y=radius*sin(i);
glVertex2f((GLfloat)cenx+x,(GLfloat)ceny+y);
}
glEnd();
}
void ball::update(int typ)
{
if(active)
switch(direction)
{
case 0:cenx+=velocity;//east
if(cenx>WIDTH) {
active=0;
running=0;
init(typ);
}
break;
case 1:ceny+=velocity;//north
if(ceny>HEIGHT) {
active=0;
running=0;
init(typ);
}
break;
case 2:ceny-=velocity;//south
if(ceny<0) {
running=0;
active=0;
init(typ);
}
break;
case 3:cenx-=velocity;//west
if(cenx<0) {
active=0;
running=0;
init(typ);
}
break;
default:printf("errrrrrrrrrrr\n");
}
else
{
this->init(typ);
}
}
ball good[MAXGOOD],bad[MAXBAD],pawn;
void init()
{
glClearColor(1,1,1,1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,WIDTH,0,HEIGHT);
glMatrixMode(GL_MODELVIEW);
}
void gameinit()
{
if(gameover)
{
return ;
}
start=time(NULL); // can be used to trigger events based on time
score=0;
//init mouse cursor
glutWarpPointer(scrwidth/2,scrheight/2);
int i;
for(i=0;i<MAXGOOD;i++)
{
good[i].init(1); // 1 for good
}
for(i=0;i<MAXBAD;i++)
{
bad[i].init(0); // 0 for bad
}
pawn.init(100);
pawn.setRadius(0.25);
pawn.setPos(WIDTH/2,HEIGHT/2);
pawn.setIncrement(20) ; // hexagon
pawn.setColor(1,0.6,0.2);
}
void scoreUpdate()
{
//check for clash of pawn and bad-ball
int i;
for(i=0;i<MAXBAD;i++)
{
if(bad[i].isClash(pawn))
{
//game over FIXME: add a good interface for start,stop and restart game
gameover=1;
gameinit();
}
}
// clash of good-ball add to point
for(i=0;i<MAXGOOD;i++)
{
if(good[i].isClash(pawn))
{
score+=1;
printf("%d~\n",score);
pawn.setRadius(0.25+(score/10.0));
good[i].init(1);
}
}
}
void pawnupdate(int x,int y)
{
pawn.setPos((x*WIDTH/scrwidth),((scrheight-y)*HEIGHT)/scrheight);
scoreUpdate();
glutPostRedisplay();
}
void gameUpdate()
{
int i;
for(i=0;i<MAXGOOD;i++)
{
good[i].update(1);
}
for(i=0;i<MAXBAD;i++)
{
bad[i].update(0);
}
for(i=0;i<MAXGOOD;i++)
{
good[i].init(1);
}
for(i=0;i<MAXBAD;i++)
{
bad[i].init(0);
}
scoreUpdate();
}
void ballupdate(int k)
{
if(gameover) return ;
gameUpdate();
glutPostRedisplay();
glutTimerFunc(20,ballupdate,0);
}
void game()
{
glClear(GL_COLOR_BUFFER_BIT);
int i;
for(i=0;i<MAXGOOD;i++)
{
good[i].display();
}
for(i=0;i<MAXBAD;i++)
{
bad[i].display();
}
pawn.display();
glFlush();
}
void resize(int x,int y)
{
glViewport(0,0,x,y);
scrheight=y;
scrwidth=x;
}
int main(int argc,char **argv)
{
scrwidth=640;
scrheight=480;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutInitWindowSize(scrwidth,scrheight);
glutInitWindowPosition(50,50);
glutCreateWindow("ClearBall");
init();
gameinit();
glutDisplayFunc(game);
glutPassiveMotionFunc(pawnupdate);
glutTimerFunc(500,ballupdate,0);
glutReshapeFunc(resize);
glutSetCursor(GLUT_CURSOR_NONE);
glutMainLoop();
}