From fbd302ed00a427302a1e162b599cb59577e33a04 Mon Sep 17 00:00:00 2001 From: Guillaume Keller Date: Sat, 15 May 2021 17:59:38 +0200 Subject: [PATCH] feat: add button controllers bottom[left|right] --- include/apps/tools/snake_game.h | 10 +++++-- src/apps/main/snake_game.cpp | 50 +++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/include/apps/tools/snake_game.h b/include/apps/tools/snake_game.h index 098dfaf52..6749eed5a 100644 --- a/include/apps/tools/snake_game.h +++ b/include/apps/tools/snake_game.h @@ -19,6 +19,7 @@ class OswAppSnakeGame : public OswApp { #define cellSize 10 #define snakeLength 30 + const bool buttonControllerMode = false; //#define demo 1 // Change these values if sensitivity is too much/low @@ -32,8 +33,8 @@ class OswAppSnakeGame : public OswApp { const int gameWidth = 30; const int UP = 0; - const int DOWN = 1; - const int RIGHT = 2; + const int RIGHT = 1; + const int DOWN = 2; const int LEFT = 3; int lastDirection = DOWN; @@ -57,12 +58,14 @@ class OswAppSnakeGame : public OswApp { float deltaSeconds = 0; void drawDirection(OswHal* hal, const int xDirection, const int yDirection); - void drawDirectionArrow(OswHal* hal, const int direction); + void drawDirectionArrow(OswHal* hal, const int direction, const int topLeftX = 120, + const int topLeftY = 5); void drawPlayer(OswHal* hal); void drawGameState(OswHal* hal); void drawLunch(OswHal* hal); void drawGrid(OswHal* hal); void drawScore(OswHal* hal); + void drawButtonHints(OswHal* hal); // Game logics void snakeGame(OswHal* hal); @@ -72,6 +75,7 @@ class OswAppSnakeGame : public OswApp { bool coordsInGame(const int xCoord, const int yCoord); // Controls + void buttonController(OswHal* hal); void accelerometerController(OswHal* hal); void autoController(); diff --git a/src/apps/main/snake_game.cpp b/src/apps/main/snake_game.cpp index db5539545..c34cb7909 100644 --- a/src/apps/main/snake_game.cpp +++ b/src/apps/main/snake_game.cpp @@ -49,26 +49,25 @@ void OswAppSnakeGame::drawDirection(OswHal* hal, const int xDirection, const int } } -void OswAppSnakeGame::drawDirectionArrow(OswHal* hal, const int direction) { +void OswAppSnakeGame::drawDirectionArrow(OswHal* hal, const int direction, const int topLeftX, + const int topLeftY) { + const int length = 8; + if (direction == UP || direction == DOWN) { - const int x = 120; - const int height = 8; - const int yUp = 5; - const int yDown = yUp + height; + const int yDown = topLeftY + length; - hal->getCanvas()->drawLine(x, yUp, x, yDown, WHITE); + hal->getCanvas()->drawLine(topLeftX, topLeftY, topLeftX, yDown, WHITE); if (direction == UP) { - hal->getCanvas()->drawTriangle(x - 1, yUp, x + 1, yUp, x, yUp - 1, WHITE); + hal->getCanvas()->drawTriangle(topLeftX - 1, topLeftY, topLeftX + 1, topLeftY, topLeftX, topLeftY - 1, WHITE); } else { - hal->getCanvas()->drawTriangle(x - 1, yDown, x + 1, yDown, x, yDown + 1, WHITE); + hal->getCanvas()->drawTriangle(topLeftX - 1, yDown, topLeftX + 1, yDown, topLeftX, yDown + 1, WHITE); } } else { - const int length = 8; - const int xLeft = 116; + const int xLeft = topLeftX - length / 2; const int xRight = xLeft + length; - const int y = 9; + const int y = topLeftY + length / 2; hal->getCanvas()->drawLine(xLeft, y, xRight, y, WHITE); if (direction == LEFT) { @@ -152,6 +151,11 @@ void OswAppSnakeGame::drawScore(OswHal* hal) { hal->gfx()->print(score); } +void OswAppSnakeGame::drawButtonHints(OswHal* hal) { + drawDirectionArrow(hal, LEFT, 240 - 200, 240 - 48); + drawDirectionArrow(hal, RIGHT, 200, 240 - 48); +} + void OswAppSnakeGame::snakeGame(OswHal* hal) { hal->gfx()->setTextSize(1); @@ -161,7 +165,12 @@ void OswAppSnakeGame::snakeGame(OswHal* hal) { autoController(); fastGame = true; #else - accelerometerController(hal); + if (buttonControllerMode) { + drawButtonHints(hal); + buttonController(hal); + } else { + accelerometerController(hal); + } #endif drawGrid(hal); @@ -263,6 +272,23 @@ bool OswAppSnakeGame::coordsInGame(const int xCoord, const int yCoord) { return pow(120 - (xCoord + halfSize), 2) + pow(120 - (yCoord + halfSize), 2) <= squaredWidth && yCoord > 20; } +void OswAppSnakeGame::buttonController(OswHal* hal) { + // Top right + if (hal->btnHasGoneDown(BUTTON_3)) { + lastDirection--; + } + // Bottom right + else if (hal->btnHasGoneDown(BUTTON_1)) { + lastDirection++; + } + + if (lastDirection < UP) { + lastDirection += 4; + } else if (lastDirection > LEFT) { + lastDirection -= 4; + } +} + void OswAppSnakeGame::accelerometerController(OswHal* hal) { float xAcceleration = hal->getAccelerationX(); float yAcceleration = hal->getAccelerationY();