GameBoard.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 head...)
  68. for(uint8_t x = 0; x < board_width; x++) {
  69. for(uint8_t y = 0; y < board_height; y++) {
  70. if(board_data[x][y] < BLOCK_STATUS_CHERRY && board_data[x][y] != BLOCK_STATUS_EMPTY) {
  71. board_data[x][y] = board_data[x][y] + 1;
  72. }
  73. }
  74. }
  75. // Next move to be defined
  76. int8_t next_block_x = current_head_x;
  77. int8_t next_block_y = current_head_y;
  78. // Define the next move of the head
  79. switch(current_direction) {
  80. case DIRECTION_UP :
  81. next_block_y = current_head_y - 1;
  82. break;
  83. case DIRECTION_RIGHT :
  84. next_block_x = current_head_x + 1;
  85. break;
  86. case DIRECTION_DOWN :
  87. next_block_y = current_head_y + 1;
  88. break;
  89. case DIRECTION_LEFT :
  90. next_block_x = current_head_x - 1;
  91. break;
  92. }
  93. // Check if the move is valid...
  94. // Check the limit of the board for X
  95. if(next_block_x < 0 || next_block_x >= board_width) {
  96. return false;
  97. }
  98. // Check the limit of the board for Y
  99. if(next_block_y < 0 || next_block_y >= board_height) {
  100. return false;
  101. }
  102. // Check if there is a cherry on the cell (if not a cherry, remove the last block of the tail
  103. if(board_data[next_block_x][next_block_y] != BLOCK_STATUS_CHERRY) {
  104. removeTail();
  105. }
  106. // Check if there is another part of the snake
  107. if(board_data[next_block_x][next_block_y] > BLOCK_STATUS_EMPTY && board_data[next_block_x][next_block_y] < BLOCK_STATUS_CHERRY) {
  108. return false;
  109. }
  110. // OK, move the head of the snake
  111. setCell(next_block_x, next_block_y, BLOCK_STATUS_HEAD);
  112. return true;
  113. }
  114. /**
  115. * Identify and remove the tail (last block of the snake)
  116. */
  117. void GameBoardClass::removeTail() {
  118. uint16_t greatest_value = 0;
  119. uint8_t tail_x = 0;
  120. uint8_t tail_y = 0;
  121. // Find the cell with the biggest value (it is the tail)
  122. for(uint8_t x = 0; x < board_width; x++) {
  123. for(uint8_t y = 0; y < board_height; y++) {
  124. if(board_data[x][y] < BLOCK_STATUS_CHERRY) {
  125. if(board_data[x][y] > greatest_value) {
  126. tail_x = x;
  127. tail_y = y;
  128. greatest_value = board_data[x][y];
  129. }
  130. }
  131. }
  132. }
  133. // Remove the tail
  134. setCell(tail_x, tail_y, BLOCK_STATUS_EMPTY);
  135. }
  136. /**
  137. * Get the max score
  138. */
  139. uint16_t GameBoardClass::getMaxScore() {
  140. uint16_t greatest_value = 0;
  141. // Find the cell with the biggest value (it is the tail)
  142. for(uint8_t x = 0; x < board_width; x++) {
  143. for(uint8_t y = 0; y < board_height; y++) {
  144. if(board_data[x][y] < BLOCK_STATUS_CHERRY) {
  145. if(board_data[x][y] > greatest_value) {
  146. greatest_value = board_data[x][y];
  147. }
  148. }
  149. }
  150. }
  151. return greatest_value - 1;
  152. }
  153. /**
  154. * Draw the change of one cell
  155. */
  156. void GameBoardClass::drawChange(uint8_t x, uint8_t y) {
  157. // Compute box position
  158. uint16_t pos_x = x * BLOCK_SIZE;
  159. uint16_t pos_y = y * BLOCK_SIZE;
  160. // Depending on the content of the cell, draw the box
  161. switch(board_data[x][y]) {
  162. case BLOCK_STATUS_EMPTY :
  163. M5.Lcd.fillRect(pos_x, pos_y, BLOCK_SIZE, BLOCK_SIZE, BLACK);
  164. break;
  165. case BLOCK_STATUS_CHERRY :
  166. M5.Lcd.fillRect(pos_x, pos_y, BLOCK_SIZE, BLOCK_SIZE, BLACK);
  167. M5.Lcd.fillCircle(pos_x + BLOCK_SIZE / 2, pos_y + BLOCK_SIZE / 2, BLOCK_SIZE / 2 - 1, RED);
  168. break;
  169. default :
  170. M5.Lcd.drawRect(pos_x, pos_y, BLOCK_SIZE, BLOCK_SIZE, BLACK);
  171. M5.Lcd.fillRect(pos_x + 1, pos_y + 1, BLOCK_SIZE - 2, BLOCK_SIZE - 2, WHITE);
  172. break;
  173. }
  174. }
  175. /**
  176. * Add a ramdom cherry on the board
  177. */
  178. void GameBoardClass::addCherry() {
  179. uint8_t pos_x = random(0, board_width);
  180. uint8_t pos_y = random(0, board_height);
  181. while(board_data[pos_x][pos_y] != BLOCK_STATUS_EMPTY) {
  182. pos_x = random(0, board_width);
  183. pos_y = random(0, board_height);
  184. }
  185. setCell(pos_x, pos_y, BLOCK_STATUS_CHERRY);
  186. }
  187. /**
  188. * Set direction
  189. */
  190. void GameBoardClass::setDirection(uint8_t direction) {
  191. current_direction = direction;
  192. }
  193. /**
  194. * Set a value in a cell
  195. */
  196. void GameBoardClass::setCell(uint8_t x, uint8_t y, uint16_t status) {
  197. board_data[x][y] = status;
  198. board_changes[x][y] = 1;
  199. if(status == BLOCK_STATUS_HEAD) {
  200. current_head_x = x;
  201. current_head_y = y;
  202. }
  203. }
  204. GameBoardClass GameBoard;