Advertisement
Guest User

Untitled

a guest
Jan 15th, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. #include <allegro.h>
  2. #include <cstdlib>
  3. #include <time.h>
  4. int ball_x = 320;
  5. int ball_y = 240;
  6. int ball_tempX = 320;
  7. int ball_tempY = 240;
  8. int p1_x = 20;
  9. int p1_y = 210;
  10. int p1_tempX = 20;
  11. int p1_tempY = 210;
  12. int p2_x = 620;
  13. int p2_y = 210;
  14. int p2_tempX = 620;
  15. int p2_tempY = 210;
  16. time_t secs; //The seconds on the system clock will be stored here
  17. //this will be used as the seed for srand()
  18. int dir; //This will keep track of the circles direction
  19. //1= up and left, 2 = down and left, 3 = up and right, 4 = down and
  20. right
  21. BITMAP *buffer; //This will be our temporary bitmap for double buffering
  22. void moveBall(){
  23. ball_tempX = ball_x;
  24. ball_tempY = ball_y;
  25. if (dir == 1 && ball_x > 5 && ball_y > 5){
  26.  
  27. if( ball_x == p1_x + 15 && ball_y >= p1_y && ball_y <= p1_y + 60){
  28. dir = rand()% 2 + 3;
  29. }else{
  30. --ball_x;
  31. --ball_y;
  32. }
  33.  
  34. } else if (dir == 2 && ball_x > 5 && ball_y < 475){
  35. if( ball_x == p1_x + 15 && ball_y >= p1_y && ball_y <= p1_y + 60){
  36. dir = rand()% 2 + 3;
  37. }else{
  38. --ball_x;
  39. ++ball_y;
  40. }
  41. } else if (dir == 3 && ball_x < 635 && ball_y > 5){
  42. if( ball_x + 5 == p2_x && ball_y >= p2_y && ball_y <= p2_y + 60){
  43. dir = rand()% 2 + 1;
  44. }else{
  45. ++ball_x;
  46. --ball_y;
  47. }
  48. } else if (dir == 4 && ball_x < 635 && ball_y < 475){
  49. if( ball_x + 5 == p2_x && ball_y >= p2_y && ball_y <= p2_y + 60){
  50. dir = rand()% 2 + 1;
  51. }else{
  52. ++ball_x;
  53. ++ball_y;
  54. }
  55. } else {
  56. if (dir == 1 || dir == 3) ++dir;
  57. else if (dir == 2 || dir == 4) --dir;
  58. }
  59.  
  60. acquire_screen();
  61. circlefill ( buffer, ball_tempX, ball_tempY, 5, makecol( 0, 0, 0));
  62. circlefill ( buffer, ball_x, ball_y, 5, makecol( 128, 255, 0));
  63. draw_sprite( screen, buffer, 0, 0);
  64. release_screen();
  65.  
  66. rest(5);
  67. }
  68. void p1Move(){
  69. p1_tempY = p1_y;
  70. if( key[KEY_W] && p1_y > 0){
  71.  
  72. --p1_y;
  73.  
  74. } else if( key[KEY_S] && p1_y < 420){
  75.  
  76. ++p1_y;
  77.  
  78. }
  79.  
  80. acquire_screen();
  81. rectfill( buffer, p1_tempX, p1_tempY, p1_tempX + 10, p1_tempY + 60, makecol ( 0,
  82. 0, 0));
  83. rectfill( buffer, p1_x, p1_y, p1_x + 10, p1_y + 60, makecol ( 0, 0, 255));
  84. release_screen();
  85.  
  86. }
  87. void p2Move(){
  88. p2_tempY = p2_y;
  89. if( key[KEY_UP] && p2_y > 0){
  90.  
  91. --p2_y;
  92.  
  93. } else if( key[KEY_DOWN] && p2_y < 420){
  94.  
  95. ++p2_y;
  96.  
  97. }
  98.  
  99. acquire_screen();
  100. rectfill( buffer, p2_tempX, p2_tempY, p2_tempX + 10, p2_tempY + 60, makecol ( 0,
  101. 0, 0));
  102. rectfill( buffer, p2_x, p2_y, p2_x + 10, p2_y + 60, makecol ( 0, 0, 255));
  103. release_screen();
  104.  
  105. }
  106. void startNew(){
  107. clear_keybuf();
  108. readkey();
  109. clear_to_color( buffer, makecol( 0, 0, 0));
  110. ball_x = 320;
  111. ball_y = 240;
  112. p1_x = 20;
  113. p1_y = 210;
  114. p2_x = 620;
  115. p2_y = 210;
  116. }
  117. void checkWin(){
  118. if ( ball_x < p1_x){
  119. textout_ex( screen, font, "Player 2 Wins!", 320, 240, makecol( 255, 0, 0),
  120. makecol( 0, 0, 0));
  121. startNew();
  122. } else if ( ball_x > p2_x){
  123. textout_ex( screen, font, "Player 1 Wins!", 320, 240, makecol( 255, 0, 0),
  124. makecol( 0, 0, 0));
  125. startNew();
  126. }
  127.  
  128. }
  129. void setupGame(){
  130. acquire_screen();
  131. rectfill( buffer, p1_x, p1_y, p1_x + 10, p1_y + 60, makecol ( 0, 0, 255));
  132. rectfill( buffer, p2_x, p2_y, p2_x + 10, p2_y + 60, makecol ( 0, 0, 255));
  133. circlefill ( buffer, ball_x, ball_y, 5, makecol( 128, 255, 0));
  134. draw_sprite( screen, buffer, 0, 0);
  135. release_screen();
  136.  
  137. time(&secs);
  138. srand( (unsigned int)secs);
  139. dir = rand() % 4 + 1;
  140.  
  141. }
  142. int main(){
  143. allegro_init();
  144. install_keyboard();
  145. set_color_depth(16);
  146. set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
  147.  
  148. buffer = create_bitmap( 640, 480);
  149.  
  150. setupGame();
  151.  
  152. while( !key[KEY_ESC]){
  153. p1Move();
  154. p2Move();
  155. moveBall();
  156. checkWin();
  157.  
  158. }
  159.  
  160. return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement