Advertisement
Guest User

Untitled

a guest
May 25th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.02 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <stdlib.h>
  3. #include<stdbool.h>
  4. #ifdef __WIN64
  5. #include <windows.h> //for Sleep() only
  6. #include<GL/glut.h>
  7. #elif __WIN32
  8. #include <windows.h> //for Sleep() only
  9. #include<GL/glut.h>
  10. #elif __APPLE_CC__
  11. #include <GLUT/glut.h>
  12. #elif __linux
  13. #include<GL/glut.h>
  14. #elif __unix
  15. #include<GL/glut.h>
  16. #endif
  17. #include<limits.h>
  18. #include<unistd.h>
  19. #include<time.h>
  20. #include<math.h>
  21. #include<cstring>
  22. #include<iostream>
  23. #include<vector>
  24.  
  25.  
  26. #define MAX_CARS 3
  27.  
  28. void drawCar();
  29. void drawFootPath();
  30. void drawDivider();
  31. void drawSurroundings();
  32. void stopGame();
  33. void drawOtherCars(int);
  34. void drawOther();
  35. void setCars();
  36. void moveOtherCars();
  37. int detectCollision();
  38. void drawString(std::string);
  39. void drawMainMenu();
  40. void drawExitMenu();
  41. void drawTime();
  42. void stopGame();
  43. void resumeGame();
  44. void drawScore(int);
  45. void drawDistanceBar();
  46. void drawEnd();
  47. void moveEnd();
  48. void drawFuelBar();
  49. void fuelMessage();
  50. void drawFuel();
  51.  
  52. int foot_y = 200;
  53. int div_y = 220;
  54. int end_y = 160;
  55.  
  56. int a = 0;
  57. int speed = 1;
  58. int steerSpeed = 1;
  59. int temp = 0;
  60. int seconds = 0;
  61. int distance = 178;
  62. int fuel = 178;
  63.  
  64. int game_state = 0;
  65.  
  66. void moveDivider();
  67. void moveRoad();
  68. void moveCar();
  69. void periodicFunction(int);
  70. void moveFuel();
  71.  
  72. int lane[MAX_CARS];
  73. int pos[MAX_CARS];
  74. int speeds[MAX_CARS];
  75. int speedsTemp[MAX_CARS];
  76.  
  77. GLdouble width=1200, height=800;
  78.  
  79. bool carMoveLeft = false, carMoveRight = false, carMoveFast = false, carStopped = false, gameStopped = false, horn = false;
  80. bool highlightStart = false, highlightExit = false;
  81. bool reachedEnd = false, gameCompleted = false, fuelOver = false;
  82. //Car variables
  83. int car_x = 0;
  84. int car_y = -100;
  85.  
  86. int fuel_x = 0;
  87. int fuel_y = -80;
  88.  
  89. void maindisp()
  90. {
  91.  
  92. glClear(GL_COLOR_BUFFER_BIT);
  93. glMatrixMode(GL_MODELVIEW);
  94.  
  95. switch(game_state)
  96. {
  97. case 1 : glClearColor(0.5, 0.5, 0.5, 0.0);
  98. drawFootPath();
  99. drawSurroundings();
  100. drawDivider();
  101. drawEnd();
  102. drawCar();
  103. drawOther();
  104. drawTime();
  105. drawDistanceBar();
  106. drawFuelBar();
  107. drawFuel();
  108. break;
  109.  
  110. case 0 : drawMainMenu();
  111.  
  112. break;
  113.  
  114. case 2 : glClearColor(0.5, 0.5, 0.5, 0.0);
  115. drawFootPath();
  116. drawSurroundings();
  117. drawDivider();
  118. if(gameCompleted)
  119. drawEnd();
  120.  
  121. if(fuelOver)
  122. fuelMessage();
  123.  
  124. drawCar();
  125. drawOther();
  126. drawDistanceBar();
  127. drawFuelBar();
  128. drawExitMenu();
  129. drawTime();
  130. break;
  131. }
  132.  
  133. glFlush();
  134. glutSwapBuffers();
  135. }
  136.  
  137. void keyBoardFunc(int key, int x, int y)
  138. {
  139. switch(key)
  140. {
  141. case GLUT_KEY_LEFT : carMoveLeft = true; carMoveRight = false; break;
  142. case GLUT_KEY_RIGHT : carMoveRight = true; carMoveLeft = false; break;
  143. case GLUT_KEY_UP : carMoveFast = true; break;
  144. }
  145. }
  146.  
  147. void keyboard_up_func(int k, int x, int y)
  148. {
  149. switch(k)
  150. {
  151. case GLUT_KEY_LEFT: carMoveLeft=false;
  152. break;
  153. case GLUT_KEY_RIGHT:carMoveRight=false;
  154. break;
  155.  
  156. case GLUT_KEY_UP : carMoveFast = false; break;
  157. case GLUT_KEY_DOWN : carStopped = false; break;
  158. }
  159. }
  160.  
  161. void normalKeyBoardFunc(unsigned char key, int x, int y)
  162. {
  163. if(game_state == 1)
  164. {
  165. switch(key)
  166. {
  167. //Horn
  168. case 'H':
  169. case 'h': horn = true; break;
  170.  
  171. }
  172. }
  173.  
  174. else if(game_state == 0)
  175. {
  176. if(key == 13)
  177. {
  178. setCars();
  179. game_state = 1;
  180. }
  181. }
  182. }
  183.  
  184. void mouse_func(int button, int state, int x, int y)
  185. {
  186. switch(game_state)
  187. {
  188. case 0 : if(button == GLUT_LEFT_BUTTON && state == GLUT_UP)
  189. {
  190. if(x >= 630 && x <= 810 && y >= 320 && y <= 405)
  191. {
  192. //gameStopped = false;
  193. setCars();
  194. game_state = 1;
  195. }
  196.  
  197. else if(x >= 630 && x <= 810 && y >= 490 && y <= 575)
  198. exit(0);
  199. }
  200. break;
  201.  
  202. case 2 : if(button == GLUT_LEFT_BUTTON && state == GLUT_UP)
  203. {
  204. if(x >= 630 && x <= 810 && y >= 320 && y <= 405)
  205. {
  206. gameStopped = false;
  207. gameCompleted = false;
  208. reachedEnd = false;
  209. seconds = 0;
  210. distance = 178;
  211. fuel = 178;
  212. fuelOver = false;
  213. end_y = 160;
  214. setCars();
  215. game_state = 1;
  216. car_x = 0;
  217. }
  218.  
  219. else if(x >= 630 && x <= 810 && y >= 490 && y <= 575)
  220. exit(0);
  221. }
  222. break;
  223.  
  224.  
  225.  
  226. }
  227. }
  228.  
  229. void mouse_hover(int x, int y)
  230. {
  231. if(x >= 630 && x <= 810 && y >= 320 && y <= 405)
  232. {
  233. highlightStart = true;
  234. highlightExit = false;
  235. }
  236.  
  237. else if(x >= 630 && x <= 810 && y >= 490 && y <= 575)
  238. {
  239. highlightStart = false;
  240. highlightExit = true;
  241. }
  242.  
  243. else
  244. {
  245. highlightExit = false;
  246. highlightStart = false;
  247. }
  248. }
  249.  
  250. int main(int argc, char *argv[])
  251. {
  252. glutInit(&argc, argv);
  253. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
  254. glutInitWindowSize((int) width, (int) height);
  255. glutCreateWindow("Galaxian");
  256. glutFullScreen();
  257.  
  258.  
  259. gluOrtho2D(-240.0, 240.0, -160.0, 160.0);
  260.  
  261. glutDisplayFunc(maindisp);
  262. glutTimerFunc(100, periodicFunction, 0);
  263.  
  264. glutSpecialFunc(keyBoardFunc);
  265. glutSpecialUpFunc(keyboard_up_func);
  266. glutKeyboardFunc(normalKeyBoardFunc);
  267. glutMouseFunc(mouse_func);
  268. glutPassiveMotionFunc(mouse_hover);
  269.  
  270. glutMainLoop();
  271. return 0;
  272. }
  273.  
  274. //This function draws a car at (car_x, car_y)
  275. void drawCar(void)
  276. {
  277. glPushMatrix();
  278.  
  279. glTranslated(car_x, car_y, 0.0);
  280.  
  281. glColor3f(0.34, 1.0, 1.0);
  282.  
  283. glRectd(2, 10, -2, -10);
  284. glRectf(-8, 12, 8, 13);
  285. glRectf(-10.0, 9.0, -8.0, 15.0);
  286. glRectf(8.0, 9.0, 10.0, 15.0);
  287.  
  288. glBegin(GL_LINES);
  289. glVertex2f(8.0,11.0);
  290. glVertex2f(6.0,7.0);
  291. glVertex2f(6.0,7.0);
  292. glVertex2f(6.0,-7.0);
  293. glVertex2f(6.0,-7.0);
  294. glVertex2f(10.0,-12.0);
  295. glVertex2f(-8.0,11.0);
  296. glVertex2f(-6.0,7.0);
  297. glVertex2f(-6.0,7.0);
  298. glVertex2f(-6.0,-7.0);
  299. glVertex2f(-6.0,-7.0);
  300. glVertex2f(-10.0,-12.0);
  301. glVertex2f(10.0,-12.0);
  302. glVertex2f(-10.0,-12.0);
  303. glEnd();
  304.  
  305. glRectf(-11.0, -16.0, -9.0, -8.0);
  306. glRectf(9.0, -16.0, 11.0, -8.0);
  307. glRectf(-7.0, -14.0, -5.0, -12.0);
  308. glRectf(5.0, -14.0, 7.0, -12.0);
  309.  
  310. glBegin(GL_POINTS);
  311. glVertex2f(-7,-15);
  312. glVertex2f(-5,-15);
  313. glVertex2f(-8,-16);
  314. glVertex2f(-4,-16);
  315. glVertex2f(-6,-16);
  316. glVertex2f(-6,-17);
  317. glEnd();
  318.  
  319. glPopMatrix();
  320. }
  321.  
  322. void drawOtherCars(int i)
  323. {
  324. glPushMatrix();
  325.  
  326. glTranslated((lane[i] - 1)*37, pos[i], 0.0);
  327.  
  328. switch(speeds[i])
  329. {
  330. case 2 : glColor3f(0.0, 1.0, 0.0); break;
  331. case 3 : glColor3f(0.0, 0.0, 1.0); break;
  332. case 4 :
  333. case 5 :
  334. case 6:
  335. case 7 : glColor3f(1.0, 0.0, 0.0); break;
  336. case 0 : glColor3f(0.0, 0.0, 0.0); break;
  337. }
  338.  
  339. glRectd(2, 10, -2, -10);
  340. glRectf(-8, 12, 8, 13);
  341. glRectf(-10.0, 9.0, -8.0, 15.0);
  342. glRectf(8.0, 9.0, 10.0, 15.0);
  343.  
  344. glBegin(GL_LINES);
  345. glVertex2f(8.0,11.0);
  346. glVertex2f(6.0,7.0);
  347. glVertex2f(6.0,7.0);
  348. glVertex2f(6.0,-7.0);
  349. glVertex2f(6.0,-7.0);
  350. glVertex2f(10.0,-12.0);
  351. glVertex2f(-8.0,11.0);
  352. glVertex2f(-6.0,7.0);
  353. glVertex2f(-6.0,7.0);
  354. glVertex2f(-6.0,-7.0);
  355. glVertex2f(-6.0,-7.0);
  356. glVertex2f(-10.0,-12.0);
  357. glVertex2f(10.0,-12.0);
  358. glVertex2f(-10.0,-12.0);
  359. glEnd();
  360.  
  361. glRectf(-11.0, -16.0, -9.0, -8.0);
  362. glRectf(9.0, -16.0, 11.0, -8.0);
  363. glRectf(-7.0, -14.0, -5.0, -12.0);
  364. glRectf(5.0, -14.0, 7.0, -12.0);
  365.  
  366. glBegin(GL_POINTS);
  367. glVertex2f(-7,-15);
  368. glVertex2f(-5,-15);
  369. glVertex2f(-8,-16);
  370. glVertex2f(-4,-16);
  371. glVertex2f(-6,-16);
  372. glVertex2f(-6,-17);
  373. glEnd();
  374.  
  375. glPopMatrix();
  376. }
  377.  
  378. void drawFootPath()
  379. {
  380. int i, y;
  381. glPushMatrix();
  382.  
  383. //Draw right side foot path
  384. glTranslated(60, 0, 0);
  385. y = foot_y + 20;
  386.  
  387. for(i = 0; i < 20; i++)
  388. {
  389. if(a == 0)
  390. {
  391. if(i % 2 == 0)
  392. glColor3f(1.0, 1.0, 1.0);
  393. else
  394. glColor3f(1.0, 1.0, 0.0);
  395. }
  396.  
  397. else
  398. {
  399. if(i % 2 == 1)
  400. glColor3f(1.0, 1.0, 1.0);
  401. else
  402. glColor3f(1.0, 1.0, 0.0);
  403. }
  404.  
  405.  
  406. y -= 20;
  407. glRectd(5, y, -5, y - 20);
  408. }
  409. glPopMatrix();
  410.  
  411. //Draw left side foot path
  412. glPushMatrix();
  413. glTranslated(-60, 0, 0);
  414.  
  415. y = foot_y + 20;
  416. for(i = 0; i < 20; i++)
  417. {
  418.  
  419. if(a == 0)
  420. {
  421. if(i % 2 == 0)
  422. glColor3f(1.0, 1.0, 1.0);
  423. else
  424. glColor3f(1.0, 1.0, 0.0);
  425. }
  426.  
  427. else
  428. {
  429. if(i % 2 == 1)
  430. glColor3f(1.0, 1.0, 1.0);
  431. else
  432. glColor3f(1.0, 1.0, 0.0);
  433. }
  434.  
  435. y -= 20;
  436. glRectd(5, y, -5, y - 20);
  437. }
  438. glPopMatrix();
  439.  
  440. }
  441.  
  442. void drawDivider()
  443. {
  444. int i;
  445. int y = div_y + 80;
  446. glColor3f(1.0, 1.0, 1.0);
  447.  
  448. //Draw left divider
  449.  
  450. for(i = 0 ; i < 8; i++)
  451. {
  452. y-=80;
  453. glRectd(22, y, 18, y-40);
  454. glRectd(-18, y, -22, y-40);
  455. }
  456.  
  457. }
  458.  
  459. void drawSurroundings()
  460. {
  461. glColor3f(0.0, 1.0, 0.0);
  462. glRectd(240, 160, 65, -160);
  463. glRectd(-240, 160, -65, -160);
  464. }
  465.  
  466. void stopGame()
  467. {
  468. speed = 0;
  469. steerSpeed = 0;
  470. }
  471.  
  472. void resumeGame()
  473. {
  474. speed = 2;
  475. steerSpeed = 1;
  476. }
  477.  
  478. void drawOther()
  479. {
  480. int i;
  481. for(i = 0; i < MAX_CARS; i++)
  482. {
  483. drawOtherCars(i);
  484. }
  485. }
  486.  
  487. void setCars()
  488. {
  489. int i;
  490. for(i = 0; i < MAX_CARS; i++)
  491. {
  492. lane[i] = i;
  493. pos[i] = 110 + rand()%100;
  494. speeds[i] = 1 + i + rand()%4;
  495. }
  496. }
  497.  
  498. void moveDivider()
  499. {
  500. div_y -= speed;
  501. if(div_y < 120 && distance > 0)
  502. {
  503. div_y = 200;
  504. distance -= 1;
  505. fuel -= 2;
  506.  
  507. if(fuel < 0)
  508. fuelOver = true;
  509.  
  510. if(distance < 5)
  511. reachedEnd = true;
  512.  
  513. }
  514. }
  515.  
  516. void moveCar()
  517. {
  518. if(carMoveLeft)
  519. car_x -= steerSpeed;
  520.  
  521.  
  522. else if(carMoveRight)
  523. car_x += steerSpeed;
  524.  
  525. if(car_x > 45 || car_x < -45)
  526. {
  527. game_state = 2;
  528. gameStopped = true;
  529. }
  530. }
  531.  
  532. void moveRoad()
  533. {
  534. foot_y -= speed;
  535. if(foot_y < 160)
  536. {
  537. foot_y = 180;
  538. if(a == 0)
  539. a = 1;
  540. else
  541. a = 0;
  542. }
  543. }
  544.  
  545. void periodicFunction(int v)
  546. {
  547. if(gameStopped)
  548. stopGame();
  549.  
  550. else
  551. resumeGame();
  552.  
  553. if(speed != 0)
  554. {
  555. if(carMoveFast)
  556. speed = 6;
  557.  
  558. else
  559. speed = 2;
  560.  
  561. }
  562.  
  563. if(fuelOver)
  564. {
  565. gameStopped = true;
  566. game_state = 2;
  567.  
  568. }
  569.  
  570. moveRoad();
  571. moveDivider();
  572. moveCar();
  573. moveOtherCars();
  574. moveEnd();
  575. moveFuel();
  576.  
  577. if(!detectCollision() && !gameStopped && game_state == 1)
  578. temp += 15;
  579.  
  580. if(temp > 1000)
  581. {
  582. temp = 0;
  583. seconds++;
  584. }
  585.  
  586. glutPostRedisplay();
  587. glutTimerFunc(10, periodicFunction, v);
  588. }
  589.  
  590. void moveOtherCars()
  591. {
  592. int i;
  593. for(i = 0; i < MAX_CARS; i++)
  594. {
  595. pos[i] += -speed + speeds[i];
  596.  
  597. if(pos[i] < -200 || pos[i] > 200)
  598. {
  599. pos[i] = 200 + rand()%100;
  600. speeds[i] = 2 + rand()%4;
  601. }
  602. //if((speeds[0] == speeds[1]) && (speeds[1] == speeds[2]))
  603. // speeds[1]--;
  604.  
  605. }
  606.  
  607. if(horn)
  608. {
  609. speeds[(car_x + 60)/40]++;
  610. if(speeds[(car_x + 60)/40] > 7)
  611. speeds[(car_x + 60)/40] = 7;
  612. horn = false;
  613. }
  614. }
  615.  
  616. int detectCollision()
  617. {
  618. if(game_state != 1)
  619. return 0;
  620.  
  621. int i, limit;
  622. for(i = 0; i < MAX_CARS; i++)
  623. {
  624. if(pos[i] < -70 && pos[i] > -130)
  625. {
  626. limit = (i - 1)*40;
  627. if(car_x < limit + 22 && car_x > limit - 22)
  628. {
  629. speeds[i] = 0;
  630. gameStopped = true;
  631. game_state = 2;
  632. return 1;
  633. }
  634. }
  635. }
  636.  
  637. if((fuel_x > car_x && fuel_x - car_x < 20) || (fuel_x < car_x && car_x - fuel_x < 20))
  638. {
  639. if(fuel_y < -80 && fuel_y > -120)
  640. {
  641. fuel+=20;
  642. if(fuel > 178)
  643. fuel = 178;
  644. fuel_y = 600 + rand()%150;
  645. fuel_x = (rand()%3 - 1)*37;
  646. }
  647. }
  648.  
  649. return 0;
  650.  
  651. }
  652.  
  653. void draw_string(std::string str)
  654. {
  655. for(unsigned int i=0;i<str.length();i++)
  656. {
  657. glutStrokeCharacter(GLUT_STROKE_ROMAN, *(str.begin()+i));
  658. }
  659. }
  660.  
  661. void drawMainMenu()
  662. {
  663. //Draw start button
  664. glClearColor(0.5, 0.5, 0.5, 0.0);
  665. glColor3f(0.0, 0.0, 0.0);
  666. drawFootPath();
  667. drawSurroundings();
  668. drawDivider();
  669. drawCar();
  670.  
  671. glColor3f(1.0, 1.0, 1.0);
  672. glPushMatrix();
  673. glTranslated(0, 30, 0);
  674. glBegin(GL_LINE_LOOP);
  675. glVertex2f(30, 15);
  676. glVertex2f(30, -15);
  677. glVertex2f(-30, -15);
  678. glVertex2d(-30, 15);
  679. glEnd();
  680. glPopMatrix();
  681.  
  682. glPushMatrix();
  683. glTranslated(-20, 30, 0);
  684. glScalef(0.1, 0.1, 0.1);
  685. glColor3f(1.0, 1.0, 1.0);
  686. draw_string("START");
  687.  
  688. glPopMatrix();
  689.  
  690. //Draw exit button
  691. glColor3f(1.0, 1.0, 1.0);
  692.  
  693. glPushMatrix();
  694. glTranslated(0, -30, 0);
  695. glBegin(GL_LINE_LOOP);
  696. glVertex2f(30, 15);
  697. glVertex2f(30, -15);
  698. glVertex2f(-30, -15);
  699. glVertex2d(-30, 15);
  700. glEnd();
  701. glPopMatrix();
  702.  
  703. glPushMatrix();
  704. glTranslated(-15, -30, 0);
  705. glScalef(0.1, 0.1, 0.1);
  706. draw_string("EXIT");
  707.  
  708. glPopMatrix();
  709.  
  710. if(highlightStart)
  711. {
  712. glColor3f(1.0, 0.0, 0.0);
  713.  
  714. glPushMatrix();
  715. glTranslated(0, 30, 0);
  716. glBegin(GL_LINE_LOOP);
  717. glVertex2f(35, 20);
  718. glVertex2f(35, -20);
  719. glVertex2f(-35, -20);
  720. glVertex2d(-35, 20);
  721. glEnd();
  722. glPopMatrix();
  723.  
  724. }
  725.  
  726. if(highlightExit)
  727. {
  728. glColor3f(1.0, 0.0, 0.0);
  729.  
  730. glPushMatrix();
  731. glTranslated(0, -30, 0);
  732. glBegin(GL_LINE_LOOP);
  733. glVertex2f(35, 20);
  734. glVertex2f(35, -20);
  735. glVertex2f(-35, -20);
  736. glVertex2d(-35, 20);
  737. glEnd();
  738. glPopMatrix();
  739.  
  740. }
  741.  
  742.  
  743.  
  744.  
  745. }
  746.  
  747. void drawExitMenu()
  748. {
  749. //Draw start button
  750. /*
  751. glClearColor(0.5, 0.5, 0.5, 0.0);
  752. glColor3f(0.0, 0.0, 0.0);
  753. drawFootPath();
  754. drawSurroundings();
  755. drawDivider();
  756. drawCar();
  757. */
  758. glColor3f(1.0, 1.0, 1.0);
  759. glPushMatrix();
  760. glTranslated(0, 30, 0);
  761. glBegin(GL_LINE_LOOP);
  762. glVertex2f(30, 15);
  763. glVertex2f(30, -15);
  764. glVertex2f(-30, -15);
  765. glVertex2d(-30, 15);
  766. glEnd();
  767. glPopMatrix();
  768.  
  769. glPushMatrix();
  770. glTranslated(-25, 30, 0);
  771. glScalef(0.1, 0.1, 0.1);
  772. glColor3f(1.0, 1.0, 1.0);
  773. draw_string("RESTART");
  774.  
  775. glPopMatrix();
  776.  
  777. //Draw exit button
  778. glColor3f(1.0, 1.0, 1.0);
  779.  
  780. glPushMatrix();
  781. glTranslated(0, -30, 0);
  782. glBegin(GL_LINE_LOOP);
  783. glVertex2f(30, 15);
  784. glVertex2f(30, -15);
  785. glVertex2f(-30, -15);
  786. glVertex2d(-30, 15);
  787. glEnd();
  788. glPopMatrix();
  789.  
  790. glPushMatrix();
  791. glTranslated(-15, -30, 0);
  792. glScalef(0.1, 0.1, 0.1);
  793. draw_string("EXIT");
  794.  
  795. glPopMatrix();
  796.  
  797. if(highlightStart)
  798. {
  799. glColor3f(1.0, 0.0, 0.0);
  800.  
  801. glPushMatrix();
  802. glTranslated(0, 30, 0);
  803. glBegin(GL_LINE_LOOP);
  804. glVertex2f(35, 20);
  805. glVertex2f(35, -20);
  806. glVertex2f(-35, -20);
  807. glVertex2d(-35, 20);
  808. glEnd();
  809. glPopMatrix();
  810.  
  811. }
  812.  
  813. if(highlightExit)
  814. {
  815. glColor3f(1.0, 0.0, 0.0);
  816.  
  817. glPushMatrix();
  818. glTranslated(0, -30, 0);
  819. glBegin(GL_LINE_LOOP);
  820. glVertex2f(35, 20);
  821. glVertex2f(35, -20);
  822. glVertex2f(-35, -20);
  823. glVertex2d(-35, 20);
  824. glEnd();
  825. glPopMatrix();
  826.  
  827. }
  828. }
  829.  
  830. void drawTime()
  831. {
  832. glColor3f(1.0, 1.0, 1.0);
  833. glPushMatrix();
  834. glTranslated(-200, 90, 0);
  835. glBegin(GL_LINE_LOOP);
  836. glVertex2f(50, 15);
  837. glVertex2f(50, -15);
  838. glVertex2f(-30, -15);
  839. glVertex2d(-30, 15);
  840. glEnd();
  841. glPopMatrix();
  842.  
  843. glPushMatrix();
  844. glTranslated(-220, 85, 0);
  845. glScalef(0.1, 0.1, 0.1);
  846. glColor3f(1.0, 1.0, 1.0);
  847. draw_string("Time: ");
  848. glPopMatrix();
  849.  
  850. glPushMatrix();
  851. glTranslated(-180, 85, 0);
  852. glScalef(0.1, 0.1, 0.1);
  853. glColor3f(1.0, 0.0, 0.0);
  854. //glutStrokeCharacter(GLUT_STROKE_ROMAN, '4');
  855. drawScore(seconds);
  856. glPopMatrix();
  857. }
  858.  
  859. void drawScore(int score)
  860. {
  861. int temp = score;
  862. int str[20],i=0;
  863.  
  864. while(temp>0)
  865. {
  866. str[i++] = (temp%10);
  867. temp /= 10;
  868. }
  869. i--;
  870. while(i>=0)
  871. {
  872. glutStrokeCharacter(GLUT_STROKE_ROMAN, str[i--]+'0');
  873. }
  874. if(score == 0) glutStrokeCharacter(GLUT_STROKE_ROMAN, '0');
  875. }
  876.  
  877. void drawDistanceBar()
  878. {
  879. glPushMatrix();
  880. glColor3f(0.0, 0.0, 0.0);
  881.  
  882. glTranslated(-75, 40, 0);
  883. glBegin(GL_LINE_LOOP);
  884. glVertex2f(5, 90);
  885. glVertex2f(5, -90);
  886. glVertex2f(-5, -90);
  887. glVertex2d(-5, 90);
  888. glEnd();
  889. glPopMatrix();
  890.  
  891. glPushMatrix();
  892. glTranslated(-75, -49, 0);
  893. glColor3f(0.6, 0.6, 0.6);
  894. glRectd(4, 0, -4, distance);
  895. glPopMatrix();
  896.  
  897. }
  898.  
  899. void drawFuelBar()
  900. {
  901. glPushMatrix();
  902. glColor3f(0.0, 0.0, 0.0);
  903.  
  904. glTranslated(75, 40, 0);
  905. glBegin(GL_LINE_LOOP);
  906. glVertex2f(5, 90);
  907. glVertex2f(5, -90);
  908. glVertex2f(-5, -90);
  909. glVertex2d(-5, 90);
  910. glEnd();
  911. glPopMatrix();
  912.  
  913. glPushMatrix();
  914. glTranslated(75, -49, 0);
  915. glColor3f(0.6, 0.6, 0.6);
  916. glRectd(4, 0, -4, fuel);
  917. glPopMatrix();
  918.  
  919. }
  920.  
  921. void drawEnd()
  922. {
  923. int i, j;
  924. for(i = 0; i < 5; i++)
  925. {
  926. if(i % 2 == 0)
  927. glColor3f(0.0, 0.0, 0.0);
  928. else
  929. glColor3f(1.0, 1.0, 1.0);
  930. glPushMatrix();
  931.  
  932. glTranslated(-55 , end_y + 10*i, 0);
  933.  
  934. for(j = 0; j < 11; j++)
  935. {
  936. if(i%2)
  937. {
  938. if(j % 2 == 0)
  939. glColor3f(0.0, 0.0, 0.0);
  940. else
  941. glColor3f(1.0, 1.0, 1.0);
  942.  
  943. }
  944.  
  945. else
  946. {
  947. if(j % 2)
  948. glColor3f(0.0, 0.0, 0.0);
  949. else
  950. glColor3f(1.0, 1.0, 1.0);
  951. }
  952.  
  953. glRectd(10 * j,0,10*(j+1),10);
  954. }
  955.  
  956. glPopMatrix();
  957. }
  958. }
  959.  
  960. void fuelMessage()
  961. {
  962. glPushMatrix();
  963. glTranslated(75, -70, 0);
  964. glScalef(0.1,0.1,0.1);
  965. glColor3f(1.0, 0,0);
  966. draw_string("Fuel Exhausted\n");
  967. glPopMatrix();
  968. }
  969.  
  970. void moveEnd()
  971. {
  972. if(reachedEnd)
  973. end_y-= speed;
  974.  
  975. if(end_y < -125)
  976. {
  977. gameStopped = true;
  978. gameCompleted = true;
  979. game_state = 2;
  980. }
  981. }
  982.  
  983. void drawFuel()
  984. {
  985. glPushMatrix();
  986. glTranslated(fuel_x, fuel_y, 0);
  987. glColor3f(0.0, 1.0, 0.0);
  988. glRectf(5, 5, -5, -5);
  989. glPopMatrix();
  990.  
  991. glPushMatrix();
  992. glTranslated(fuel_x, fuel_y+5, 0);
  993. glBegin(GL_LINE_LOOP);
  994. glVertex2f(5, 0);
  995. glVertex2f(5, 5);
  996. glVertex2f(-3, 5);
  997. glVertex2d(-5, 0);
  998. glEnd();
  999. glPopMatrix();
  1000.  
  1001. }
  1002.  
  1003. void moveFuel()
  1004. {
  1005. fuel_y-=speed;
  1006.  
  1007. if(fuel_y < -200)
  1008. {
  1009. fuel_y = 600 + rand()%150;
  1010. fuel_x = (rand()%3 - 1)*37;
  1011. }
  1012. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement