Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.85 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, log1_f = 1, len=0, m3, mm3, stop_dec = 0, log2_y = 600+200, m1, m2, mm1, mm2;
  7. char n1[4], nn1[4], score = 0, c1 = 0, c2 = 0;
  8. char ch, in_ans[10], in_ans2[10], log1_math[4], log2_math[4];
  9.  
  10.  
  11. void log1_y_dec(void);
  12. void log2_y_dec(void);
  13. void score_in(void);
  14. int intConvert(char in_ans[]);
  15.  
  16. int intConvert(char in_ans[])
  17. {
  18. /*this function converts a string of numbers to interger number. it's parameter is the string that takes input from the keyboard
  19. it will be cosidered the ans to the math expression.
  20. PROBLEM: it works even if the string is not null terminated*/
  21. int i, ans;
  22. ans = in_ans[strlen(in_ans) - 1] - 48;
  23. for (i = strlen(in_ans) - 2; i >= 0; i--){
  24. ans = ans + (in_ans[i] - 48)*10;
  25. }
  26. return ans;
  27. }
  28.  
  29. void math_sum_exp2(void)
  30. {
  31. /*generates a mathematical expression with a plus sign.
  32. PROBLEM: once compiles it gives the same expresion until it is compiled again*/
  33. //int mm1, mm2;
  34. //srand(time(NULL));
  35. mm1 = rand() % 10;
  36. nn1[0] = 48 + mm1;
  37. nn1[1] = '+';
  38. mm2 = rand() % 10;
  39. nn1[2] = 48 + mm2;
  40. nn1[3] = '=';
  41. mm3 = mm1 + mm2;
  42. nn1[4] = '\0';
  43.  
  44. }
  45.  
  46. void math_sum_exp(void)
  47. {
  48. /*generates a mathematical expression with a plus sign.
  49. PROBLEM: once compiles it gives the same expresion until it is compiled again*/
  50. //int m1, m2;
  51. srand(time(NULL));
  52. m1 = rand() % 10;
  53. n1[0] = 48 + m1;
  54. n1[1] = '+';
  55. m2 = rand() % 10;
  56. n1[2] = 48 + m2;
  57. n1[3] = '=';
  58. m3 = m1 + m2;
  59. n1[4] = '\0';
  60. strcpy(log1_math,n1);
  61. }
  62.  
  63. void log1_y_dec(void)
  64. {
  65. /*decreases the logs y co-ordinate to lower it. if the log crashes with the boat y coordinate is set back to initial*/
  66. if (f == 1)
  67. {
  68. if (stop_dec==0){
  69. log1_y -= 3;
  70. log2_y -= 3;
  71. }
  72. if (stop_dec==1){
  73. log1_y = log2_y + 200;
  74. if(log1_y<600) log1_y = 600;
  75. c1=0;
  76. stop_dec=0;
  77. }
  78. if (stop_dec==2){
  79. log2_y = log1_y + 200;
  80. if(log2_y<600) log2_y = 600;
  81. c2=0;
  82. stop_dec=0;
  83. }
  84. if (log1_y <= 128 || log2_y <= 128){
  85. f = 4;///final screen flag
  86. }
  87. }
  88. }
  89. /*
  90. function iDraw() is called again and again by the system.
  91. */
  92. void iDraw()
  93. {
  94. iClear();
  95. if (f == 0)
  96. {
  97. ///home screen
  98. iShowBMP(0, 0, "home back.bmp");
  99. iSetColor(255, 0, 0);
  100. iRectangle(130, 300, 240, 67);///lower box
  101. iRectangle(130, 367, 240, 67);///middle box
  102. iRectangle(130, 434, 240, 67);///upper box
  103. iText(215, 460, "START", GLUT_BITMAP_TIMES_ROMAN_24);
  104. iText(165, 390, "INSTRUCTIONS", GLUT_BITMAP_TIMES_ROMAN_24);
  105. iText(220, 325, "QUIT", GLUT_BITMAP_TIMES_ROMAN_24);
  106. }
  107.  
  108. if (f == 1)
  109. {
  110. ///main game page
  111. iSetColor(47, 141, 255);
  112. iFilledRectangle(100, 0, 300, 600);
  113.  
  114. iSetColor(0, 0, 0);
  115. iText(290,60,"YOUR ANSWER:");
  116. //iText(320,40,in_ans, GLUT_BITMAP_TIMES_ROMAN_24);
  117.  
  118. //if(stop_dec==0){
  119. //if (intConvert(in_ans2) != m3){
  120. iShowBMP(100, log1_y, "log1.bmp");
  121. if (c1 == 0){
  122. math_sum_exp();
  123. puts(n1);
  124. c1++;
  125. }
  126.  
  127. iSetColor(255, 255, 255);
  128. iText(240, log1_y + 25, log1_math, GLUT_BITMAP_9_BY_15);
  129.  
  130.  
  131. //}
  132.  
  133. iShowBMP(100,log2_y, "log2.bmp");
  134. if (c2 == 0){
  135. math_sum_exp2();
  136. puts(nn1);
  137. c2++;
  138. }
  139.  
  140. iText(240, log2_y + 25, "I am good", GLUT_BITMAP_9_BY_15);
  141.  
  142. iSetColor(0, 0, 0);
  143. iText(320,40,in_ans, GLUT_BITMAP_TIMES_ROMAN_24);
  144.  
  145. if (intConvert(in_ans2) == m3){
  146. stop_dec = 1;
  147. puts(nn1);
  148. }
  149.  
  150. if (intConvert(in_ans2) == mm3)
  151. stop_dec = 2;
  152. // this portion checks keyboard input string converted to an integer and compares it to the actual answer of the expression
  153. //if they match stops loading the log image
  154. // PROBLEM: Doesn't set the log back, doesn't work for continuos logs.
  155.  
  156. iSetColor(0, 0, 0);
  157. iFilledRectangle(0, 600, 500, 50);
  158. iSetColor(255, 255, 255);
  159. iText(200, 620, "SCORE: ");
  160. /// scores isn't complete
  161. iText(30, 550, "SHAPES");
  162. iText(430, 550, "COLORS");
  163.  
  164. iSetColor(255, 255, 255);
  165. iShowBMP(218, 0, "boat.bmp");
  166.  
  167. iLine(0, 600, 500, 600);///draws horizontal line
  168.  
  169. ///for shapes buttons
  170. iSetColor(255, 0, 0);
  171. iFilledRectangle(0, 0, 100, 100);
  172.  
  173. iSetColor(0, 255, 0);
  174. iFilledRectangle(0, 100, 100, 100);
  175.  
  176. iSetColor(255, 173, 47);
  177. iFilledRectangle(0, 200, 100, 100);
  178.  
  179. iSetColor(0, 0, 255);
  180. iFilledRectangle(0, 300, 100, 100);
  181.  
  182. iSetColor(160, 32, 240);
  183. iFilledRectangle(0, 400, 100, 100);
  184.  
  185. iSetColor(255, 255, 255);
  186. iFilledRectangle(30, 30, 40, 40);
  187. iFilledCircle(50, 150, 20);
  188. iFilledCircle(50, 250, 20, 3);
  189. iFilledEllipse(50, 350, 20, 30, 4);
  190. iFilledCircle(50, 450, 20, 5);
  191.  
  192. ///for color buttons
  193. iSetColor(255, 0, 0);///red
  194. iFilledRectangle(400, 0, 100, 100);
  195.  
  196. iSetColor(0, 255, 0);///green
  197. iFilledRectangle(400, 100, 100, 100);
  198.  
  199. iSetColor(255, 173, 47);///orange
  200. iFilledRectangle(400, 200, 100, 100);
  201.  
  202. iSetColor(0, 0, 255);///blue
  203. iFilledRectangle(400, 300, 100, 100);
  204.  
  205. iSetColor(160, 32, 240);///purple
  206. iFilledRectangle(400, 400, 100, 100);
  207. }
  208. if (f == 2)
  209. {
  210. ///instruction page
  211. iShowBMP(0, 0, "ins.bmp");
  212. iSetColor(255, 255, 200);
  213. iText(10, 10, "GO BACK", GLUT_BITMAP_TIMES_ROMAN_24);
  214.  
  215. }
  216. if (f == 3)
  217. {
  218. ///quit button
  219. exit(1);
  220. }
  221. if (f == 4)
  222. {
  223. ///end screen
  224. iText(180, 500, "GAME OVER", GLUT_BITMAP_TIMES_ROMAN_24);
  225. iText(10, 10, "RETURN HOME", GLUT_BITMAP_9_BY_15);
  226. }
  227.  
  228.  
  229. }
  230. /*
  231. function iMouseMove() is called when the user presses and drags the mouse.
  232. (mx, my) is the position where the mouse pointer is.
  233. */
  234. void iMouseMove(int mx, int my)
  235. {
  236. //place your codes here
  237. }
  238. /*
  239. function iMouse() is called when the user presses/releases the mouse.
  240. (mx, my) is the position where the mouse pointer is.
  241. */
  242. void iMouse(int button, int state, int mx, int my)
  243. {
  244. if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  245. {
  246. //place your codes here
  247. if (mx>130 && my>434 && mx <370 && my<501 && f == 0)
  248. {
  249. f = 1;
  250. }
  251. if (mx>130 && my>367 && mx <370 && my<433 && f == 0)
  252. {
  253. f = 2;
  254. }
  255. if (mx>130 && my>300 && mx <370 && my<366 && f == 0)
  256. {
  257. f = 3;
  258. }
  259. if (mx>10 && my>10 && mx <100 && my<50 && f == 2)
  260. {
  261. f = 0;
  262. }
  263. if (mx>10 && my>10 && mx < 100 && my < 50 && f == 4){
  264. f = 0;
  265. log1_y = 600, log2_y = 800;
  266. }
  267. }
  268. if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
  269. {
  270. //place your codes here
  271. }
  272. }
  273. /*
  274. function iKeyboard() is called whenever the user hits a key in keyboard.
  275. key- holds the ASCII value of the key pressed.
  276. */
  277. void iKeyboard(unsigned char key)
  278. {
  279. /// takes input from keyboard to a string
  280. int i;
  281. if (f == 1){
  282. if(key == '\r')
  283. {
  284. //it'll set the string to zero
  285. // PROBLEM: log disappears before enter is pressed
  286.  
  287. //for (i = 0; i < len; i++)
  288. in_ans[len] = '\0';
  289. strcpy(in_ans2,in_ans);
  290.  
  291. for(i=0;i<len;i++)in_ans[i] = 0;
  292.  
  293. len = 0;
  294. }
  295. else
  296. {
  297. in_ans[len] = key;
  298. puts(in_ans);
  299. len++;
  300. }
  301. }
  302. //place your codes for other keys here
  303. }
  304. /*
  305. function iSpecialKeyboard() is called whenver user hits special keys likefunction
  306. keys, home, end, pg up, pg down, arraows etc. you have to use
  307. appropriate constants to detect them. A list is:
  308. GLUT_KEY_F1, GLUT_KEY_F2, GLUT_KEY_F3, GLUT_KEY_F4, GLUT_KEY_F5, GLUT_KEY_F6,
  309. GLUT_KEY_F7, GLUT_KEY_F8, GLUT_KEY_F9, GLUT_KEY_F10, GLUT_KEY_F11,
  310. GLUT_KEY_F12, GLUT_KEY_LEFT, GLUT_KEY_UP, GLUT_KEY_RIGHT, GLUT_KEY_DOWN,
  311. GLUT_KEY_PAGE UP, GLUT_KEY_PAGE DOWN, GLUT_KEY_HOME, GLUT_KEY_END,
  312. GLUT_KEY_INSERT */
  313. void iSpecialKeyboard(unsigned char key)
  314. {
  315. if (key == GLUT_KEY_END)
  316. {
  317. exit(0);
  318. }
  319. //place your codes for other keys here
  320. }
  321. int main()
  322. {
  323. //place your own initialization codes here.
  324. iSetTimer(50, log1_y_dec);
  325. iInitialize(500, 650, "RIVER LEARNING");
  326. return 0;
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement