Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* author: S. M. Shahriar Nirjon
- last modified: August 8, 2008
- */
- # include "iGraphics.h"
- # include "gl.h"
- #include<windows.h>
- #define BALL_R 15
- #define BALL_MOVEMENT 20
- #define STICK_R 46//from the bmp pixel size
- int g_ball_x=900, g_ball_y=450;//start from the center
- int dx, dy;
- int g_stick1_x=100+200,g_stick1_y=500,g_stick2_x=1700-200,g_stick2_y=500;
- int draw_text=0;
- int page_draw=0;//0 means start page, 1 means star play and 2 means settings
- int draw_arrow=0;
- void start_page()
- {
- iSetColor(255,0,0);
- iText(10,10,"Press Right arrow on your selected choice");
- iShowBMP(600,250,"start_page.bmp");//(1800/2)-pixel_size_x(600)/2; 1000/2-pixel_size_y(500)/2;
- if(draw_arrow==0)iShowBMP(600+210,250+110,"arrow.bmp");
- if(draw_arrow==1)iShowBMP(600+185,250+50,"arrow.bmp");
- }
- void is_goal()
- {
- if(g_ball_y>450 && g_ball_y<550 && g_ball_x-BALL_R-100<10)//should be in between 450 and 550
- draw_text=1;
- if(g_ball_y<550 && g_ball_y>450 && 1700-g_ball_x-BALL_R<10)//should be in between 450 and 550
- draw_text=1;
- else draw_text=0;
- }
- void ball_hit()
- {
- dx=-dx;
- dy=-dy;
- // PlaySound((LPCSTR) "C:\\Users\\Teertho\\Desktop\\iGraphics Project\\iGraphics\\Hit.mp3", NULL, SND_FILENAME | SND_ASYNC);
- }
- void draw_field()
- {
- iSetColor(255,255,255);
- //iRectangle(100,100,1600,800);
- iLine(100,450,100,100);
- iLine(100,550,100,900);
- iLine(100,100,1700,100);
- iLine(100,900,1700,900);
- iLine(1700,100,1700,450);
- iLine(1700,550,1700,900);
- //field stretches from point 100 to 1700 in x co-ordinate. and 100 to 900 in y co-ordinate
- iLine(900,900,900,100);//draws the middle line
- iCircle(900,500,100);//Draws the middle circle
- iSetColor(125,125,0);//Now comes the goal post
- iLine(1700,450,1700,550);
- iLine(100,450,100,550);
- }
- void ball()
- {
- iSetColor(255,0,0);
- iFilledCircle(g_ball_x,g_ball_y,BALL_R);
- if((g_stick1_x-g_ball_x)*(g_stick1_x-g_ball_x)+(g_stick1_y-g_ball_y)*(g_stick1_y-g_ball_y)<=(STICK_R+BALL_R)*(STICK_R+BALL_R))
- {
- ball_hit();
- }
- }
- void draw_stick()
- {
- iShowBMP(g_stick1_x-46,g_stick1_y-46,"stick1.bmp");
- iShowBMP(g_stick2_x-46,g_stick2_y-46,"stick2.bmp");
- //-46 was included as I recently had learnt that images are rendered to the x,y coordinate from the left-bottom corner. so it will now always render at the center of the coordinate
- }
- void draw_goal()
- {
- iSetColor(255,255,255);
- iText(800,500,"Goal Scored!!!!!");
- }
- void iDraw()
- {
- iClear();
- if(page_draw==0)start_page();
- else
- {
- if(draw_text==0) is_goal();
- if(draw_text==1) iShowBMP(0,0,"goal.bmp");
- else
- {
- //PlaySound("C:\Users\Teertho\Desktop\iGraphics Project\iGraphics\Hit.mp3", NULL, SND_FILENAME | SND_ASYNC);
- draw_field();
- draw_stick();
- ball();
- }
- }
- }
- /*
- 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
- }
- void iMouse(int button, int state, int mx, int my)
- {
- if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN);
- if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN);
- }
- /*
- 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)
- {
- if(key == 'p')
- {
- //do something with 'q'
- iPauseTimer(0);
- }
- if(key == 'r')
- {
- iResumeTimer(0);
- }
- if(key=='w' && g_stick1_y<900-STICK_R-15){g_stick1_y+=BALL_MOVEMENT;}//15 was added or substracted to avoid the ball crossing the field
- if(key=='s' && g_stick1_y>100+STICK_R+15){g_stick1_y-=BALL_MOVEMENT;}
- if(key=='a' && g_stick1_x>300+STICK_R){g_stick1_x-=BALL_MOVEMENT;}
- if(key=='d' && g_stick1_x<800-STICK_R){g_stick1_x+=BALL_MOVEMENT;}//don't want to access whole half field
- }
- /*
- function iSpecialKeyboard() is called whenver user hits special keys like-
- function 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);
- }
- if(key==GLUT_KEY_UP)
- {
- if(page_draw==0&&draw_arrow==1)draw_arrow=0;
- else if(page_draw==1 && g_stick2_y<900-STICK_R-15)g_stick2_y+=BALL_MOVEMENT;
- }
- if(key==GLUT_KEY_DOWN)
- {
- if(page_draw==0&&draw_arrow==0)draw_arrow=1;
- else if(page_draw==1 && g_stick2_y>100+STICK_R+15)g_stick2_y-=BALL_MOVEMENT;
- }
- if(key==GLUT_KEY_LEFT)
- {
- if(page_draw==1 && g_stick2_x>1000+STICK_R)g_stick2_x-=BALL_MOVEMENT;
- }
- if(key==GLUT_KEY_RIGHT)
- {
- if(page_draw==0)page_draw=1;
- else if(page_draw==1 && g_stick2_x<1500-STICK_R)g_stick2_x+=BALL_MOVEMENT;
- }
- }
- void ballChange()
- {
- g_ball_x += dx;
- g_ball_y += dy;
- if(g_ball_x > 1700-BALL_R || g_ball_x < 100+BALL_R)dx = -dx;
- if(g_ball_y > 900-BALL_R || g_ball_y < 100+BALL_R)dy = -dy;
- }
- int main()
- {
- iSetTimer(3, ballChange);
- dx = 7;
- dy = 5;
- iInitialize(1800, 1000, "HIT TO WIN!!");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement