GameBoard.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /******************************************************************************
  2. * M5Snake : Game board management *
  3. * ------------------------------- *
  4. * Manage the game board (storage in memory and display *
  5. * Author: Olivier Staquet *
  6. * Last version available on https://github.com/ostaquet/M5Snake *
  7. *****************************************************************************/
  8. #include "GameBoard.h"
  9. /**
  10. * Initialize
  11. */
  12. void GameBoardClass::begin(uint8_t _max_game_cycles) {
  13. // Keep the number of game cycles
  14. max_game_cycles = _max_game_cycles;
  15. current_game_cycle = 0;
  16. // Init the board at blank
  17. for (uint8_t x = 0; x < board_width; x++) {
  18. for (uint8_t y = 0; y < board_height; y++) {
  19. board_data[x][y] = BLOCK_STATUS_EMPTY;
  20. board_changes[x][y] = 0;
  21. }
  22. }
  23. // Set screen to blank
  24. M5.Lcd.fillScreen(BLACK);
  25. }
  26. /**
  27. * Refresh display
  28. */
  29. void GameBoardClass::refresh() {
  30. // Check where there are some changes
  31. for (uint8_t x = 0; x < board_width; x++) {
  32. for (uint8_t y = 0; y < board_height; y++) {
  33. // Check the cell
  34. if (board_changes[x][y] > 0) {
  35. // There is a change...
  36. drawChange(x, y);
  37. // Reset the change tracking
  38. board_changes[x][y] = 0;
  39. }
  40. }
  41. }
  42. }
  43. /**
  44. * Put the head of the snake on the board and go on right
  45. */
  46. void GameBoardClass::startSnake() {
  47. // Define the middle of the screen
  48. setCell(board_width / 2, board_height / 2, BLOCK_STATUS_HEAD);
  49. // Define the direction
  50. setDirection(DIRECTION_RIGHT);
  51. }
  52. /**
  53. * Make the snake move on the board
  54. * Return boolean true if OK, false if game over
  55. */
  56. bool GameBoardClass::moveSnake() {
  57. // Check if it is a cycle to move
  58. if (current_game_cycle < max_game_cycles) {
  59. // Wait for the next cycle
  60. current_game_cycle++;
  61. return true;
  62. } else {
  63. // Reset the game cycle
  64. current_game_cycle = 0;
  65. }
  66. // Add 1 to all current block with between 1 and 512
  67. // to keep count of the movement of the snake (1 = head, 2 = 2nd block after
  68. // head...)
  69. for (uint8_t x = 0; x < board_width; x++) {
  70. for (uint8_t y = 0; y < board_height; y++) {
  71. if (board_data[x][y] < BLOCK_STATUS_CHERRY &&
  72. board_data[x][y] != BLOCK_STATUS_EMPTY) {
  73. board_data[x][y] = board_data[x][y] + 1;
  74. }
  75. }
  76. }
  77. // Next move to be defined
  78. int8_t next_block_x = current_head_x;
  79. int8_t next_block_y = current_head_y;
  80. // Define the next move of the head
  81. switch (current_direction) {
  82. case DIRECTION_UP:
  83. next_block_y = current_head_y - 1;
  84. break;
  85. case DIRECTION_RIGHT:
  86. next_block_x = current_head_x + 1;
  87. break;
  88. case DIRECTION_DOWN:
  89. next_block_y = current_head_y + 1;
  90. break;
  91. case DIRECTION_LEFT:
  92. next_block_x = current_head_x - 1;
  93. break;
  94. }
  95. // Check if the move is valid...
  96. // Check the limit of the board for X
  97. if (next_block_x < 0 || next_block_x >= board_width) {
  98. return false;
  99. }
  100. // Check the limit of the board for Y
  101. if (next_block_y < 0 || next_block_y >= board_height) {
  102. return false;
  103. }
  104. // Check if there is a cherry on the cell (if not a cherry, remove the last
  105. // block of the tail
  106. if (board_data[next_block_x][next_block_y] != BLOCK_STATUS_CHERRY) {
  107. removeTail();
  108. }
  109. // Check if there is another part of the snake
  110. if (board_data[next_block_x][next_block_y] > BLOCK_STATUS_EMPTY &&
  111. board_data[next_block_x][next_block_y] < BLOCK_STATUS_CHERRY) {
  112. return false;
  113. }
  114. // OK, move the head of the snake
  115. setCell(next_block_x, next_block_y, BLOCK_STATUS_HEAD);
  116. return true;
  117. }
  118. /**
  119. * Identify and remove the tail (last block of the snake)
  120. */
  121. void GameBoardClass::removeTail() {
  122. uint16_t greatest_value = 0;
  123. uint8_t tail_x = 0;
  124. uint8_t tail_y = 0;
  125. // Find the cell with the biggest value (it is the tail)
  126. for (uint8_t x = 0; x < board_width; x++) {
  127. for (uint8_t y = 0; y < board_height; y++) {
  128. if (board_data[x][y] < BLOCK_STATUS_CHERRY) {
  129. if (board_data[x][y] > greatest_value) {
  130. tail_x = x;
  131. tail_y = y;
  132. greatest_value = board_data[x][y];
  133. }
  134. }
  135. }
  136. }
  137. // Remove the tail
  138. setCell(tail_x, tail_y, BLOCK_STATUS_EMPTY);
  139. }
  140. /**
  141. * Get the max score
  142. */
  143. uint16_t GameBoardClass::getMaxScore() {
  144. uint16_t greatest_value = 0;
  145. // Find the cell with the biggest value (it is the tail)
  146. for (uint8_t x = 0; x < board_width; x++) {
  147. for (uint8_t y = 0; y < board_height; y++) {
  148. if (board_data[x][y] < BLOCK_STATUS_CHERRY) {
  149. if (board_data[x][y] > greatest_value) {
  150. greatest_value = board_data[x][y];
  151. }
  152. }
  153. }
  154. }
  155. return greatest_value - 1;
  156. }
  157. /**
  158. * Draw the change of one cell
  159. */
  160. void GameBoardClass::drawChange(uint8_t x, uint8_t y) {
  161. // Compute box position
  162. uint16_t pos_x = x * BLOCK_SIZE;
  163. uint16_t pos_y = y * BLOCK_SIZE;
  164. // Depending on the content of the cell, draw the box
  165. switch (board_data[x][y]) {
  166. case BLOCK_STATUS_EMPTY:
  167. M5.Lcd.fillRect(pos_x, pos_y, BLOCK_SIZE, BLOCK_SIZE, BLACK);
  168. break;
  169. case BLOCK_STATUS_CHERRY:
  170. M5.Lcd.fillRect(pos_x, pos_y, BLOCK_SIZE, BLOCK_SIZE, BLACK);
  171. M5.Lcd.fillCircle(pos_x + BLOCK_SIZE / 2, pos_y + BLOCK_SIZE / 2,
  172. BLOCK_SIZE / 2 - 1, RED);
  173. break;
  174. default:
  175. M5.Lcd.drawRect(pos_x, pos_y, BLOCK_SIZE, BLOCK_SIZE, BLACK);
  176. M5.Lcd.fillRect(pos_x + 1, pos_y + 1, BLOCK_SIZE - 2,
  177. BLOCK_SIZE - 2, WHITE);
  178. break;
  179. }
  180. }
  181. /**
  182. * Add a ramdom cherry on the board
  183. */
  184. void GameBoardClass::addCherry() {
  185. uint8_t pos_x = random(0, board_width);
  186. uint8_t pos_y = random(0, board_height);
  187. while (board_data[pos_x][pos_y] != BLOCK_STATUS_EMPTY) {
  188. pos_x = random(0, board_width);
  189. pos_y = random(0, board_height);
  190. }
  191. setCell(pos_x, pos_y, BLOCK_STATUS_CHERRY);
  192. }
  193. /**
  194. * Set direction
  195. */
  196. void GameBoardClass::setDirection(uint8_t direction) {
  197. current_direction = direction;
  198. }
  199. /**
  200. * Set a value in a cell
  201. */
  202. void GameBoardClass::setCell(uint8_t x, uint8_t y, uint16_t status) {
  203. board_data[x][y] = status;
  204. board_changes[x][y] = 1;
  205. if (status == BLOCK_STATUS_HEAD) {
  206. current_head_x = x;
  207. current_head_y = y;
  208. }
  209. }
  210. GameBoardClass GameBoard;