Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include "iGraphics.h"
- # include <stdio.h>
- # include <string.h>
- # include <time.h>
- int f = 0, log1_y = 600, log1_f = 1, len=0, m3, mm3, stop_dec = 0, log2_y = 600+200, m1, m2, mm1, mm2;
- char n1[4], nn1[4], score = 0, c1 = 0, c2 = 0;
- char ch, in_ans[10], in_ans2[10], log1_math[4], log2_math[4];
- char signs[4]={'+','-','*','/0'};
- void log1_y_dec(void);
- void log2_y_dec(void);
- void score_in(void);
- int intConvert(char in_ans[]);
- int intConvert(char in_ans[])
- {
- /*this function converts a string of numbers to interger number. it's parameter is the string that takes input from the keyboard
- it will be cosidered the ans to the math expression.
- PROBLEM: it works even if the string is not null terminated*/
- int i, ans, k=0;
- ans = in_ans[strlen(in_ans) - 1] - 48;
- if(in_ans[0]=='-') k=1;
- for (i = strlen(in_ans) - 2; i >= k; i--){
- ans = ans + (in_ans[i] - 48)*10;
- }
- if (k==0)return ans;
- else return ans*-1;
- }
- void math_sum_exp2(void)
- {
- /*generates a mathematical expression with a plus sign.
- PROBLEM: once compiles it gives the same expresion until it is compiled again*/
- //int mm1, mm2;
- //srand(time(NULL));
- mm1 = rand() % 10;
- nn1[0] = 48 + mm1;
- int sign2 = rand() % 3;
- nn1[1] = signs[sign2];
- mm2 = rand() % 10;
- nn1[2] = 48 + mm2;
- nn1[3] = '=';
- if(sign2 == 0) mm3 = mm1 + mm2;
- else if (sign2 == 1) mm3 = mm1 - mm2;
- else mm3 = mm1 * mm2;
- nn1[4] = '\0';
- }
- void math_sum_exp(void)
- {
- /*generates a mathematical expression with a plus sign.
- PROBLEM: once compiles it gives the same expresion until it is compiled again*/
- //int m1, m2;
- srand(time(NULL));
- m1 = rand() % 10;
- n1[0] = 48 + m1;
- int sign1 = rand() % 3;
- n1[1] = signs[sign1];
- m2 = rand() % 10;
- n1[2] = 48 + m2;
- n1[3] = '=';
- if(sign1 == 0) m3 = m1 + m2;
- else if (sign1 == 1) m3 = m1 - m2;
- else m3 = m1 * m2;
- n1[4] = '\0';
- strcpy(log1_math,n1);
- }
- void log1_y_dec(void)
- {
- /*decreases the logs y co-ordinate to lower it. if the log crashes with the boat y coordinate is set back to initial*/
- if (f == 1)
- {
- if (stop_dec==0){
- log1_y -= 3;
- log2_y -= 3;
- }
- if (stop_dec==1){
- log1_y = log2_y + 200;
- if(log1_y<600) log1_y = 600;
- c1=0;
- stop_dec=0;
- }
- if (stop_dec==2){
- log2_y = log1_y + 200;
- if(log2_y<600) log2_y = 600;
- c2=0;
- stop_dec=0;
- }
- if (log1_y <= 128 || log2_y <= 128){
- f = 4;///final screen flag
- }
- }
- }
- /*
- function iDraw() is called again and again by the system.
- */
- void iDraw()
- {
- iClear();
- if (f == 0)
- {
- ///home screen
- iShowBMP(0, 0, "home back.bmp");
- iSetColor(255, 0, 0);
- iRectangle(130, 300, 240, 67);///lower box
- iRectangle(130, 367, 240, 67);///middle box
- iRectangle(130, 434, 240, 67);///upper box
- iText(215, 460, "START", GLUT_BITMAP_TIMES_ROMAN_24);
- iText(165, 390, "INSTRUCTIONS", GLUT_BITMAP_TIMES_ROMAN_24);
- iText(220, 325, "QUIT", GLUT_BITMAP_TIMES_ROMAN_24);
- }
- if (f == 1)
- {
- ///main game page
- iSetColor(47, 141, 255);
- iFilledRectangle(100, 0, 300, 600);
- iSetColor(0, 0, 0);
- iText(290,60,"YOUR ANSWER:");
- //iText(320,40,in_ans, GLUT_BITMAP_TIMES_ROMAN_24);
- //if(stop_dec==0){
- //if (intConvert(in_ans2) != m3){
- iShowBMP(100, log1_y, "log1.bmp");
- if (c1 == 0){
- math_sum_exp();
- puts(n1);
- c1++;
- }
- iSetColor(255, 255, 255);
- iText(240, log1_y + 25, log1_math, GLUT_BITMAP_9_BY_15);
- //}
- iShowBMP(100,log2_y, "log2.bmp");
- if (c2 == 0){
- math_sum_exp2();
- puts(nn1);
- c2++;
- }
- iText(240, log2_y + 25, nn1, GLUT_BITMAP_9_BY_15);
- iSetColor(0, 0, 0);
- iText(320,40,in_ans, GLUT_BITMAP_TIMES_ROMAN_24);
- if (intConvert(in_ans2) == m3){
- stop_dec = 1;
- puts(nn1);
- }
- if (intConvert(in_ans2) == mm3)
- stop_dec = 2;
- // this portion checks keyboard input string converted to an integer and compares it to the actual answer of the expression
- //if they match stops loading the log image
- // PROBLEM: Doesn't set the log back, doesn't work for continuos logs.
- iSetColor(0, 0, 0);
- iFilledRectangle(0, 600, 500, 50);
- iSetColor(255, 255, 255);
- iText(200, 620, "SCORE: ");
- /// scores isn't complete
- iText(30, 550, "SHAPES");
- iText(430, 550, "COLORS");
- iSetColor(255, 255, 255);
- iShowBMP(218, 0, "boat.bmp");
- iLine(0, 600, 500, 600);///draws horizontal line
- ///for shapes buttons
- iSetColor(255, 0, 0);
- iFilledRectangle(0, 0, 100, 100);
- iSetColor(0, 255, 0);
- iFilledRectangle(0, 100, 100, 100);
- iSetColor(255, 173, 47);
- iFilledRectangle(0, 200, 100, 100);
- iSetColor(0, 0, 255);
- iFilledRectangle(0, 300, 100, 100);
- iSetColor(160, 32, 240);
- iFilledRectangle(0, 400, 100, 100);
- iSetColor(255, 255, 255);
- iFilledRectangle(30, 30, 40, 40);
- iFilledCircle(50, 150, 20);
- iFilledCircle(50, 250, 20, 3);
- iFilledEllipse(50, 350, 20, 30, 4);
- iFilledCircle(50, 450, 20, 5);
- ///for color buttons
- iSetColor(255, 0, 0);///red
- iFilledRectangle(400, 0, 100, 100);
- iSetColor(0, 255, 0);///green
- iFilledRectangle(400, 100, 100, 100);
- iSetColor(255, 173, 47);///orange
- iFilledRectangle(400, 200, 100, 100);
- iSetColor(0, 0, 255);///blue
- iFilledRectangle(400, 300, 100, 100);
- iSetColor(160, 32, 240);///purple
- iFilledRectangle(400, 400, 100, 100);
- }
- if (f == 2)
- {
- ///instruction page
- iShowBMP(0, 0, "ins.bmp");
- iSetColor(255, 255, 200);
- iText(10, 10, "GO BACK", GLUT_BITMAP_TIMES_ROMAN_24);
- }
- if (f == 3)
- {
- ///quit button
- exit(1);
- }
- if (f == 4)
- {
- ///end screen
- iText(180, 500, "GAME OVER", GLUT_BITMAP_TIMES_ROMAN_24);
- iText(10, 10, "RETURN HOME", GLUT_BITMAP_9_BY_15);
- }
- }
- /*
- function iMouseMove() is called when the user presses and drags the mouse.
- (mx, my) is the position where the mouse pointer is.
- */
- void iMouseMove(int mx, int my)
- {
- //place your codes here
- }
- /*
- function iMouse() is called when the user presses/releases the mouse.
- (mx, my) is the position where the mouse pointer is.
- */
- void iMouse(int button, int state, int mx, int my)
- {
- if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
- {
- //place your codes here
- if (mx>130 && my>434 && mx <370 && my<501 && f == 0)
- {
- f = 1;
- }
- if (mx>130 && my>367 && mx <370 && my<433 && f == 0)
- {
- f = 2;
- }
- if (mx>130 && my>300 && mx <370 && my<366 && f == 0)
- {
- f = 3;
- }
- if (mx>10 && my>10 && mx <100 && my<50 && f == 2)
- {
- f = 0;
- }
- if (mx>10 && my>10 && mx < 100 && my < 50 && f == 4){
- f = 0;
- log1_y = 600, log2_y = 800;
- }
- }
- if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
- {
- //place your codes here
- }
- }
- /*
- function iKeyboard() is called whenever the user hits a key in keyboard.
- key- holds the ASCII value of the key pressed.
- */
- void iKeyboard(unsigned char key)
- {
- /// takes input from keyboard to a string
- int i;
- if (f == 1){
- if(key == '\r')
- {
- //it'll set the string to zero
- // PROBLEM: log disappears before enter is pressed
- //for (i = 0; i < len; i++)
- in_ans[len] = '\0';
- strcpy(in_ans2,in_ans);
- for(i=0;i<len;i++)in_ans[i] = 0;
- len = 0;
- }
- else
- {
- in_ans[len] = key;
- puts(in_ans);
- len++;
- }
- }
- //place your codes for other keys here
- }
- /*
- function iSpecialKeyboard() is called whenver user hits special keys likefunction
- keys, home, end, pg up, pg down, arraows etc. you have to use
- appropriate constants to detect them. A list is:
- GLUT_KEY_F1, GLUT_KEY_F2, GLUT_KEY_F3, GLUT_KEY_F4, GLUT_KEY_F5, GLUT_KEY_F6,
- GLUT_KEY_F7, GLUT_KEY_F8, GLUT_KEY_F9, GLUT_KEY_F10, GLUT_KEY_F11,
- GLUT_KEY_F12, GLUT_KEY_LEFT, GLUT_KEY_UP, GLUT_KEY_RIGHT, GLUT_KEY_DOWN,
- GLUT_KEY_PAGE UP, GLUT_KEY_PAGE DOWN, GLUT_KEY_HOME, GLUT_KEY_END,
- GLUT_KEY_INSERT */
- void iSpecialKeyboard(unsigned char key)
- {
- if (key == GLUT_KEY_END)
- {
- exit(0);
- }
- //place your codes for other keys here
- }
- int main()
- {
- //place your own initialization codes here.
- iSetTimer(50, log1_y_dec);
- iInitialize(500, 650, "RIVER LEARNING");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement