Advertisement
Guest User

Untitled

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