game.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #define SAF_PROGRAM_NAME "meow"
  2. #include "saf.h"
  3. #define PADDLE_SPEED 3
  4. #define DELAYTIMEUNTILBALLVISIBLE 25 * 1
  5. #define DELAYTIMEUNTILBALLRUN 25 * 2
  6. int8_t ball[2];
  7. int8_t ballold[2];
  8. int8_t ballv[2];
  9. uint8_t ballOut = 1;
  10. int8_t paddle1[2];
  11. int8_t paddle2[2];
  12. uint8_t paddleWidth = 2;
  13. uint8_t paddleHeight = 6;
  14. uint8_t paddleWallDistance;
  15. uint8_t delayBallSpawnCurrent = 0;
  16. uint8_t player1Score = 0;
  17. uint8_t player2Score = 0;
  18. void GAME_clampBall(int8_t ball_in[2], int8_t ballv_in[2])
  19. {
  20. // Check if ball collided with top region of the screen
  21. if (ball_in[1] < 0)
  22. {
  23. ball_in[1] = 0;
  24. ballv_in[1] *= -1;
  25. }
  26. // Check if ball collided with bottom region of the screen
  27. else if (ball_in[1] >= SAF_SCREEN_HEIGHT)
  28. {
  29. ball_in[1] = SAF_SCREEN_HEIGHT - 1;
  30. ballv_in[1] *= -1;
  31. }
  32. }
  33. void GAME_drawPaddle(int8_t paddle_in[2], uint8_t ballHit_in)
  34. {
  35. SAF_drawRect(paddle_in[0], // position x
  36. paddle_in[1], // position y
  37. paddleWidth, // position width
  38. paddleHeight, // position height
  39. // color the paddle red if paddle was hit by a ball
  40. ballHit_in == 0 ? SAF_COLOR_WHITE : SAF_COLOR_RED,
  41. 1); // Make the paddle filled rectangle
  42. }
  43. void SAF_init(void)
  44. {
  45. paddleWallDistance = 2;
  46. paddle1[0] = paddleWallDistance,
  47. paddle1[1] = (SAF_SCREEN_HEIGHT / 2) - (paddleHeight / 2);
  48. paddle2[0] = SAF_SCREEN_WIDTH - paddleWallDistance - paddleWidth,
  49. paddle2[1] = paddle1[1];
  50. ball[0] = SAF_SCREEN_WIDTH / 2, ball[1] = SAF_SCREEN_HEIGHT / 2;
  51. ballv[0] = 1, ballv[1] = 1;
  52. }
  53. uint8_t SAF_loop()
  54. {
  55. ballold[0] = ball[0], ballold[1] = ball[1];
  56. uint8_t ballHit1 = 0, ballHit2 = 0;
  57. // Paddle 1
  58. if (SAF_buttonPressed(SAF_BUTTON_UP))
  59. {
  60. paddle1[1] -= PADDLE_SPEED;
  61. if (paddle1[1] < 0)
  62. paddle1[1] = 0;
  63. }
  64. if (SAF_buttonPressed(SAF_BUTTON_DOWN))
  65. {
  66. paddle1[1] += PADDLE_SPEED;
  67. if (paddle1[1] + paddleHeight >= SAF_SCREEN_HEIGHT)
  68. paddle1[1] = SAF_SCREEN_HEIGHT - paddleHeight;
  69. }
  70. // Paddle 2
  71. if (SAF_buttonPressed(SAF_BUTTON_B))
  72. {
  73. paddle2[1] -= PADDLE_SPEED;
  74. if (paddle2[1] < 0)
  75. paddle2[1] = 0;
  76. }
  77. if (SAF_buttonPressed(SAF_BUTTON_A))
  78. {
  79. paddle2[1] += PADDLE_SPEED;
  80. if (paddle2[1] + paddleHeight >= SAF_SCREEN_HEIGHT)
  81. paddle2[1] = SAF_SCREEN_HEIGHT - paddleHeight;
  82. }
  83. if (ballOut == 0)
  84. {
  85. // Apply ball's velocity
  86. ball[0] += ballv[0];
  87. ball[1] += ballv[1];
  88. }
  89. else
  90. {
  91. delayBallSpawnCurrent += 1;
  92. if (delayBallSpawnCurrent > DELAYTIMEUNTILBALLRUN)
  93. {
  94. delayBallSpawnCurrent = 0;
  95. ballOut = 0;
  96. }
  97. }
  98. // Check collision between ball and paddle 1
  99. if (ball[0] < paddle1[0] + paddleWidth &&
  100. ballold[0] >= paddle1[0] &&
  101. ball[1] < paddle1[1] + paddleHeight &&
  102. ball[1] >= paddle1[1])
  103. {
  104. ball[0] = paddle1[0] + paddleWidth + 1;
  105. ballv[0] *= -1;
  106. ballHit1 = 1;
  107. SAF_playSound(SAF_SOUND_BEEP);
  108. }
  109. // Check collision between ball and paddle 2
  110. if (ball[0] >= paddle2[0] &&
  111. ballold[0] < paddle2[0] &&
  112. ball[1] < paddle2[1] + paddleHeight &&
  113. ball[1] >= paddle2[1])
  114. {
  115. ball[0] = paddle2[0] - 1;
  116. ballv[0] *= -1;
  117. ballHit2 = 1;
  118. SAF_playSound(SAF_SOUND_BEEP);
  119. }
  120. /* Check if ball went outside the screen through
  121. left or right side. */
  122. if (ball[0] < 0)
  123. {
  124. ball[0] = SAF_SCREEN_WIDTH / 2;
  125. ball[1] = SAF_SCREEN_HEIGHT / 2;
  126. ballv[0] *= -1;
  127. ballOut = 1;
  128. player2Score += 1;
  129. SAF_playSound(SAF_SOUND_BOOM);
  130. }
  131. if (ball[0] >= SAF_SCREEN_WIDTH)
  132. {
  133. ball[0] = SAF_SCREEN_WIDTH / 2;
  134. ball[1] = SAF_SCREEN_HEIGHT / 2;
  135. ballv[0] *= -1;
  136. ballOut = 1;
  137. player1Score += 1;
  138. SAF_playSound(SAF_SOUND_BOOM);
  139. }
  140. GAME_clampBall(ball, ballv);
  141. /////////////////////////
  142. //// Draw code below ////
  143. /////////////////////////
  144. SAF_clearScreen(SAF_COLOR_BLUE_DARK);
  145. GAME_drawPaddle(paddle1, ballHit1);
  146. GAME_drawPaddle(paddle2, ballHit2);
  147. if (delayBallSpawnCurrent > DELAYTIMEUNTILBALLVISIBLE || !ballOut)
  148. SAF_drawPixel(ball[0], ball[1], SAF_COLOR_WHITE);
  149. char player1ScoreStr[3];
  150. SAF_intToStr(player1Score, player1ScoreStr);
  151. char player2ScoreStr[3];
  152. SAF_intToStr(player2Score, player2ScoreStr);
  153. SAF_drawText(player1ScoreStr, 0, 0, SAF_COLOR_WHITE, 1);
  154. SAF_drawText(player2ScoreStr, player2Score < 10 ? 60 : 54, 0, SAF_COLOR_WHITE, 1);
  155. /*
  156. SAF_drawPixel(SAF_SCREEN_WIDTH / 2,
  157. SAF_SCREEN_HEIGHT / 2,
  158. SAF_COLOR_RED);
  159. */
  160. return 1;
  161. }