GameBoard.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #ifndef _GAMEBOARD_H_
  9. #define _GAMEBOARD_H_
  10. #include <Arduino.h>
  11. #include <M5Stack.h>
  12. #define LCD_WIDTH 320
  13. #define LCD_HEIGHT 240
  14. #define BLOCK_SIZE 16
  15. #define BLOCK_STATUS_EMPTY 0x00 // Empty block
  16. #define BLOCK_STATUS_HEAD 0x01 // Head of the snake
  17. // All values between 2 and 511 // Body of the snake, greatest
  18. // value = tail
  19. #define BLOCK_STATUS_CHERRY 0x200 // Cherry to increase the size of the snake
  20. #define DIRECTION_UP 0x01
  21. #define DIRECTION_RIGHT 0x02
  22. #define DIRECTION_DOWN 0x03
  23. #define DIRECTION_LEFT 0x04
  24. class GameBoardClass {
  25. public:
  26. // Initialize
  27. void begin(uint8_t game_cycles = 4);
  28. // Refresh display
  29. void refresh();
  30. // Start the snake
  31. void startSnake();
  32. // Make the snake move on the board
  33. // Return boolean true if OK, false if game over
  34. bool moveSnake();
  35. // Set direction
  36. void setDirection(uint8_t direction);
  37. // Add a ramdom cherry on the board
  38. void addCherry();
  39. // Get the max score
  40. uint16_t getMaxScore();
  41. private:
  42. // Variables
  43. // Current direction
  44. uint8_t current_direction = 0x00;
  45. uint8_t current_head_x = 0x00;
  46. uint8_t current_head_y = 0x00;
  47. // Cycles
  48. uint8_t max_game_cycles = 4;
  49. uint8_t current_game_cycle = 0;
  50. // Keep track of the size of the board
  51. uint8_t board_width = LCD_WIDTH / BLOCK_SIZE;
  52. uint8_t board_height = LCD_HEIGHT / BLOCK_SIZE;
  53. // Game board in memory
  54. uint16_t board_data[LCD_WIDTH / BLOCK_SIZE][LCD_HEIGHT / BLOCK_SIZE];
  55. // Track change of the game board to optimize refresh
  56. uint8_t board_changes[LCD_WIDTH / BLOCK_SIZE][LCD_HEIGHT / BLOCK_SIZE];
  57. // Internal functions
  58. // Set a value in a cell
  59. void setCell(uint8_t x, uint8_t y, uint16_t status);
  60. // Draw a cell with the change indicated
  61. void drawChange(uint8_t x, uint8_t y);
  62. // Remove the tail of the snake (put the biggest cell at blank)
  63. void removeTail();
  64. };
  65. extern GameBoardClass GameBoard;
  66. #endif // _GAMEBOARD_H_