Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. /* author: S. M. Shahriar Nirjon
  2. last modified: August 8, 2008
  3. */
  4.  
  5.  
  6.  
  7. # include "iGraphics.h"
  8. # include "gl.h"
  9. #include<windows.h>
  10.  
  11. #define BALL_R 15
  12. #define BALL_MOVEMENT 20
  13. #define STICK_R 46//from the bmp pixel size
  14.  
  15. int g_ball_x=900, g_ball_y=450;//start from the center
  16. int dx, dy;
  17. int g_stick1_x=100+200,g_stick1_y=500,g_stick2_x=1700-200,g_stick2_y=500;
  18.  
  19. int draw_text=0;
  20.  
  21. int page_draw=0;//0 means start page, 1 means star play and 2 means settings
  22. int draw_arrow=0;
  23.  
  24. void start_page()
  25. {
  26. iSetColor(255,0,0);
  27. iText(10,10,"Press Right arrow on your selected choice");
  28. iShowBMP(600,250,"start_page.bmp");//(1800/2)-pixel_size_x(600)/2; 1000/2-pixel_size_y(500)/2;
  29. if(draw_arrow==0)iShowBMP(600+210,250+110,"arrow.bmp");
  30. if(draw_arrow==1)iShowBMP(600+185,250+50,"arrow.bmp");
  31. }
  32.  
  33. void is_goal()
  34. {
  35. if(g_ball_y>450 && g_ball_y<550 && g_ball_x-BALL_R-100<10)//should be in between 450 and 550
  36. draw_text=1;
  37.  
  38. if(g_ball_y<550 && g_ball_y>450 && 1700-g_ball_x-BALL_R<10)//should be in between 450 and 550
  39. draw_text=1;
  40. else draw_text=0;
  41. }
  42. void ball_hit()
  43. {
  44. dx=-dx;
  45. dy=-dy;
  46. // PlaySound((LPCSTR) "C:\\Users\\Teertho\\Desktop\\iGraphics Project\\iGraphics\\Hit.mp3", NULL, SND_FILENAME | SND_ASYNC);
  47. }
  48. void draw_field()
  49. {
  50. iSetColor(255,255,255);
  51. //iRectangle(100,100,1600,800);
  52. iLine(100,450,100,100);
  53. iLine(100,550,100,900);
  54. iLine(100,100,1700,100);
  55. iLine(100,900,1700,900);
  56. iLine(1700,100,1700,450);
  57. iLine(1700,550,1700,900);
  58. //field stretches from point 100 to 1700 in x co-ordinate. and 100 to 900 in y co-ordinate
  59. iLine(900,900,900,100);//draws the middle line
  60. iCircle(900,500,100);//Draws the middle circle
  61.  
  62. iSetColor(125,125,0);//Now comes the goal post
  63. iLine(1700,450,1700,550);
  64. iLine(100,450,100,550);
  65. }
  66.  
  67.  
  68. void ball()
  69. {
  70. iSetColor(255,0,0);
  71. iFilledCircle(g_ball_x,g_ball_y,BALL_R);
  72. 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))
  73. {
  74. ball_hit();
  75. }
  76.  
  77. }
  78.  
  79. void draw_stick()
  80. {
  81. iShowBMP(g_stick1_x-46,g_stick1_y-46,"stick1.bmp");
  82. iShowBMP(g_stick2_x-46,g_stick2_y-46,"stick2.bmp");
  83. //-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
  84. }
  85.  
  86. void draw_goal()
  87. {
  88. iSetColor(255,255,255);
  89. iText(800,500,"Goal Scored!!!!!");
  90. }
  91.  
  92. void iDraw()
  93. {
  94. iClear();
  95. if(page_draw==0)start_page();
  96. else
  97. {
  98. if(draw_text==0) is_goal();
  99. if(draw_text==1) iShowBMP(0,0,"goal.bmp");
  100. else
  101. {
  102. //PlaySound("C:\Users\Teertho\Desktop\iGraphics Project\iGraphics\Hit.mp3", NULL, SND_FILENAME | SND_ASYNC);
  103. draw_field();
  104. draw_stick();
  105. ball();
  106. }
  107. }
  108. }
  109. /*
  110. function iMouseMove() is called when the user presses and drags the mouse.
  111. (mx, my) is the position where the mouse pointer is.
  112. */
  113.  
  114. void iMouseMove(int mx, int my)
  115. {
  116. //place your codes here
  117. }
  118.  
  119. void iMouse(int button, int state, int mx, int my)
  120. {
  121. if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN);
  122. if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN);
  123. }
  124.  
  125. /*
  126. function iKeyboard() is called whenever the user hits a key in keyboard.
  127. key- holds the ASCII value of the key pressed.
  128. */
  129. void iKeyboard(unsigned char key)
  130. {
  131. if(key == 'p')
  132. {
  133. //do something with 'q'
  134. iPauseTimer(0);
  135. }
  136. if(key == 'r')
  137. {
  138. iResumeTimer(0);
  139. }
  140. 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
  141. if(key=='s' && g_stick1_y>100+STICK_R+15){g_stick1_y-=BALL_MOVEMENT;}
  142. if(key=='a' && g_stick1_x>300+STICK_R){g_stick1_x-=BALL_MOVEMENT;}
  143. if(key=='d' && g_stick1_x<800-STICK_R){g_stick1_x+=BALL_MOVEMENT;}//don't want to access whole half field
  144.  
  145.  
  146. }
  147. /*
  148. function iSpecialKeyboard() is called whenver user hits special keys like-
  149. function keys, home, end, pg up, pg down, arraows etc. you have to use
  150. appropriate constants to detect them. A list is:
  151. GLUT_KEY_F1, GLUT_KEY_F2, GLUT_KEY_F3, GLUT_KEY_F4, GLUT_KEY_F5, GLUT_KEY_F6,
  152. GLUT_KEY_F7, GLUT_KEY_F8, GLUT_KEY_F9, GLUT_KEY_F10, GLUT_KEY_F11, GLUT_KEY_F12,
  153. GLUT_KEY_LEFT, GLUT_KEY_UP, GLUT_KEY_RIGHT, GLUT_KEY_DOWN, GLUT_KEY_PAGE UP,
  154. GLUT_KEY_PAGE DOWN, GLUT_KEY_HOME, GLUT_KEY_END, GLUT_KEY_INSERT
  155. */
  156. void iSpecialKeyboard(unsigned char key)
  157. {
  158.  
  159. if(key == GLUT_KEY_END)
  160. {
  161. exit(0);
  162. }
  163. if(key==GLUT_KEY_UP)
  164. {
  165. if(page_draw==0&&draw_arrow==1)draw_arrow=0;
  166. else if(page_draw==1 && g_stick2_y<900-STICK_R-15)g_stick2_y+=BALL_MOVEMENT;
  167. }
  168. if(key==GLUT_KEY_DOWN)
  169. {
  170. if(page_draw==0&&draw_arrow==0)draw_arrow=1;
  171. else if(page_draw==1 && g_stick2_y>100+STICK_R+15)g_stick2_y-=BALL_MOVEMENT;
  172. }
  173. if(key==GLUT_KEY_LEFT)
  174. {
  175. if(page_draw==1 && g_stick2_x>1000+STICK_R)g_stick2_x-=BALL_MOVEMENT;
  176. }
  177. if(key==GLUT_KEY_RIGHT)
  178. {
  179. if(page_draw==0)page_draw=1;
  180. else if(page_draw==1 && g_stick2_x<1500-STICK_R)g_stick2_x+=BALL_MOVEMENT;
  181. }
  182. }
  183.  
  184. void ballChange()
  185. {
  186. g_ball_x += dx;
  187. g_ball_y += dy;
  188.  
  189. if(g_ball_x > 1700-BALL_R || g_ball_x < 100+BALL_R)dx = -dx;
  190. if(g_ball_y > 900-BALL_R || g_ball_y < 100+BALL_R)dy = -dy;
  191. }
  192.  
  193. int main()
  194. {
  195.  
  196. iSetTimer(3, ballChange);
  197. dx = 7;
  198. dy = 5;
  199. iInitialize(1800, 1000, "HIT TO WIN!!");
  200.  
  201. return 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement