Skip to content

Commit

Permalink
feat: add button controllers bottom[left|right]
Browse files Browse the repository at this point in the history
  • Loading branch information
GPaddle committed May 15, 2021
1 parent ac4617d commit fbd302e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
10 changes: 7 additions & 3 deletions include/apps/tools/snake_game.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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();

Expand Down
50 changes: 38 additions & 12 deletions src/apps/main/snake_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit fbd302e

Please sign in to comment.