Guest User

Untitled

a guest
Apr 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.02 KB | None | 0 0
  1. /*
  2. author: S. M. Shahriar Nirjon
  3. last modified: August 8, 2008
  4. */
  5. # include "iGraphics.h"
  6.  
  7. /*
  8. function iDraw() is called again and again by the system.
  9. */
  10. int wd = 800;
  11. int hg = 600;
  12. int sp = 10;
  13. int start = 0;
  14. int gameOption = 0;
  15. int help = 0;
  16. int rad = 35;
  17. int newGame = 0;
  18. int exitOption = 0;
  19. int optionMenu = 0;
  20. int easyMaze = 0;
  21. int dx = 30;
  22. int count = 0;
  23. int foodCount = 0;
  24. int foodFound = 0;
  25. int food[6];
  26. int finish = 0;
  27. char score[1000] = "0";
  28.  
  29.  
  30.  
  31.  
  32. void drawFood(int x, int y, int rad)
  33. {
  34. iSetcolor(255, 0, 0);
  35. iFilledCircle(x, y, rad);
  36. iSetcolor(0, 0, 0);
  37. iCircle(x, y, rad);
  38. }
  39.  
  40. void drawCircle(int x, int y, int rad)
  41. {
  42. iSetcolor(0, 64, 128);
  43. iFilledCircle(x, y, rad);
  44. iSetcolor(0, 0, 0);
  45. iCircle(x, y, rad);
  46. }
  47.  
  48. void drawEllipse(int x, int y, int a, int b)
  49. {
  50. iSetcolor(0, 64, 128);
  51. iFilledEllipse(x, y, a, b);
  52. iSetcolor(0, 0, 0);
  53. iEllipse(x, y, a, b);
  54. }
  55.  
  56. void drawEye(int x, int y, int r)
  57. {
  58. iSetcolor(255, 255, 255);
  59. iFilledCircle(x, y, r);
  60. iSetcolor(0, 0, 0);
  61. iCircle(x, y, r);
  62. }
  63.  
  64. void drawEyeBall(int x, int y, int r)
  65. {
  66. iSetcolor(0, 0, 0);
  67. iFilledCircle(x, y, r);
  68. }
  69.  
  70. void clearFood(int u, int v)
  71. {
  72. iSetcolor(34, 177, 76);
  73. drawFood(u, v, 15);
  74. }
  75.  
  76. void drawFoodPlacing()
  77. {
  78. if(food[0] == 0) drawFood(25 + (10 * 30) + 15, 30 + (5 * 30) + 15, 15);
  79. if(food[1] == 0) drawFood(25 + (13 * 30) + 15, 30 + (5 * 30) + 15, 15);
  80. if(food[2] == 0) drawFood(25 + (13 * 30) + 15, 30 + (8 * 30) + 15, 15);
  81. if(food[3] == 0) drawFood(25 + (7 * 30) + 15, 30 + (16 * 30) + 15, 15);
  82. if(food[4] == 0) drawFood(25 + (10 * 30) + 15, 30 + (13 * 30) + 15, 15);
  83. if(food[5] == 0) drawFood(25 + (10 * 30) + 15, 30 + (10 * 30) + 15, 15);
  84. }
  85.  
  86. int drawMovingSnake()
  87. {
  88. drawCircle(25 + (5 * 30) + 15 + (count * dx), 30 + (5 * 30) + 15, 15);
  89. drawEllipse(25 + (4 * 30) + 15 + (count * dx), 30 + (5 * 30) + 15, 15, 10);
  90. drawEllipse(25 + (3 * 30) + 15 + (count * dx), 30 + (5 * 30) + 15, 15, 10);
  91. drawEllipse(25 + (2 * 30) + 15 + (count * dx), 30 + (5 * 30) + 15, 15, 10);
  92. drawEye(25 + (5 * 30) + 15 + 5 + (count * dx), 30 + (5 * 30) + 15 + 7, 5);
  93. drawEye(25 + (5 * 30) + 15 + 5 + (count * dx), 30 + (5 * 30) + 10, 5);
  94. drawEyeBall(25 + (5 * 30) + 15 + 5 + (count * dx), 30 + (5 * 30) + 15 + 7, 2);
  95. drawEyeBall(25 + (5 * 30) + 15 + 5 + (count * dx), 30 + (5 * 30) + 10, 2);
  96.  
  97. return 25 + (5 * 30) + 15 + (count * dx);
  98. }
  99.  
  100. void drawSnakePic()
  101. {
  102. drawCircle(60, 300, 35);
  103. drawEllipse(116, 272, 30, 20);
  104. drawEllipse(116 + 60, 272, 30, 20);
  105. drawEllipse(116 + 120, 272, 30, 20);
  106. drawEye(45, 290, 10);
  107. drawEye(65, 310, 10);
  108. drawEyeBall(42, 291, 7);
  109. drawEyeBall(62, 312, 7);
  110. }
  111.  
  112. void drawTitle()
  113. {
  114. iSetcolor(0, 0, 51);
  115.  
  116. //Draw S
  117. double sx[] = {100, 140, 200, 160};
  118. double sy[] = {hg - 200, hg - 200, hg - 260, hg - 260};
  119.  
  120. iFilledRectangle(100, hg - 200, 100, 30);
  121. iFilledRectangle(100, hg - 200 - 60 - 30, 100, 30);
  122. iFilledPolygon(sx, sy, 4);
  123.  
  124. //Draw N
  125.  
  126. double nx[] = {200 + sp, 230 + sp, 290 + sp, 260 + sp};
  127. double ny[] = {hg - 170, hg - 170, hg - 290, hg - 290};
  128.  
  129. iFilledRectangle(100 + 100 + sp, hg - 200 - 90, 30, 120);
  130. iFilledRectangle(100 + 100 + 60 + sp, hg - 200 - 90, 30, 120);
  131. iFilledPolygon(nx, ny, 4);
  132.  
  133. //Draw A
  134.  
  135. double a1x[] = {340 + sp, 370 + sp, 330 + sp, 300 + sp};
  136. double a1y[] = {hg - 170, hg - 170, hg - 290, hg - 290};
  137.  
  138. double a2x[] = {340 + sp, 370 + sp, 410 + sp, 380 + sp};
  139. double a2y[] = {hg - 170, hg - 170, hg - 290, hg - 290};
  140.  
  141. double a3x[] = {320 + sp, 390 + sp, 400 + sp, 310 + sp};
  142. double a3y[] = {hg - 235, hg - 235, hg - 265, hg - 265};
  143.  
  144. iFilledPolygon(a1x, a1y, 4);
  145. iFilledPolygon(a2x, a2y, 4);
  146. iFilledPolygon(a3x, a3y, 4);
  147.  
  148. //Draw K
  149.  
  150. double k1x[] = {420 + sp, 420 + sp, 500 + sp, 500 + sp};
  151. double k1y[] = {hg - 210, hg - 250, hg - 170 - 30, hg - 170};
  152.  
  153. double k2x[] = {420 + sp, 420 + sp, 500 + sp, 500 + sp};
  154. double k2y[] = {hg - 210, hg - 250, hg - 290, hg - 260};
  155.  
  156. iFilledRectangle(420 + sp, hg - 290, 30, 120);
  157. iFilledPolygon(k1x, k1y, 4);
  158. iFilledPolygon(k2x, k2y, 4);
  159.  
  160. //Draw E
  161. iFilledRectangle(510 + sp, hg - 290, 30, 120);
  162. iFilledRectangle(510 + sp, hg - 200, 80, 30);
  163. iFilledRectangle(510 + sp, hg - 245, 60, 30);
  164. iFilledRectangle(510 + sp, hg - 290, 80, 30);
  165.  
  166. //Game
  167. iSetcolor(0,0,51);
  168. //iShowBMP(220, 20, "pic1.bmp");
  169.  
  170. //start
  171. iSetcolor(0, 0, 51);
  172. iText(600, 150, "START...", GLUT_BITMAP_TIMES_ROMAN_24);
  173. }
  174.  
  175. void drawPlayGround()
  176. {
  177. int i = 0 ;
  178. int j = 0;
  179.  
  180. iSetcolor(233, 233, 233);
  181. iFilledRectangle(0, 0, wd, hg - 30);
  182.  
  183. iSetcolor(34, 177, 76);
  184. iFilledRectangle(25, 30, wd - 50, hg - 90);
  185. }
  186.  
  187. void drawMenuBar()
  188. {
  189. iSetcolor(200, 191, 231);
  190. iFilledRectangle(0, hg - 30, 800, 30);
  191. iSetcolor(0, 0, 0);
  192. //iRectangle(10, hg - 25, 50, 20);//Game
  193. //iRectangle(70, hg - 25, 50, 20);//Help
  194. iText(15, hg - 20, "Game", GLUT_BITMAP_9_BY_15);
  195. iText(75, hg - 20, "Help", GLUT_BITMAP_9_BY_15);
  196. iText(500, 10, "Score : ", GLUT_BITMAP_9_BY_15);
  197. iRectangle(570, 5, 60, 20);
  198. iText(590, 10, score, GLUT_BITMAP_9_BY_15);
  199. }
  200.  
  201. void drawFirstPage()
  202. {
  203. iSetcolor(119, 119, 255);
  204. iFilledRectangle(0, 0, 800, 600);
  205. drawTitle();
  206. drawSnakePic();
  207. }
  208.  
  209. void drawHelpMenu()
  210. {
  211. iSetcolor(200, 191, 231);
  212. iFilledRectangle(70, hg - 25 - 60, 150, 55);
  213.  
  214. iSetcolor(233, 233, 233);
  215. iLine(70, hg - 25 - 60, 70, hg - 25 - 60 + 55);
  216. iLine(70 + 25, hg - 25 - 60, 70 + 25, hg - 25 - 60 + 55);
  217. iLine(70 + 150, hg - 25 - 60, 70 + 150, hg - 25 - 60 + 55);
  218. iLine(70, hg - 25 - 60 + 55, 70 + 150, hg - 25 - 60 + 55);
  219. iLine(70, hg - 25 - 60, 70 + 150, hg - 25 - 60);
  220. iLine(70 + 25, hg - 25 - 60 + 27.5, 70 + 150, hg - 25 - 60 + 27.5);
  221.  
  222. iSetcolor(0, 0, 0);
  223. iText(70 + 35, hg - 25 - 60 + 55 - 15, "About Game", GLUT_BITMAP_9_BY_15);
  224. iText(70 + 35, hg - 25 - 60 + 12, "View Help", GLUT_BITMAP_9_BY_15);
  225. }
  226.  
  227. void drawOptionMenu()
  228. {
  229. iSetcolor(200, 191, 231);
  230. iFilledRectangle(10, hg - 25 - 115, 150, 110);
  231.  
  232. iSetcolor(233, 233, 233);
  233. iLine(10, hg - 25 - 115, 10, hg - 25 - 115 + 110);
  234. iLine(10 + 25, hg - 25 - 115, 10 + 25, hg - 25 - 115 + 110);
  235. iLine(10 + 150, hg - 25 - 115, 10 + 150, hg - 25 - 115 + 110);
  236. iLine(10, hg - 25 - 115 + 110, 10 + 150, hg - 25 - 115 + 110);
  237. iLine(10, hg - 25 - 115, 10 + 150, hg - 25 - 115);
  238. iLine(10 + 25, hg - 25 - 115 + 27.5, 10 + 150, hg - 25 - 115 + 27.5);
  239. iLine(10 + 25, hg - 25 - 115 + ( 2 * 27.5), 10 + 150, hg - 25 - 115 + ( 2 * 27.5));
  240. iLine(10 + 25, hg - 25 - 115 + ( 3 * 27.5), 10 + 150, hg - 25 - 115 + ( 3 * 27.5));
  241.  
  242. iSetcolor(0, 0, 0);
  243. iText(10 + 35, hg - 25 - 60 + 12 + 27, "New Game", GLUT_BITMAP_9_BY_15);
  244. iText(10 + 35, hg - 25 - 60 + 12, "Statistics", GLUT_BITMAP_9_BY_15);
  245. iText(10 + 35, hg - 25 - 60 + 12 - 27.5, "Options", GLUT_BITMAP_9_BY_15);
  246. iText(10 + 35, hg - 25 - 60 + 12 - 54, "Exit", GLUT_BITMAP_9_BY_15);
  247. }
  248.  
  249. void drawEasyMaze()
  250. {
  251. int i =0;
  252. int j = 0;
  253.  
  254. //iShowBMP(100,38,"pic.bmp");
  255. iSetcolor(34, 177, 76);
  256. iFilledRectangle(25, 30, wd - 50, hg - 90);
  257.  
  258. while(i <= 17)
  259. {
  260. iSetcolor(0, 0, 0);
  261. iLine(25, 30 + ((480/16)*i), wd - 55 + 27.5, 30 + ((480/16)*i));
  262. i++;
  263. }
  264.  
  265. while(j <= 25)
  266. {
  267. iSetcolor(0, 0, 0);
  268. iLine(25 + ((720/24)*j), 30 , 25 + ((720/24)*j), hg - 90 + 30);
  269. j++;
  270. }
  271.  
  272. iSetcolor(99, 62, 41);
  273. iFilledRectangle((25 + (3 * 30)), (2 * 30), (8 * 30), (1 * 30));
  274. iFilledRectangle((25 + (5 * 30)), (8 * 30), (1 * 30), (6 * 30));
  275. iFilledRectangle((25 + (6 * 30)), (5 * 30), (10 * 30), (1 * 30));
  276. iFilledRectangle((25 + (3 * 30)), (16 * 30), (5 * 30), (1 * 30));
  277. iFilledRectangle((25 + (20 * 30)), (4 * 30), (1 * 30), (12 * 30));
  278. }
  279.  
  280.  
  281.  
  282. void drawDifficultyOptions()
  283. {
  284. iSetcolor(105, 114, 216);
  285. iFilledRectangle(200, 200, 300, 200);
  286. iSetcolor(17, 20, 64);
  287. iRectangle(200, 200, 300, 200);
  288. iSetcolor(200, 191, 231);
  289. iFilledRectangle(210, 210, 280, 160);
  290. iSetcolor(0, 0, 0);
  291. iRectangle(210, 210, 280, 160);
  292. iText(210, 380, "Options", GLUT_BITMAP_HELVETICA_18);
  293. iText(220, 350, "Difficulty lavels : ", GLUT_BITMAP_HELVETICA_18);
  294. iSetcolor(233, 233, 233);
  295. iLine(211, 340, 490, 340);
  296. iLine(211, 300, 490, 300);
  297. iLine(211, 250, 490, 250);
  298. iSetcolor(0, 0, 0);
  299. iFilledCircle(220, 320, 5);
  300. //iRectangle(230, 305, 80, 30);
  301. iText(230, 315, "Easy Maze", GLUT_BITMAP_9_BY_15);
  302. iFilledCircle(220, 270, 5);
  303. iText(230, 265, "Medium Maze", GLUT_BITMAP_9_BY_15);
  304. iFilledCircle(220, 230, 5);
  305. iText(230, 225, "Advanced Maze", GLUT_BITMAP_9_BY_15);
  306. }
  307.  
  308. void iDraw()
  309. {
  310. //place your drawing codes here
  311. //iClear();
  312. int snakePlace;
  313.  
  314. drawFirstPage();
  315. if(start == 1)
  316. {
  317. drawPlayGround();
  318. drawMenuBar();
  319. }
  320. if(gameOption == 1)
  321. {
  322. if(newGame == 1)
  323. {
  324. drawDifficultyOptions();
  325. }
  326. }
  327. if(easyMaze == 1)
  328. {
  329.  
  330. drawEasyMaze();
  331. drawFoodPlacing();
  332. drawMovingSnake();
  333. snakePlace = drawMovingSnake();
  334. if( (snakePlace == (25 + (10 * 30) + 15)) && (food[0]!= 1) )
  335. {
  336. foodCount++;
  337. sprintf(score, "%d", foodCount *5);
  338. food[0] = 1;
  339. }
  340. else if( snakePlace == (25 + (13 * 30) + 15) && (food[1]!= 1))
  341. {
  342. foodCount++;
  343. sprintf(score, "%d", foodCount *5);
  344. food[1] = 1;
  345. }
  346. if(snakePlace >= ((25 + (20 * 30))))
  347. {
  348. easyMaze = 0;
  349. finish = 1;
  350. }
  351. }
  352. if(foodFound == 1)
  353. {
  354. drawMovingSnake();
  355. }
  356. if(help == 1)
  357. {
  358. drawHelpMenu();
  359. }
  360. if(gameOption == 1)
  361. {
  362. drawOptionMenu();
  363. }
  364. if(finish == 1)
  365. {
  366. iText(300, 300, "GAME OVER!!!", GLUT_BITMAP_TIMES_ROMAN_24);
  367. iText(200, 260, "EXIT and RESTART THE GAME AGAIN", GLUT_BITMAP_TIMES_ROMAN_24);
  368. }
  369. }
  370.  
  371. /*
  372. function iMouseMove() is called when the user presses and drags the mouse.
  373. (mx, my) is the position where the mouse pointer is.
  374. */
  375. void iMouseMove(int mx, int my)
  376. {
  377. //place your codes here
  378. }
  379.  
  380. /*
  381. function iMouse() is called when the user presses/releases the mouse.
  382. (mx, my) is the position where the mouse pointer is.
  383. */
  384.  
  385.  
  386. void iMouse(int button, int state, int mx, int my)
  387. {
  388. if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  389. {
  390. drawMovingSnake();
  391. if((mx >= 600) && (mx <= (600 + 100)) && (my >= 150) && (my <= (150 + 20)))
  392. {
  393. start = 1;
  394. }
  395. if((mx >= 10) && (mx <= 60) && (my >= (hg - 25)) && (my <= (hg - 5)))
  396. {
  397. help = 0;
  398. gameOption = 1;
  399. }
  400. if((mx >= 70) && (mx <= 120) && (my >= (hg - 25)) && (my <= (hg - 5)))
  401. {
  402. gameOption = 0;
  403. help = 1;
  404. }
  405. if((mx >= 120) && (mx <= wd) && (my >= (hg - 25)) && (my <= (hg - 5)))
  406. {
  407. gameOption = 0;
  408. help = 0;
  409. }
  410. if((mx >= (10 + 25)) && (mx <= (10 + 150)) && (my >= (hg - 25 - 115 + ( 3 * 27.5))) && (my <= (hg - 25 - 115 + ( 4 * 27.5))))
  411. {
  412. newGame = 1;
  413. finish = 0;
  414. //easyMaze = 1;
  415. }
  416. if((mx >= 230) && (mx <= (230 + 80)) && (my >= 305) && (my <= (305 + 30)))
  417. {
  418. //finish = 0;
  419. easyMaze = 1;
  420. gameOption = 0;
  421. newGame = 0;
  422. finish = 0;
  423. }
  424. if((mx >= (10 + 25)) && (mx <= (10 + 150)) && (my >= (hg - 25 - 115 + ( 0 * 27.5))) && (my <= (hg - 25 - 115) + ( 1 * 27.5)))
  425. {
  426. printf("exit");
  427. exitOption = 1;
  428. exit(0);
  429. }
  430. }
  431. if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
  432. {
  433. //place your codes here
  434. }
  435. }
  436.  
  437. /*
  438. function iKeyboard() is called whenever the user hits a key in keyboard.
  439. key- holds the ASCII value of the key pressed.
  440. */
  441. void iKeyboard(unsigned char key)
  442. {
  443. if(key == 'q')
  444. {
  445. //do something with 'q'
  446. }
  447. //place your codes for other keys here
  448. }
  449.  
  450. /*
  451. function iSpecialKeyboard() is called whenver user hits special keys like-
  452. function keys, home, end, pg up, pg down, arraows etc. you have to use
  453. appropriate constants to detect them. A list is:
  454. GLUT_KEY_F1, GLUT_KEY_F2, GLUT_KEY_F3, GLUT_KEY_F4, GLUT_KEY_F5, GLUT_KEY_F6,
  455. GLUT_KEY_F7, GLUT_KEY_F8, GLUT_KEY_F9, GLUT_KEY_F10, GLUT_KEY_F11, GLUT_KEY_F12,
  456. GLUT_KEY_LEFT, GLUT_KEY_UP, GLUT_KEY_RIGHT, GLUT_KEY_DOWN, GLUT_KEY_PAGE UP,
  457. GLUT_KEY_PAGE DOWN, GLUT_KEY_HOME, GLUT_KEY_END, GLUT_KEY_INSERT
  458. */
  459. void iSpecialKeyboard(unsigned char key)
  460. {
  461.  
  462. if(key == GLUT_KEY_END)
  463. {
  464. exit(0);
  465. }
  466. if(key == GLUT_KEY_RIGHT)
  467. {
  468. count++;
  469. }
  470. //place your codes for other keys here
  471. }
  472.  
  473. int main()
  474. {
  475. //place your own initialization codes here.
  476. iInitialize(wd, hg, "Snake Game");
  477. return 0;
  478. }
Add Comment
Please, Sign In to add comment