-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
386 lines (335 loc) · 8.93 KB
/
main.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
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
385
386
#include<windows.h>
#include <GL/glut.h>
#include <math.h>
#include <iostream>
#include <string>
#include <bits/stdc++.h>
#define PI 3.1416
///Global Variable
int score = 0;
bool gameover = false;
char a[100] = "Game over, score:";
float boundary = 50; /// Scene boundary
float border = 40; /// Game boundary
///Agent 1 Parameters
float RAgent = 6.0;
float CAgentX = -20, CAgentY = 0;
bool isAgentDestroyed = false;
float agentSpeed = 0.5;
///Agent 1 Parameters
float CAgentBX = 20, CAgentBY = 0;
bool isAgentBDestroyed = false;
float agentBSpeed = 0.5;
///Enemy 1 Parameters
float REnemy = 4.0;
float CEnemyX = -border, CEnemyY = 0.0;
int state = 1;
float enemySpeed = 0.001;
///Enemy 2 Parameters
float CEnemyBX = border, CEnemyBY = -border;
int stateB = 1;
float enemyBSpeed = 0.001;
void circle(float radius_x, float radius_y)
{
int i=0;
float angle = 0.0;
glBegin(GL_POLYGON);
for(i = 0; i < 100; i++)
{
angle = 2 * PI * i / 100;
glVertex2f (cos(angle) * radius_x, sin(angle) * radius_y);
//glVertex2f (cos(angle) * radius_x+5, sin(angle) * radius_y);
}
glEnd();
}
void Sprint( int x, int y, char *st)
{
int l,i;
l=strlen( st );
glRasterPos3i( x, y, -1);
for( i=0; i < l; i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, st[i]);
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINE_STRIP);
glVertex2f(-border, border);
glVertex2f(border, border);
glVertex2f(border, -border);
glVertex2f(-border, -border);
glVertex2f(-border, border);
glEnd();
if(!isAgentDestroyed)
{
glPushMatrix(); //Green Circle
glTranslatef(CAgentX, CAgentY,0);
glColor3f(0.0, 1.0, 0.0);
circle(RAgent,RAgent);
glPopMatrix();
}
if(!isAgentBDestroyed)
{
glPushMatrix(); //Green Circle
glTranslatef(CAgentBX, CAgentBY,0);
glColor3f(0.0, 1.0, 0.0);
circle(RAgent,RAgent);
glPopMatrix();
}
//Enemy A
glPushMatrix(); //Red circle (Static)
glTranslatef(CEnemyX, CEnemyY,0);
glColor3f(1.0, 0.0, 0.0);
circle(REnemy,REnemy);
glPopMatrix();
//Enemy B
glPushMatrix(); //Red circle (Static)
glTranslatef(CEnemyBX, CEnemyBY,0);
glColor3f(1.0, 0.0, 0.0);
circle(REnemy,REnemy);
glPopMatrix();
if(gameover)
{
Sprint(-20,0, a);
char num_str[10];
sprintf(num_str, "%d", score); // convert the number to a string
for (int i = 0; i < strlen(num_str); i++) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, num_str[i]);
}
}
glFlush();
}
void animate()
{
if(!gameover)
{
score++;
///Enemy A Movement
if(state == 1)
{
CEnemyX += enemySpeed;
CEnemyY += enemySpeed;
if(CEnemyX>0 && CEnemyY>border)
{
state = 2;
}
}
else if(state == 2)
{
CEnemyY -= enemySpeed;
if(CEnemyY<-border)
{
state = 3;
}
}
else if(state == 3)
{
CEnemyX += enemySpeed;
CEnemyY += enemySpeed;
if(CEnemyX>border && CEnemyY>0)
{
state = 4;
}
}
else if(state == 4){
CEnemyX -= enemySpeed;
if(CEnemyX<-border)
{
state = 1;
}
}
///Enemy B Movement
if(stateB == 1)
{
CEnemyBX -= enemySpeed;
CEnemyBY += enemySpeed;
if(CEnemyBX<-border && CEnemyBY>border)
{
stateB = 2;
}
}
else if(stateB == 2)
{
CEnemyBX += enemySpeed;
CEnemyBY -= (enemySpeed/2);
if(CEnemyBX>border &&CEnemyBY<0)
{
stateB = 3;
}
}
else if(stateB == 3)
{
CEnemyBX -= (enemySpeed/2);
CEnemyBY += (enemySpeed/2);
if(CEnemyBX<0 && CEnemyBY>border)
{
stateB = 4;
}
}
else if(stateB == 4){
CEnemyBX += (enemySpeed/2);
CEnemyBY -= enemySpeed;
if(CEnemyBX>border && CEnemyBY<-border)
{
stateB = 1;
}
}
///Attack Logic of agent A
float distanceAWithA = sqrt((CAgentX-CEnemyX)*(CAgentX-CEnemyX) + (CAgentY-CEnemyY)*(CAgentY-CEnemyY));
float distanceAWithB = sqrt((CAgentX-CEnemyBX)*(CAgentX-CEnemyBX) + (CAgentY-CEnemyBY)*(CAgentY-CEnemyBY));
///Attack Logic of agent B
float distanceBWithA = sqrt((CAgentBX-CEnemyX)*(CAgentBX-CEnemyX) + (CAgentBY-CEnemyY)*(CAgentBY-CEnemyY));
float distanceBWithB = sqrt((CAgentBX-CEnemyBX)*(CAgentBX-CEnemyBX) + (CAgentBY-CEnemyBY)*(CAgentBY-CEnemyBY));
if(distanceAWithA <= RAgent+REnemy || distanceAWithB <= RAgent+REnemy)
{
isAgentDestroyed = true;
isAgentBDestroyed = true;
gameover = true;
}
if(distanceBWithA <= RAgent+REnemy || distanceBWithB <= RAgent+REnemy)
{
isAgentDestroyed = true;
isAgentBDestroyed = true;
gameover = true;
}
}else{
//glutPostRedisplay();
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
Sprint(0,0, "Hello");
glutPostRedisplay();
}
glutPostRedisplay();
}
void getStringRepxn()
{
glRasterPos2d(50,180);
for(int i=0;i<15;i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, a[i]);
}
glFlush();
}
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glOrtho(-boundary, boundary, -boundary, boundary, -boundary, boundary);
}
void spe_key(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_LEFT:
CAgentX -= agentSpeed;
if(CAgentX<0 && -CAgentX>border-RAgent)
{
isAgentDestroyed = true;
gameover = true;
}
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT:
CAgentX += agentSpeed;
if(CAgentX>0 && CAgentX>border-RAgent)
{
isAgentDestroyed = true;
gameover = true;
}
glutPostRedisplay();
break;
case GLUT_KEY_DOWN:
CAgentY -= agentSpeed;
if(CAgentY<0 && -CAgentY>border - RAgent)
{
isAgentDestroyed = true;
gameover = true;
}
glutPostRedisplay();
break;
case GLUT_KEY_UP:
CAgentY += agentSpeed;
if(CAgentY>0 && CAgentY>border-RAgent)
{
isAgentDestroyed = true;
gameover = true;
}
glutPostRedisplay();
break;
default:
break;
}
}
void keyboard_action(unsigned char key, int x, int y)
{
if(key == 'u')
{
CAgentBY += agentSpeed;
if(CAgentBY>0 && CAgentBY>border-RAgent)
{
isAgentBDestroyed = true;
gameover = true;
}
glutPostRedisplay();
}
else if(key == 'd')
{
CAgentBY -= agentSpeed;
if(CAgentBY<0 && -CAgentBY>border - RAgent)
{
isAgentBDestroyed = true;
gameover = true;
}
glutPostRedisplay();
}
else if(key == 'l')
{
CAgentBX -= agentSpeed;
if(CAgentBX<0 && -CAgentBX>border-RAgent)
{
isAgentBDestroyed = true;
gameover = true;
}
glutPostRedisplay();
}
else if(key == 'r')
{
CAgentBX += agentSpeed;
if(CAgentBX>0 && CAgentBX>border-RAgent)
{
isAgentBDestroyed = true;
gameover = true;
}
glutPostRedisplay();
}
}
void mouse_action(int button,int state,int x, int y)
{
if(button == GLUT_LEFT_BUTTON)
{
glutPostRedisplay();
}
else if(button == GLUT_RIGHT_BUTTON)
{
glutPostRedisplay();
}
else if(button == GLUT_MIDDLE_BUTTON)
{
glutPostRedisplay();
}
}
int main()
{
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Circle");
init();
glutDisplayFunc(display);
glutIdleFunc(animate);
glutSpecialFunc(spe_key);
glutKeyboardFunc(keyboard_action);
glutMouseFunc(mouse_action);
glutMainLoop();
return 0;
}