Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #include <allegro.h>
  2.  
  3.  
  4.  
  5.  
  6. #define DOWN_RIGHT 0
  7. #define UP_RIGHT 1
  8. #define DOWN_LEFT 2
  9. #define UP_LEFT 3
  10.  
  11. void moveBall(void);
  12. void respondToKeyboard(void);
  13. void reverseVerticalDirection(void);
  14. void reverseHorizontalDirection(void);
  15.  
  16. int ball_x;
  17. int ball_y;
  18. int bar1_x;
  19. int direction;
  20. BITMAP *ball;
  21. BITMAP *buffer;
  22. SAMPLE *boing;
  23. BITMAP *bar;
  24.  
  25. int main(void)
  26. {
  27. allegro_init();
  28. install_keyboard();
  29. install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL);
  30. set_color_depth(16);
  31. set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
  32. ball = load_bitmap("ball.bmp", NULL);
  33. buffer = create_bitmap(SCREEN_W, SCREEN_H);
  34. boing = load_sample("cartoon008.wav");
  35. bar = load_bitmap("paddle.bmp", NULL);
  36. ball_x = SCREEN_W / 2;
  37. ball_y = SCREEN_H / 2;
  38. bar1_x = SCREEN_H / 2;
  39. srand(time(NULL));
  40. direction = rand() % 4;
  41.  
  42. while (!key[KEY_ESC])
  43. {
  44. moveBall();
  45. respondToKeyboard();
  46. clear_to_color(buffer, makecol(255, 255, 255));
  47. blit(ball, buffer, 0, 0, ball_x, ball_y, ball->w, ball->h);
  48. blit(bar, buffer, 0, 0, 0, bar1_x, bar->w, bar->h);
  49. blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h);
  50. clear_bitmap(buffer);
  51. }
  52. destroy_bitmap(ball);
  53. destroy_bitmap(bar);
  54. destroy_bitmap(buffer);
  55. destroy_sample(boing);
  56. return 0;
  57. }
  58. END_OF_MAIN()
  59.  
  60. void moveBall()
  61. {
  62. switch (direction) {
  63. case DOWN_RIGHT:
  64. ++ball_x;
  65. ++ball_y;
  66. break;
  67. case UP_RIGHT:
  68. ++ball_x;
  69. --ball_y;
  70. break;
  71. case DOWN_LEFT:
  72. --ball_x;
  73. ++ball_y;
  74. break;
  75. case UP_LEFT:
  76. --ball_x;
  77. --ball_y;
  78. break;
  79. }
  80.  
  81. if (ball_y <= 30 || ball_y >= 440)
  82. reverseVerticalDirection();
  83. if (ball_x <= 0 || ball_x >= 600)
  84. reverseHorizontalDirection();
  85. }
  86. void respondToKeyboard()
  87. {
  88. if (key[KEY_UP])
  89. bar1_x += -3;
  90. if (key[KEY_DOWN])
  91. bar1_x -= -3;
  92.  
  93. if (bar1_x < 0)
  94. bar1_x = 0;
  95. else if (bar1_x > 380)
  96. bar1_x = 380;
  97.  
  98.  
  99. }
  100. void reverseVerticalDirection()
  101. {
  102. if ((direction % 2) == 0)
  103. ++direction;
  104. else
  105. --direction;
  106. play_sample(boing, 255, 128, 1000, 0);
  107. }
  108. void reverseHorizontalDirection()
  109. {
  110. direction = (direction + 2) % 4;
  111. play_sample(boing, 255, 128, 1000, 0);
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement