123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- #include <M5Stack.h>
- #include "GameboyInput.h"
- #include "Power.h"
- #include "GameBoard.h"
- #define GAME_STATUS_INIT 0x00
- #define GAME_STATUS_MENU 0x10
- #define GAME_STATUS_START_GAME 0x20
- #define GAME_STATUS_GAME 0x21
- #define GAME_STATUS_GAMEOVER 0x30
- #define GAME_CYCLES 3
- uint8_t game_status = GAME_STATUS_INIT;
- void gameLoop();
- void fromInitToMenu();
- void fromMenuToGame();
- void fromGameToGameOver();
- void setup() {
-
- game_status = GAME_STATUS_INIT;
-
-
- M5.begin(true, false, true, true);
-
- M5.Lcd.setBrightness(100);
- M5.Lcd.fillScreen(BLACK);
- M5.Lcd.setTextSize(2);
- M5.Lcd.println(F("M5Snake by Olivier Staquet"));
-
-
- dacWrite(25,0);
-
- GameboyInput.begin();
-
- Power.begin();
- Power.adaptChargeMode();
- M5.Lcd.print(F("Battery level "));
- int8_t batt = Power.getBatteryLevel();
- if(batt >= 0) {
- M5.Lcd.print(batt);
- M5.Lcd.println(F("%"));
- } else {
- M5.Lcd.println(F("unavailable"));
- }
-
- delay(1000);
- }
- void loop() {
-
- switch(game_status) {
-
- case GAME_STATUS_INIT :
- fromInitToMenu();
- break;
-
-
- case GAME_STATUS_MENU :
- if(GameboyInput.getActivity() == GAMEBOY_KEY_START) {
- fromMenuToGame();
- }
- break;
-
- case GAME_STATUS_GAME :
- gameLoop();
- break;
-
- case GAME_STATUS_GAMEOVER :
- delay(3000);
- fromInitToMenu();
- break;
- }
-
- Power.adaptChargeMode();
-
- delay(25);
- }
- void gameLoop() {
-
- switch(GameboyInput.getActivity()) {
- case GAMEBOY_KEY_UP :
- GameBoard.setDirection(DIRECTION_UP);
- break;
- case GAMEBOY_KEY_RIGHT :
- GameBoard.setDirection(DIRECTION_RIGHT);
- break;
- case GAMEBOY_KEY_DOWN :
- GameBoard.setDirection(DIRECTION_DOWN);
- break;
- case GAMEBOY_KEY_LEFT :
- GameBoard.setDirection(DIRECTION_LEFT);
- break;
- }
-
- if(!GameBoard.moveSnake()) {
-
- fromGameToGameOver();
- } else {
-
- if(random(0,15 * GAME_CYCLES) == 0) {
- GameBoard.addCherry();
- }
-
- GameBoard.refresh();
- }
- }
- void fromInitToMenu() {
-
- M5.Lcd.setBrightness(100);
- M5.Lcd.fillScreen(BLACK);
-
- M5.Lcd.setTextSize(5);
- M5.Lcd.setTextColor(RED);
- M5.Lcd.drawString(F("M5Snake"), (M5.Lcd.width() - M5.Lcd.textWidth(F("M5Snake"))) / 2, M5.Lcd.height() / 4);
-
- M5.Lcd.setTextSize(1);
- M5.Lcd.setTextColor(WHITE);
- M5.Lcd.drawString(F("by Olivier Staquet"), (M5.Lcd.width() - M5.Lcd.textWidth(F("by Olivier Staquet"))) / 2, M5.Lcd.height() / 2);
-
- M5.Lcd.setTextSize(2);
- M5.Lcd.setTextColor(WHITE);
- M5.Lcd.drawString(F("Press START to continue"), (M5.Lcd.width() - M5.Lcd.textWidth(F("Press START to continue"))) / 2, (M5.Lcd.height() / 4) * 3);
-
- game_status = GAME_STATUS_MENU;
- }
- void fromMenuToGame() {
-
- GameBoard.begin(GAME_CYCLES);
-
- GameBoard.startSnake();
- GameBoard.refresh();
-
- game_status = GAME_STATUS_GAME;
- }
- void fromGameToGameOver() {
-
- M5.Lcd.setBrightness(100);
- M5.Lcd.fillScreen(RED);
- M5.Lcd.setTextSize(5);
- M5.Lcd.setTextColor(BLACK);
- M5.Lcd.drawString(F("GAME OVER"), (M5.Lcd.width() - M5.Lcd.textWidth(F("GAME OVER"))) / 2, M5.Lcd.height() / 4);
-
- M5.Lcd.setTextSize(2);
- M5.Lcd.setTextColor(WHITE);
- M5.Lcd.drawString(F("Score"), (M5.Lcd.width() - M5.Lcd.textWidth(F("Score"))) / 2, M5.Lcd.height() / 2);
-
- M5.Lcd.setTextSize(4);
- M5.Lcd.setTextColor(WHITE);
- M5.Lcd.drawString(String(GameBoard.getMaxScore()), (M5.Lcd.width() - M5.Lcd.textWidth(String(GameBoard.getMaxScore()))) / 2, (M5.Lcd.height() / 4) * 3);
- game_status = GAME_STATUS_GAMEOVER;
- }
|