Skip to content

Commit

Permalink
#110 update snake controller
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyshiomitsu committed Oct 28, 2024
1 parent bb13843 commit ae948a8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
11 changes: 5 additions & 6 deletions NERODevelopment/src/controllers/snakecontroller.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#include "snakecontroller.h"

SnakeController::SnakeController(Model *model, QObject *parent)
: ButtonController{model, 6, parent} {
// this->m_debounceOffset = 150;
}
: ButtonController{model, 7, parent} {}

void SnakeController::handleKeyPress(int key) {
int newDirection = m_currentDirection;
Expand All @@ -29,14 +27,15 @@ void SnakeController::handleKeyPress(int key) {
return;
}

if (!isOppositeDirection(newDirection)) {
if (!isSameOrOppositeDirection(newDirection)) {
m_currentDirection = newDirection;
emit directionChanged(m_currentDirection);
}
}

bool SnakeController::isOppositeDirection(int newDirection) {
return (m_currentDirection + 2) % 4 == newDirection;
bool SnakeController::isSameOrOppositeDirection(int newDirection) {
return (m_currentDirection + 2) % 4 == newDirection ||
m_currentDirection == newDirection;
}

void SnakeController::saveScore(int score) {
Expand Down
2 changes: 1 addition & 1 deletion NERODevelopment/src/controllers/snakecontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public slots:

private:
int m_currentDirection;
bool isOppositeDirection(int newDirection);
bool isSameOrOppositeDirection(int newDirection);
};

#endif // SNAKECONTROLLER_H

0 comments on commit ae948a8

Please sign in to comment.