Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.76 KB | None | 0 0
  1. # include "iGraphics.h"
  2. # include <stdio.h>
  3. # include <string.h>
  4. # include <time.h>
  5.  
  6. int f = 0, log1_y = 600, len=0, m3, mm3, stop_dec = 0, log2_y = 800, m1, m2, mm1, mm2, point = 0,x=3;
  7. int i, ans_f =0, p, c, s, log3_y = 1000, c3=0, lev=50, type = 0, life = 3;
  8. char n1[4], nn1[4], c1 = 0, c2 = 0, score[4],lives[3];
  9. char ch, in_ans[10], in_ans1[10], in_ans2[10], log1_math[4], log2_math[4],signs[4]={'+','-','x','/0'};
  10. char colour[5][50] = {"Red","Green", "Orange", "Blue", "Purple"};
  11. char shape[5][50] = {"Square", "Circle", "Triangle", "Diamond", "Pentagon"};
  12.  
  13.  
  14. void log1_y_dec(void);
  15. void log2_y_dec(void);
  16. void score_in(void);
  17. int intConvert(char in_ans[]);
  18.  
  19. void initialize(){
  20. f = 0;
  21. log1_y = 600, log2_y = 800, log3_y = 1000;
  22. point = 0;
  23. c1=0, c2=0, c3 = 0;
  24. stop_dec = 0;
  25. lev=50;
  26. x=3;
  27. life = 3;
  28. }
  29.  
  30.  
  31. int intConvert(char in_ans[])
  32. {
  33. /*this function converts a string of numbers to interger number. it's parameter is the string that takes input from the keyboard
  34. it will be cosidered the ans to the math expression.*/
  35. int i, ans, k=0;
  36. ans = in_ans[strlen(in_ans) - 1] - 48;
  37. if(in_ans[0]=='-')k=1;
  38. for (i = strlen(in_ans) - 2; i >= k; i--){
  39. ans = ans + (in_ans[i] - 48)*10;
  40. }
  41. if(k==0)return ans;
  42. else return ans*-1;
  43. }
  44.  
  45. /* void math_sum_exp2(void)
  46. {
  47. /*generates a mathematical expression with a plus sign.
  48. PROBLEM: once compiles it gives the same expresion until it is compiled again*/
  49. //int mm1, mm2;
  50. //srand(time(NULL));
  51. /*mm1 = rand() % 10;
  52. nn1[0] = 48 + mm1;
  53. //nn1[1] = '+';
  54. mm2 = rand() % 10;
  55. nn1[2] = 48 + mm2;
  56. nn1[3] = '=';
  57. //mm3 = mm1 + mm2;
  58. nn1[4] = '\0';
  59. int sign2 = rand() % 3;
  60. nn1[1] = signs[sign2];
  61.  
  62. if(sign2 == 0) mm3 = mm1 + mm2;
  63. else if (sign2 == 1) mm3 = mm1 - mm2;
  64. else mm3 = mm1 * mm2;
  65.  
  66. } */
  67.  
  68. int type_no()
  69. {
  70. type = rand()%3;
  71. return type;
  72. }
  73.  
  74. int colour_exp()
  75. {
  76. c = rand()%5;
  77. return c;
  78. }
  79.  
  80. int shape_exp()
  81. {
  82. s = rand()%5;
  83. return s;
  84. }
  85.  
  86. void math_sum_exp(void)
  87. {
  88. /*generates a mathematical expression with a plus sign.
  89. PROBLEM: once compiles it gives the same expresion until it is compiled again*/
  90. //int m1, m2;
  91. srand(time(NULL));
  92. m1 = rand() % 10;
  93. n1[0] = 48 + m1;
  94. //n1[1] = '+';
  95. m2 = rand() % 10;
  96. n1[2] = 48 + m2;
  97. n1[3] = '=';
  98. //m3 = m1 + m2;
  99. n1[4] = '\0';
  100.  
  101. int sign1 = rand() % 3;
  102. n1[1] = signs[sign1];
  103.  
  104. if(sign1 == 0) m3 = m1 + m2;
  105. else if (sign1 == 1) m3 = m1 - m2;
  106. else m3 = m1 * m2;
  107.  
  108. strcpy(log1_math,n1);
  109. }
  110.  
  111. void log1_y_dec(void)
  112. {
  113. /*decreases the logs y co-ordinate to lower it. if the log crashes with the boat y coordinate is set back to initial*/
  114. if (f == 1)
  115. {
  116. if (stop_dec==0){
  117. log1_y -= x;
  118. log2_y -= x;
  119. log3_y -= x;
  120. }
  121.  
  122. if (log1_y <= 128 || log2_y <= 128 || log3_y <= 128){
  123. life--;
  124. stop_dec=0;
  125. if(life==0)f = 4;///final screen flag
  126. }
  127. }
  128. }
  129.  
  130. void done()
  131. {
  132. log1_y += 600;
  133. c1 = 0;
  134. point += 5;
  135. }
  136. /*
  137. function iDraw() is called again and again by the system.
  138. */
  139. void iDraw()
  140. {
  141. iClear();
  142. if (f == 0)
  143. {
  144. ///home screen
  145. iShowBMP(0, 0, "home back.bmp");
  146. iSetColor(255, 0, 0);
  147. iRectangle(130, 300, 240, 67);///lower box
  148. iRectangle(130, 367, 240, 67);///middle box
  149. iRectangle(130, 434, 240, 67);///upper box
  150. iText(215, 460, "START", GLUT_BITMAP_TIMES_ROMAN_24);
  151. iText(165, 390, "INSTRUCTIONS", GLUT_BITMAP_TIMES_ROMAN_24);
  152. iText(220, 325, "QUIT", GLUT_BITMAP_TIMES_ROMAN_24);
  153. }
  154.  
  155. if (f == 1)
  156. {
  157. ///main game page
  158. iSetColor(47, 141, 255);
  159. iFilledRectangle(100, 0, 300, 600);
  160.  
  161. iSetColor(0, 0, 0);
  162. iText(290,60,"YOUR ANSWER:");
  163. //iText(320,40,in_ans, GLUT_BITMAP_TIMES_ROMAN_24);
  164.  
  165. //if(stop_dec==0){
  166. //if (intConvert(in_ans2) != m3){
  167. iShowBMP(100, log1_y, "log1.bmp");
  168. if (c1 == 0){
  169. math_sum_exp();
  170. //puts(n1);
  171. c1++;
  172. }
  173.  
  174. iSetColor(255, 255, 255);
  175. iText(240, log1_y + 25, log1_math, GLUT_BITMAP_9_BY_15);
  176.  
  177.  
  178. //}
  179.  
  180. iShowBMP(100,log2_y, "log2.bmp");
  181. if (c2 == 0){
  182. c = colour_exp();
  183. c2++;
  184. }
  185.  
  186. iText(240, log2_y + 25, colour[c], GLUT_BITMAP_9_BY_15);
  187.  
  188. iShowBMP(100, log3_y, "log1.bmp");
  189. if (c3 == 0){
  190. s = shape_exp();
  191. //puts(n1);
  192. c3++;
  193. }
  194.  
  195. iText(240, log3_y + 25, shape[s], GLUT_BITMAP_9_BY_15);
  196.  
  197. iSetColor(0, 0, 0);
  198. iText(320,40,in_ans, GLUT_BITMAP_TIMES_ROMAN_24);
  199.  
  200. if (intConvert(in_ans2) == m3)
  201. done();
  202.  
  203. //if (intConvert(in_ans2) == mm3)
  204. // stop_dec = 2;
  205. // this portion checks keyboard input string converted to an integer and compares it to the actual answer of the expression
  206. //if they match stops loading the log image
  207. // PROBLEM: Doesn't set the log back, doesn't work for continuos logs.
  208. iSetColor(0, 0, 0);
  209. iFilledRectangle(0, 600, 500, 50);
  210.  
  211. ///life
  212. iSetColor(255, 255, 255);
  213. iText(420, 620, "Life: ");
  214.  
  215. lives[0] = '0';
  216. lives[1] = life%10 + 48;
  217. lives[2] = '\0';
  218.  
  219. iText(480,620,lives);
  220.  
  221. ///SCORE
  222. iSetColor(255, 255, 255);
  223. iText(200, 620, "SCORE: ");
  224.  
  225. p = point;
  226. for(i=2;i>=0;i--){
  227. score[i] = p%10 + 48;
  228. p /= 10;
  229. }
  230. score[3]='\0';
  231.  
  232. iSetColor(255, 255, 255);
  233. iText(260,620,score);
  234.  
  235. // scores isn't complete
  236. iText(30, 550, "SHAPES");
  237. iText(430, 550, "COLORS");
  238.  
  239. iSetColor(255, 255, 255);
  240. iShowBMP(218, 0, "boat.bmp");
  241.  
  242. iLine(0, 600, 500, 600);///draws horizontal line
  243.  
  244. ///for shapes buttons
  245. iSetColor(255, 0, 0);///square
  246. iFilledRectangle(0, 0, 100, 100);
  247.  
  248. iSetColor(0, 255, 0);///circle
  249. iFilledRectangle(0, 100, 100, 100);
  250.  
  251. iSetColor(255, 173, 47);///triangle
  252. iFilledRectangle(0, 200, 100, 100);
  253.  
  254. iSetColor(0, 0, 255);///diamond
  255. iFilledRectangle(0, 300, 100, 100);
  256.  
  257. iSetColor(160, 32, 240);///pentagon
  258. iFilledRectangle(0, 400, 100, 100);
  259.  
  260. iSetColor(255, 255, 255);
  261. iFilledRectangle(30, 30, 40, 40);
  262. iFilledCircle(50, 150, 20);
  263. iFilledCircle(50, 250, 20, 3);
  264. iFilledEllipse(50, 350, 20, 30, 4);
  265. iFilledCircle(50, 450, 20, 5);
  266.  
  267. ///for color buttons
  268. iSetColor(255, 0, 0);///red
  269. iFilledRectangle(400, 0, 100, 100);
  270.  
  271. iSetColor(0, 255, 0);///green
  272. iFilledRectangle(400, 100, 100, 100);
  273.  
  274. iSetColor(255, 173, 47);///orange
  275. iFilledRectangle(400, 200, 100, 100);
  276.  
  277. iSetColor(0, 0, 255);///blue
  278. iFilledRectangle(400, 300, 100, 100);
  279.  
  280. iSetColor(160, 32, 240);///purple
  281. iFilledRectangle(400, 400, 100, 100);
  282.  
  283. }
  284. if (f == 2)
  285. {
  286. ///instruction page
  287. iShowBMP(0, 0, "ins.bmp");
  288. iSetColor(255, 255, 200);
  289. iText(10, 10, "GO BACK", GLUT_BITMAP_TIMES_ROMAN_24);
  290.  
  291. }
  292. if (f == 3)
  293. {
  294. ///quit button
  295. exit(1);
  296. }
  297. if (f == 4)
  298. {
  299. ///end screen
  300. iText(180, 500, "GAME OVER", GLUT_BITMAP_TIMES_ROMAN_24);
  301. iText(180,400, "YOUR SCORE: ", GLUT_BITMAP_TIMES_ROMAN_24);
  302. iText(220, 380, score, GLUT_BITMAP_TIMES_ROMAN_24);
  303. iText(10, 10, "RETURN HOME", GLUT_BITMAP_9_BY_15);
  304. }
  305.  
  306.  
  307. }
  308. /*
  309. function iMouseMove() is called when the user presses and drags the mouse.
  310. (mx, my) is the position where the mouse pointer is.
  311. */
  312. void iMouseMove(int mx, int my)
  313. {
  314. //place your codes here
  315. }
  316. /*
  317. function iMouse() is called when the user presses/releases the mouse.
  318. (mx, my) is the position where the mouse pointer is.
  319. */
  320. void iMouse(int button, int state, int mx, int my)
  321. {
  322. if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  323. {
  324. //place your codes here
  325. if (mx>130 && my>434 && mx <370 && my<501 && f == 0)
  326. {
  327. f = 1;
  328. }
  329. if (mx>130 && my>367 && mx <370 && my<433 && f == 0)
  330. {
  331. f = 2;
  332. }
  333. if (mx>130 && my>300 && mx <370 && my<366 && f == 0)
  334. {
  335. f = 3;
  336. }
  337. if (mx>10 && my>10 && mx <100 && my<50 && f == 2)
  338. {
  339. f = 0;
  340. }
  341. if (mx>10 && my>10 && mx < 100 && my < 50 && f == 4){
  342. initialize();
  343. }
  344.  
  345. ///colour answer
  346. if(log2_y < 600 && log2_y >= 128)
  347. {
  348. ///red
  349. if(c==0 && mx > 400 && mx < 500 && my > 0 && my < 100)
  350. {
  351. log2_y += 600;
  352. c2 = 0;
  353. point += 5;
  354. }
  355.  
  356. ///green
  357. if(c==1 && mx > 400 && mx < 500 && my > 100 && my < 200)
  358. {
  359. log2_y += 600;
  360. c2 = 0;
  361. point += 5;
  362. }
  363.  
  364. ///orange
  365. if(c==2 && mx > 400 && mx < 500 && my > 200 && my < 300)
  366. {
  367. log2_y += 600;
  368. c2 = 0;
  369. point += 5;
  370. }
  371.  
  372. ///blue
  373. if(c==3 && mx > 400 && mx < 500 && my > 300 && my < 400)
  374. {
  375. log2_y += 600;
  376. c2 = 0;
  377. point += 5;
  378. }
  379.  
  380. ///purple
  381. if(c==4 && mx > 400 && mx < 500 && my > 400 && my < 500)
  382. {
  383. log2_y += 600;
  384. c2 = 0;
  385. point += 5;
  386. }
  387.  
  388. }
  389.  
  390. ///shape answer
  391. if(log3_y < 600 && log3_y >= 128)
  392. {
  393. ///square
  394. if(s==0 && mx > 0 && mx < 100 && my > 0 && my < 100)
  395. {
  396. log3_y += 600;
  397. c3 = 0;
  398. point += 5;
  399. }
  400.  
  401. ///circle
  402. if(s==1 && mx > 0 && mx < 100 && my > 100 && my < 200)
  403. {
  404. log3_y += 600;
  405. c3 = 0;
  406. point += 5;
  407. }
  408.  
  409. ///Triangle
  410. if(s==2 && mx > 0 && mx < 100 && my > 200 && my < 300)
  411. {
  412. log3_y += 600;
  413. c3 = 0;
  414. point += 5;
  415. }
  416.  
  417. ///diamond
  418. if(s==3 && mx > 00 && mx < 100 && my > 300 && my < 400)
  419. {
  420. log3_y += 600;
  421. c3 = 0;
  422. point += 5;
  423. }
  424.  
  425. ///pentagon
  426. if(s==4 && mx > 00 && mx < 100 && my > 400 && my < 500)
  427. {
  428. log3_y += 600;
  429. c3 = 0;
  430. point += 5;
  431. }
  432.  
  433. }
  434.  
  435. }
  436. if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
  437. {
  438. //place your codes here
  439. }
  440. }
  441. /*
  442. function iKeyboard() is called whenever the user hits a key in keyboard.
  443. key- holds the ASCII value of the key pressed.
  444. */
  445. void iKeyboard(unsigned char key)
  446. {
  447. /// takes input from keyboard to a string
  448. int i;
  449. if (f == 1){
  450. if(key == '\r')
  451. {
  452. //it'll set the string to zero
  453.  
  454. in_ans[len] = '\0';
  455.  
  456. strcpy(in_ans2,in_ans);
  457. for(i=0;i<len;i++)
  458. in_ans[i] = 0;
  459.  
  460. len = 0;
  461. }
  462. else
  463. {
  464. in_ans[len] = key;
  465. //puts(in_ans);
  466. len++;
  467. }
  468.  
  469. /*if(pause == 0 && key == ' '){
  470. pause = 1;
  471. }
  472. if(pause == 1 && key == ' '){
  473. pause = 0;
  474. }*/
  475. }
  476.  
  477. //place your codes for other keys here
  478. }
  479. /*
  480. function iSpecialKeyboard() is called whenver user hits special keys likefunction
  481. keys, home, end, pg up, pg down, arraows etc. you have to use
  482. appropriate constants to detect them. A list is:
  483. GLUT_KEY_F1, GLUT_KEY_F2, GLUT_KEY_F3, GLUT_KEY_F4, GLUT_KEY_F5, GLUT_KEY_F6,
  484. GLUT_KEY_F7, GLUT_KEY_F8, GLUT_KEY_F9, GLUT_KEY_F10, GLUT_KEY_F11,
  485. GLUT_KEY_F12, GLUT_KEY_LEFT, GLUT_KEY_UP, GLUT_KEY_RIGHT, GLUT_KEY_DOWN,
  486. GLUT_KEY_PAGE UP, GLUT_KEY_PAGE DOWN, GLUT_KEY_HOME, GLUT_KEY_END,
  487. GLUT_KEY_INSERT */
  488. void iSpecialKeyboard(unsigned char key)
  489. {
  490. if (key == GLUT_KEY_END)
  491. {
  492. exit(0);
  493. }
  494. //place your codes for other keys here
  495. }
  496.  
  497. void increase()
  498. {
  499. x=x+1;
  500.  
  501. }
  502.  
  503. int main()
  504. {
  505. //place your own initialization codes here.
  506. iSetTimer(10000,increase);
  507. iSetTimer(lev, log1_y_dec);
  508.  
  509. iInitialize(500, 650, "RIVER LEARNING");
  510. return 0;
  511. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement