From 9a19260a6cb6321bfd1614961dc7abd49a99cd72 Mon Sep 17 00:00:00 2001 From: Markus Ransberger Date: Mon, 25 Sep 2023 22:22:44 +0200 Subject: [PATCH] Snake: prevent that "ma sich seiba fressn ko". --- src/games/snake.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/games/snake.cpp b/src/games/snake.cpp index 562b710..869c8f4 100644 --- a/src/games/snake.cpp +++ b/src/games/snake.cpp @@ -26,7 +26,7 @@ Snake::Snake() * @param myledmatrix pointer to LEDMatrix object, need to provide gridAddPixel(x, y, col), gridFlush() * @param mylogger pointer to UDPLogger object, need to provide a function logString(message) */ -Snake::Snake(LEDMatrix * matrix, UDPLogger * logger) +Snake::Snake(LEDMatrix *matrix, UDPLogger *logger) { _logger = logger; _ledmatrix = matrix; @@ -58,7 +58,7 @@ void Snake::loopCycle() */ void Snake::ctrlUp() { - if ((system_get_time() / 1000) > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING) + if (((system_get_time() / 1000) > (_lastButtonClick + DEBOUNCE_TIME)) && (_gameState == GAME_STATE_RUNNING) && (_userDirection != DIRECTION_UP)) { _logger->log_string("Snake: UP"); _userDirection = DIRECTION_DOWN; // need to swap direction as field is rotated 180deg @@ -72,7 +72,7 @@ void Snake::ctrlUp() */ void Snake::ctrlDown() { - if ((system_get_time() / 1000) > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING) + if (((system_get_time() / 1000) > (_lastButtonClick + DEBOUNCE_TIME)) && (_gameState == GAME_STATE_RUNNING) && (_userDirection != DIRECTION_DOWN)) { _logger->log_string("Snake: DOWN"); _userDirection = DIRECTION_UP; // need to swap direction as field is rotated 180deg @@ -86,7 +86,7 @@ void Snake::ctrlDown() */ void Snake::ctrlRight() { - if ((system_get_time() / 1000) > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING) + if (((system_get_time() / 1000) > (_lastButtonClick + DEBOUNCE_TIME)) && (_gameState == GAME_STATE_RUNNING) && (_userDirection != DIRECTION_RIGHT)) { _logger->log_string("Snake: RIGHT"); _userDirection = DIRECTION_LEFT; // need to swap direction as field is rotated 180deg @@ -100,7 +100,7 @@ void Snake::ctrlRight() */ void Snake::ctrlLeft() { - if ((system_get_time() / 1000) > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING) + if (((system_get_time() / 1000) > (_lastButtonClick + DEBOUNCE_TIME)) && (_gameState == GAME_STATE_RUNNING) && (_userDirection != DIRECTION_LEFT)) { _logger->log_string("Snake: LEFT"); _userDirection = DIRECTION_RIGHT; // need to swap direction as field is rotated 180deg