-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
659 lines (543 loc) · 17.9 KB
/
game.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
//============================================================================
// Name : Vaneeza Ahmad
// Roll no : 21I-0390
// Class : BSCS-Y
// Author : FAST CS Department
// Version : 2.1
// Copyright : (c) Reserved
// Description : Basic 2D game of Rush Hour...
//============================================================================
#ifndef RushHour_CPP_
#define RushHour_CPP_
#include "util.h"
#include <iostream>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
#include<sys/types.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<pthread.h>
#include <fstream>
#include<string>
#include<cmath> // for basic math functions such as cos, sin, sqrt
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
pthread_t main_thread, player1_thread, player2_thread, menu_thread, graphics_thread ;
void SetCanvasSize(int width, int height) {
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
//using pointer to assign color to taxi
int xI = 10, yI = 800;
int x2=0, y2=0;
int x3, y3, x4, y4, x5, y5/**Ptrx, *Ptry*/;
int a = 60, b=100;
int c=b-a;
int x=50, y=670;
int z=y-x;
int X1=300, Y1=450, X2=700, Y2=350,X3=500, Y3=400,X4=500, Y4=650,X5=600, Y5=600,X6=400, Y6=550;
int X7=350, Y7=500, X8=200, Y8=350,X9=100, Y9=400,X10=500, Y10=300,X11=150, Y11=200,X12=300, Y12=200;
int score1=0, score2=0;
bool objects[741][691];
void* drawPlayer(void* arg){
DrawCircle(a, b, 10,colors[BLACK]);
DrawLine( a , b , a , c , 2 , colors[BLACK] );
DrawLine( a , c , a+10 , c-20 , 2 , colors[BLACK] );
DrawLine( a , c , a-20 , c-20 , 2 , colors[BLACK] );
glutPostRedisplay();
return NULL;
}
void* drawPassenger(void* arg) //draws passenger in the display
{
DrawCircle(x,y,10,colors[BLACK]);
DrawLine( x , y, x , z , 2 , colors[BLACK] );
DrawLine( x , z , x+10 , z-20 , 2 , colors[BLACK] );
DrawLine( x , z , x-20 , z-20 , 2 , colors[BLACK] );
glutPostRedisplay();
return NULL;
//
// DrawCircle(50,670,10,colors[WHITE]);
// DrawLine( 50 , 670 , 50 , 620 , 2 , colors[WHITE] );
// DrawLine( 50 , 620 , 60 , 600 , 2 , colors[WHITE] );
// DrawLine( 50 , 620 , 40 , 600 , 2 , colors[WHITE] );
// vector<unsigned char> imgArray;
// ReadImage("sprite.png", imgArray);
// using namespace cimg_library;
// CImg<unsigned char> img("sprite.png");
// img.display();
// for (int j = 0; j < img.width(); ++j) {
// int t = j;
// for (int i = 0; i < img.height(); ++i, t += img.width()) {
// imgArray[k++] = rp[t];
// imgArray[k++] = gp[t];
// imgArray[k++] = bp[t];
// }
// //imgArray[i][j] = img[k++];
// }
/*int a = GetRandInRange(50, 90);
int b = 100;
int c= b-a;*/
// DrawCircle(500, 530, 10,colors[WHITE]);
// DrawLine( 500 , 530 , 500 , 490 , 2 , colors[WHITE] );
// DrawLine( 500 , 490, 510 , 470 , 2 , colors[WHITE] );
// DrawLine( 500 , 490 , 480 , 470 , 2 , colors[WHITE] );
}
void* Menu(void* arg)
{
string name1, name2, taxi;
cout<<"---------------START MENU---------------\nC0LLECT 0BJECTS GAME\n";
cout<<"---The Rules---\n1.Player must collect maximum objects to win the game.\n\n";
cout<<"Player1 plays with arrow keys and shift key to collect objects.\n\nPlayer2 plays with ASDW and spacebar to collect objects.\n\n";
cout<<"Enter Player1 name: ";
cin>>name1;
cout<<"Enter Player2 name: ";
cin>>name2;
}
void objExist(int xx, int yy)
{
objects[300][450]=true;
objects[340][450]=true;
objects[320][490]=true;
objects[700][350]=true;
objects[740][350]=true;
objects[720][390]=true;
objects[500][400]=true;
objects[540][400]=true;
objects[520][440]=true;
objects[500][650]=true;
objects[540][650]=true;
objects[520][690]=true;
objects[600][600]=true;
objects[640][600]=true;
objects[620][640]=true;
objects[400][550]=true;
objects[440][550]=true;
objects[420][590]=true;
objects[350][500]=true;
objects[390][500]=true;
objects[370][540]=true;
objects[xx][yy]=false;
}
void drawBoard(int xxx, int yyy) //Draws the player board
{
// set the background color using function glClearColor.
// to change the background play with the red, green and blue values below.
// Note that r, g and b values must be in the range [0,1] where 0 means dim rid and 1 means pure red and so on.
glClearColor(1, 1, 1, 1); // Red==Green==Blue==1 --> White Colour
glClear (GL_COLOR_BUFFER_BIT); //Update the colors
objExist(xxx, yyy);
DrawLine(100, 100, 800, 100, 10, colors[DARK_CYAN]); //bottom line
DrawLine(100, 100, 100, 800, 10, colors[DARK_CYAN]); // left line
DrawLine(100, 800, 800, 800, 10, colors[DARK_CYAN]); //top line
DrawLine(800, 100, 800, 800, 10, colors[DARK_CYAN]); //right line
for(int i=100; i<800; i=i+50)
{DrawLine( 100 , i , 800 , i , 4 , colors[DARK_CYAN] );}
for(int i=100; i<800; i=i+50)
{DrawLine( i , 100 , i , 800 , 4 , colors[DARK_CYAN] );}
// objects[300][450]==true;
// objects[340][450]==true;
// objects[320][490]==true;
// objects[700][350]=true;
// objects[740][350]=true;
// objects[720][390]=true;
// objects[500][400]=true;
// objects[540][400]=true;
// objects[520][440]=true;
// objects[500][650]=true;
DrawTriangle( X1, Y1 , X1+40, Y1 , X1+20 , Y1+40, colors[GOLD] );
DrawTriangle( X2, Y2 , X2+40, Y2 , X2+20 , Y2+40, colors[ORANGE] );
DrawTriangle( X3, Y3 , X3+40, Y3 , X3+20 , Y3+40, colors[BLUE] );
DrawTriangle( X4, Y4 , X4+40, Y4 , X4+20 , Y4+40, colors[GREEN] );
DrawTriangle( X5, Y5 , X5+40, Y5 , X5+20 , Y5+40, colors[VIOLET] );
DrawTriangle( X6, Y6 , X6+40, Y6 , X6+20 , Y6+40, colors[TOMATO] );
DrawTriangle( X7, Y7 , X7+40, Y7 , X7+20 , Y7+40, colors[CRIMSON] );
DrawTriangle( X8, Y8 , X8+40, Y8 , X8+20 , Y8+40, colors[HOT_PINK] );
DrawTriangle( X9, Y9 , X9+40, Y9 , X9+20 , Y9+40, colors[DEEP_PINK] );
DrawTriangle( X10, Y10 , X10+40, Y10 , X10+20 , Y10+40, colors[NAVY] );
DrawTriangle( X11, Y11 , X11+40, Y11 , X11+20 , Y11+40, colors[PLUM] );
DrawTriangle( X12, Y12 , X12+40, Y12 , X12+20 , Y12+40, colors[TAN] );
// DrawTriangle( 300, 450 , 340, 450 , 320 , 490, colors[var] );
// DrawTriangle( 700, 350 , 740, 350 , 720 , 390, colors[ORANGE] );
// DrawTriangle( 500, 400 , 540, 400 , 520 , 440, colors[BLUE] );
// DrawTriangle( 500, 650 , 540, 650 , 520 , 690, colors[GREEN] );
// DrawTriangle( 600, 600 , 640, 600 , 620 , 640, colors[VIOLET] );
// DrawTriangle( 400, 550 , 440, 550 , 420 , 590, colors[TOMATO] );
// DrawTriangle( 350, 500 , 390, 500 , 370 , 540, colors[CRIMSON] );
//
// DrawTriangle( 200, 350 , 240, 350 , 220 , 390, colors[HOT_PINK] );
// DrawTriangle( 100, 400 , 140, 400 , 120 , 440, colors[DEEP_PINK] );
// DrawTriangle( 500, 300 , 540, 300 , 520 , 340, colors[NAVY] );
// DrawTriangle( 150, 200 , 190, 200 , 170 , 240, colors[PLUM] );
// DrawTriangle( 300, 200 , 340, 200 , 320 , 240, colors[TAN] );
//DrawTriangle(xxx, yyy, xxx+40, yyy, xxx+20, yyy+40,colors[WHITE]);
//Display Score
DrawString( 20, 800, "Player1 Score=", colors[BLACK]);
DrawString( 180, 800, Num2Str(score1), colors[BLACK]);
DrawString( 800, 800, "Player2 Score=", colors[BLACK]);
DrawString( 950, 800, Num2Str(score2), colors[BLACK]);
}
/*
* Main Canvas drawing function.
* */
void GameDisplay()/**/{
int nothing=0;
drawBoard(x2, y2);
//drawTaxi();
//drawblueCar();
//drawGreenCar();
//drawCyanCar();
//drawPurpleCar();
DrawTriangle(x2, y2, x2+40, y2, x2+20, y2+40,colors[WHITE]);
drawPassenger((void*)nothing);
drawPlayer((void*)nothing);
glutSwapBuffers(); // do not modify this line..
}
/*This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
*
* */
void NonPrintableKeys(int key, int xx, int yy) {
if (key
== GLUT_KEY_LEFT /*GLUT_KEY_LEFT is constant and contains ASCII for left arrow key*/) {
// what to do when left key is pressed...
//xI -= 10;
a -= 10;
} else if (key
== GLUT_KEY_RIGHT /*GLUT_KEY_RIGHT is constant and contains ASCII for right arrow key*/) {
//xI += 10;
a += 10;
} else if (key
== GLUT_KEY_UP/*GLUT_KEY_UP is constant and contains ASCII for up arrow key*/) {
//yI += 10;
b += 10;
c+=10;
}
else if (key
== GLUT_KEY_DOWN/*GLUT_KEY_DOWN is constant and contains ASCII for down arrow key*/) {
//yI -= 10;
b -= 10;
c-=10;
}
/* This function calls the Display function to redo the drawing. Whenever you need to redraw just call
* this function*/
glutPostRedisplay();
}
void drawing()
{
DrawTriangle(x, y, x+40, y, x+20, y+40,colors[WHITE]);
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int xx, int yy) {
// if (key == 27/* Escape key ASCII*/) {
// exit(1); // exit the program when escape key is pressed.
// }
// x =xI;
// y =yI;
// if (key == 32 && xI==53) //when spacebar pressed disappear the passenger
// {
// if (yI ==670 || yI==187)
// {
// DrawSquare(xI, yI, 30, colors[BLACK]);
// }
//
// cout << "space pressed" << endl;
// }
// ascii of A is 97, d is 100 , s is 115, w is 119
if(key ==100)
{
//xI+=10;
x+=10;
}
else if (key ==97)
{
//xI-=10;
x-=10;
}
else if (key ==115)
{
y-=10;
z-=10;
}
else if (key ==119)
{
y+=10;
z+=10;
}
else if(key==32)
{
// if (objects[x][y]==true || objects[x][z]==true)
// //DrawTriangle(x, y, x+40, y, x+20, y+40,colors[BLUE]);
// cout<<"Object at point ["<<x<<"]["<<y<<"]endl";
// x2=x;
// y2=y;
if(x==X1 && y==Y1)
{X1=-100;
Y1=-100;
score1+=5;
x2=x;
y2=y;}
if(x==X2 && y==Y2)
{X2=-100;
Y2=-100;
score1+=5;
x2=x;
y2=y;}
if(x==X3 && y==Y3)
{X3=-100;
Y3=-100;
score1+=5;
x2=x;
y2=y;}
if(x==X4 && y==Y4)
{X4=-100;
Y4=-100;
score1+=5;
x2=x;
y2=y;}
if(x==X5 && y==Y5)
{X5=-100;
Y5=-100;
score1+=5;
x2=x;
y2=y;}
if(x==X6 && y==Y6)
{X6=-100;
Y6=-100;
score1+=5;
x2=x;
y2=y;}
if(x==X7 && y==Y7)
{X7=-100;
Y7=-100;
score1+=5;
x2=x;
y2=y;}
if(x==X8 && y==Y8)
{X8=-100;
Y8=-100;
score1+=5;
x2=x;
y2=y;}
if(x==X9 && y==Y9)
{X9=-100;
Y9=-100;
score1+=5;
x2=x;
y2=y;}
if(x==X10 && y==Y10)
{X10=-100;
Y10=-100;
score1+=5;
x2=x;
y2=y;}
if(x==X11 && y==Y11)
{X11=-100;
Y11=-100;
score1+=5;
x2=x;
y2=y;}
if(x==X12 && y==Y12)
{X12=-100;
Y12=-100;
score1+=5;
x2=x;
y2=y;}
objects[x][y]=false;
//glutDisplayFunc(drawing);
}
else if (key==8)
{
if(a==X1 && b==Y1)
{X1=-100;
Y1=-100;
score2+=5;
x2=a;
y2=b;}
if(a==X2 && b==Y2)
{X2=-100;
Y2=-100;
score2+=5;
x2=a;
y2=b;}
if(a==X3 && b==Y3)
{X3=-100;
Y3=-100;
score2+=5;
x2=a;
y2=b;}
if(a==X4 && b==Y4)
{X4=-100;
Y4=-100;
score2+=5;
x2=a;
y2=b;}
if(a==X5 && b==Y5)
{X5=-100;
Y5=-100;
score2+=5;
x2=a;
y2=b;}
if(a==X6 && b==Y6)
{X6=-100;
Y6=-100;
score2+=5;
x2=a;
y2=b;}
if(a==X7 && b==Y7)
{X7=-100;
Y7=-100;
score2+=5;
x2=a;
y2=b;}
if(a==X8 && b==Y8)
{X8=-100;
Y8=-100;
score2+=5;
x2=a;
y2=b;}
if(a==X9 && b==Y9)
{X9=-100;
Y9=-100;
score2+=5;
x2=a;
y2=b;}
if(a==X10 && b==Y10)
{X10=-100;
Y10=-100;
score2+=5;
x2=a;
y2=b;}
if(a==X11 && b==Y11)
{X11=-100;
Y11=-100;
score2+=5;
x2=a;
y2=b;}
if(a==X12 && b==Y12)
{X12=-100;
Y12=-100;
score2+=5;
x2=a;
y2=b;}
objects[a][b]=false;
}
glutPostRedisplay();
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
*
* */
void Timer(int m) {
// implement your functionality here
//moveCyanCar();
// once again we tell the library to call our Timer function after next 1000/FPS
glutTimerFunc(100, Timer, 0);
}
/*This function is called (automatically) whenever your mouse moves witin inside the game window
*
* You will have to add the necessary code here for finding the direction of shooting
*
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
*
* */
void MousePressedAndMoved(int x, int y) {
cout << x << " " << y << endl;
glutPostRedisplay();
}
void MouseMoved(int x, int y) {
//cout << x << " " << y << endl;
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
*
* You will have to add the necessary code here for shooting, etc.
*
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
*
* */
void MouseClicked(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON) // dealing only with left button
{
cout << GLUT_DOWN << " " << GLUT_UP << endl;
} else if (button == GLUT_RIGHT_BUTTON) // dealing with right button
{
cout<<"Right Button Pressed"<<endl;
}
glutPostRedisplay();
}
void* creationWindow(void* arg)
{
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
}
/*
* our gateway main function
* */
int main(int argc, char*argv[]) {
int width = 1020, height = 840; // i have set my window size to be 800 x 600
string name1, name2;
pthread_create(&menu_thread, NULL, Menu, (void*) width);
pthread_join(menu_thread, NULL);
//sleep(2);
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("Collect Objects by Vaneeza Ahmad"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// GLuint texture;
// texture = LoadTexture("sprite.bmp");
// glBindTexture (GL_TEXTURE_2D, texture);
// // Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
generateBoard:
// glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
// glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
// glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// // This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
// glutTimerFunc(1000.0, Timer, 0);
pthread_create(&graphics_thread, NULL, creationWindow, (void*) width);
// glutMouseFunc(MouseClicked);
// glutPassiveMotionFunc(MouseMoved); // Mouse
// glutMotionFunc(MousePressedAndMoved); // Mouse
//
pthread_join(graphics_thread, NULL);
pthread_create(&player1_thread, NULL, drawPlayer, NULL);
pthread_join(player1_thread, NULL);
pthread_create(&player2_thread, NULL, drawPassenger, (void*) width);
pthread_join(player2_thread, NULL);
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}
#endif /* RushHour_CPP_ */