Guest User

Untitled

a guest
Jun 26th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <freeglut.h>
  4. #include <gl\GL.h>
  5. #include <gl\GLU.h>
  6. #include "ball.hpp"
  7. #define VK_Z 0x5A
  8. #define VK_S 0x53
  9. #pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
  10.  
  11. int width = 800;
  12. int height = 600;
  13. int interval = 1000 / 60;
  14. int score_player1 = 0;
  15. int score_player2 = 0;
  16. int racket_width = 10;
  17. int racket_height = 80;
  18. int racket_speed = 8;
  19. int racket_left_x = 10;
  20. int racket_left_y = 50;
  21. int racket_right_x = width - racket_width - 10;
  22. int racket_right_y = 50;
  23. float ball_pos_x = width / 2;
  24. float ball_pos_y = height / 2;
  25. float ball_dir_x = -1.0f;
  26. float ball_dir_y = 0.0f;
  27. ball pong(8,5);
  28.  
  29.  
  30. void vec2_norm(float& x, float &y) {
  31. float length = sqrt((x * x) + (y * y));
  32. if (length != 0.0f) {
  33. length = 1.0f / length;
  34. x *= length;
  35. y *= length;
  36. }
  37. }
  38.  
  39. void updateBall() {
  40. ball_pos_x += ball_dir_x * pong.getSpeed();
  41. ball_pos_y += ball_dir_y * pong.getSpeed();
  42.  
  43. if (ball_pos_x < racket_left_x + racket_width &&
  44. ball_pos_x > racket_left_x &&
  45. ball_pos_y < racket_left_y + racket_height &&
  46. ball_pos_y > racket_left_y) {
  47. float t = ((ball_pos_y - racket_left_y) / racket_height) - 0.5f;
  48. ball_dir_x = fabs(ball_dir_x);
  49. ball_dir_y = t;
  50. }
  51. if (ball_pos_x > racket_right_x &&
  52. ball_pos_x < racket_right_x + racket_width &&
  53. ball_pos_y < racket_right_y + racket_height &&
  54. ball_pos_y > racket_right_y) {
  55. float t = ((ball_pos_y - racket_right_y) / racket_height) - 0.5f;
  56. ball_dir_x = -fabs(ball_dir_x);
  57. ball_dir_y = t;
  58. }
  59. if (ball_pos_x < 0) {
  60. ++score_player2;
  61. ball_pos_x = width / 2;
  62. ball_pos_y = height / 2;
  63. ball_dir_x = fabs(ball_dir_x);
  64. ball_dir_y = 0;
  65. }
  66. if (ball_pos_x > width) {
  67. ++score_player1;
  68. ball_pos_x = width / 2;
  69. ball_pos_y = height / 2;
  70. ball_dir_x = -fabs(ball_dir_x);
  71. ball_dir_y = 0;
  72. }
  73. if (ball_pos_y > height) {
  74. ball_dir_y = -fabs(ball_dir_y);
  75. }
  76. if (ball_pos_y < 0) {
  77. ball_dir_y = fabs(ball_dir_y);
  78. }
  79.  
  80. vec2_norm(ball_dir_x, ball_dir_y);
  81. }
  82.  
  83. void keyboard() {
  84. if (GetAsyncKeyState(VK_Z)) {
  85. if (racket_left_y >= height - racket_height) {
  86. racket_left_y += 0;
  87. } else {
  88. racket_left_y += racket_speed;
  89. }
  90. }
  91. if (GetAsyncKeyState(VK_S)) {
  92. if (racket_left_y <= 0) {
  93. racket_left_y += 0;
  94. } else {
  95. racket_left_y -= racket_speed;
  96. }
  97. }
  98. if (GetAsyncKeyState(VK_UP)) {
  99. if (racket_right_y >= height - racket_height) {
  100. racket_right_y += 0;
  101. }
  102. else {
  103. racket_right_y += racket_speed;
  104. }
  105. }
  106. if (GetAsyncKeyState(VK_DOWN)) {
  107. if (racket_right_y <= 0) {
  108. racket_right_y += 0;
  109. }
  110. else {
  111. racket_right_y -= racket_speed;
  112. }
  113. }
  114. }
  115.  
  116. void drawRect(int x, int y, int width, int height) {
  117. glBegin(GL_QUADS);
  118. glVertex2i(x, y);
  119. glVertex2i(x + width, y);
  120. glVertex2i(x + width, y + height);
  121. glVertex2i(x, y + height);
  122. glEnd();
  123. }
  124.  
  125. std::string int2str(int x) {
  126. std::stringstream ss;
  127. ss << x;
  128. return ss.str();
  129. }
  130.  
  131. void drawScore(float x, float y, std::string text) {
  132. glRasterPos2f(x, y);
  133. glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, (const unsigned char*)text.c_str());
  134. }
  135.  
  136. void draw() {
  137. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  138. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  139. glLoadIdentity();
  140. if (score_player1 >= 10) {
  141. score_player2--;
  142. drawScore((width / 2) - 70, height / 2, "Player 1 won");
  143. } else if (score_player2 >= 10) {
  144. score_player1--;
  145. drawScore((width / 2) - 70, height / 2, "Player 2 won");
  146. }
  147. else {
  148. drawScore(width / 2 - 10, height - 40, int2str(score_player1) + ":" + int2str(score_player2));
  149. drawScore(10.0f, 10.0f, int2str(racket_left_x));
  150. drawScore(600.0f, 10.0f, int2str(racket_right_x));
  151. drawRect(ball_pos_x - pong.getSize() / 2, ball_pos_y - pong.getSize() / 2, pong.getSize(), pong.getSize());
  152. drawRect(racket_left_x, racket_left_y, racket_width, racket_height);
  153. drawRect(racket_right_x, racket_right_y, racket_width, racket_height);
  154. }
  155. glutSwapBuffers();
  156. }
  157.  
  158. void update(int value) {
  159. keyboard();
  160. updateBall();
  161. glutTimerFunc(interval, update, 0);
  162. glutPostRedisplay();
  163. }
  164.  
  165. void enable2D(int width, int height) {
  166. glViewport(0, 0, width, height);
  167. glMatrixMode(GL_PROJECTION);
  168. glLoadIdentity();
  169. glOrtho(0.0f, width, 0.0f, height, 0.0f, 1.0f);
  170. glMatrixMode(GL_MODELVIEW);
  171. glLoadIdentity();
  172. }
  173.  
  174. int main(int argc, char** argv) {
  175. glutInit(&argc, argv);
  176. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  177. glutInitWindowSize(width, height);
  178. glutCreateWindow("Pong");
  179. glutDisplayFunc(draw);
  180. glutTimerFunc(interval, update, 0);
  181. enable2D(width, height);
  182. glColor3f(1.0f, 1.0f, 1.0f);
  183. glutMainLoop();
  184. return 0;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment