Refactoring for stability, etc. Add new animation.

This commit is contained in:
2023-08-27 01:24:01 +02:00
parent dccfbf758e
commit 078fa74afb
17 changed files with 1845 additions and 932 deletions

View File

@@ -60,10 +60,10 @@ void Pong::loopCycle()
*/
void Pong::ctrlUp(uint8_t playerid)
{
if (millis() > _lastButtonClick + DEBOUNCE_TIME)
if ((system_get_time() / 1000) > (_lastButtonClick + DEBOUNCE_TIME))
{
_playerMovement[playerid] = PADDLE_MOVE_DOWN; // need to swap direction as field is rotated 180deg
_lastButtonClick = millis();
_lastButtonClick = (system_get_time() / 1000);
}
}
@@ -74,10 +74,10 @@ void Pong::ctrlUp(uint8_t playerid)
*/
void Pong::ctrlDown(uint8_t playerid)
{
if (millis() > _lastButtonClick + DEBOUNCE_TIME)
if ((system_get_time() / 1000) > (_lastButtonClick + DEBOUNCE_TIME))
{
_playerMovement[playerid] = PADDLE_MOVE_UP; // need to swap direction as field is rotated 180deg
_lastButtonClick = millis();
_lastButtonClick = (system_get_time() / 1000);
}
}
@@ -88,10 +88,10 @@ void Pong::ctrlDown(uint8_t playerid)
*/
void Pong::ctrlNone(uint8_t playerid)
{
if (millis() > _lastButtonClick + DEBOUNCE_TIME)
if ((system_get_time() / 1000) > (_lastButtonClick + DEBOUNCE_TIME))
{
_playerMovement[playerid] = PADDLE_MOVE_NONE;
_lastButtonClick = millis();
_lastButtonClick = (system_get_time() / 1000);
}
}
@@ -102,9 +102,9 @@ void Pong::ctrlNone(uint8_t playerid)
*/
void Pong::initGame(uint8_t numBots)
{
(*_logger).log_string("Pong: init with " + String(numBots) + " Bots");
_logger->log_string("Pong: init with " + String(numBots) + " Bots");
resetLEDs();
_lastButtonClick = millis();
_lastButtonClick = (system_get_time() / 1000);
_numBots = numBots;
@@ -134,11 +134,11 @@ void Pong::initGame(uint8_t numBots)
void Pong::updateBall()
{
bool hitBall = false;
if ((millis() - _lastBallUpdate) < _ballDelay)
if (((system_get_time() / 1000) - _lastBallUpdate) < _ballDelay)
{
return;
}
_lastBallUpdate = millis();
_lastBallUpdate = (system_get_time() / 1000);
toggleLed(_ball.x, _ball.y, LED_TYPE_OFF);
// collision detection for player 1
@@ -199,7 +199,7 @@ void Pong::updateBall()
*/
void Pong::endGame()
{
(*_logger).log_string("Pong: Game ended");
_logger->log_string("Pong: Game ended");
_gameState = GAME_STATE_END;
toggleLed(_ball.x, _ball.y, LED_TYPE_BALL_RED);
}
@@ -210,11 +210,11 @@ void Pong::endGame()
*/
void Pong::updateGame()
{
if ((millis() - _lastDrawUpdate) < GAME_DELAY)
if (((system_get_time() / 1000) - _lastDrawUpdate) < GAME_DELAY)
{
return;
}
_lastDrawUpdate = millis();
_lastDrawUpdate = (system_get_time() / 1000);
// turn off paddle LEDs
for (uint8_t p = 0; p < PLAYER_AMOUNT; p++)
@@ -297,7 +297,7 @@ uint8_t Pong::getPlayerMovement(uint8_t playerId)
*/
void Pong::resetLEDs()
{
(*_ledmatrix).flush();
_ledmatrix->flush();
}
/**
@@ -328,5 +328,5 @@ void Pong::toggleLed(uint8_t x, uint8_t y, uint8_t type)
break;
}
(*_ledmatrix).grid_add_pixel(x, y, color);
_ledmatrix->grid_add_pixel(x, y, color);
}

View File

@@ -58,11 +58,11 @@ void Snake::loopCycle()
*/
void Snake::ctrlUp()
{
if (millis() > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING)
if ((system_get_time() / 1000) > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING)
{
(*_logger).log_string("Snake: UP");
_logger->log_string("Snake: UP");
_userDirection = DIRECTION_DOWN; // need to swap direction as field is rotated 180deg
_lastButtonClick = millis();
_lastButtonClick = (system_get_time() / 1000);
}
}
@@ -72,11 +72,11 @@ void Snake::ctrlUp()
*/
void Snake::ctrlDown()
{
if (millis() > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING)
if ((system_get_time() / 1000) > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING)
{
_logger->log_string("Snake: DOWN");
_userDirection = DIRECTION_UP; // need to swap direction as field is rotated 180deg
_lastButtonClick = millis();
_lastButtonClick = (system_get_time() / 1000);
}
}
@@ -86,11 +86,11 @@ void Snake::ctrlDown()
*/
void Snake::ctrlRight()
{
if (millis() > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING)
if ((system_get_time() / 1000) > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING)
{
_logger->log_string("Snake: RIGHT");
_userDirection = DIRECTION_LEFT; // need to swap direction as field is rotated 180deg
_lastButtonClick = millis();
_lastButtonClick = (system_get_time() / 1000);
}
}
@@ -100,11 +100,11 @@ void Snake::ctrlRight()
*/
void Snake::ctrlLeft()
{
if (millis() > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING)
if ((system_get_time() / 1000) > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING)
{
_logger->log_string("Snake: LEFT");
_userDirection = DIRECTION_RIGHT; // need to swap direction as field is rotated 180deg
_lastButtonClick = millis();
_lastButtonClick = (system_get_time() / 1000);
}
}
@@ -131,7 +131,7 @@ void Snake::initGame()
_food.y = -1;
_wormLength = MIN_TAIL_LENGTH;
_userDirection = DIRECTION_LEFT;
_lastButtonClick = millis();
_lastButtonClick = (system_get_time() / 1000);
for (int i = 0; i < MAX_TAIL_LENGTH; i++)
{
@@ -148,7 +148,7 @@ void Snake::initGame()
*/
void Snake::updateGame()
{
if ((millis() - _lastDrawUpdate) > GAME_DELAY)
if (((system_get_time() / 1000) - _lastDrawUpdate) > GAME_DELAY)
{
_logger->log_string("Snake: update game");
toggleLed(_tail[_wormLength - 1].x, _tail[_wormLength - 1].y, LED_TYPE_OFF);
@@ -197,7 +197,7 @@ void Snake::updateGame()
updateFood();
}
_lastDrawUpdate = millis();
_lastDrawUpdate = (system_get_time() / 1000);
}
}

View File

@@ -26,7 +26,7 @@ Tetris::Tetris(LEDMatrix *myledmatrix, UDPLogger *mylogger)
{
_logger = mylogger;
_ledmatrix = myledmatrix;
_gameStatet = GAME_STATE_READY;
_gameState = GAME_STATE_READY;
}
/**
@@ -35,7 +35,7 @@ Tetris::Tetris(LEDMatrix *myledmatrix, UDPLogger *mylogger)
*/
void Tetris::loopCycle()
{
switch (_gameStatet)
switch (_gameState)
{
case GAME_STATE_READY:
@@ -51,18 +51,18 @@ void Tetris::loopCycle()
// move faster down when allow drop
if (_allowdrop)
{
if (millis() > _droptime + 50)
if ((system_get_time() / 1000) > (_droptime + 50))
{
_droptime = millis();
_droptime = (system_get_time() / 1000);
shiftActiveBrick(DIR_DOWN);
printField();
}
}
// move down with regular speed
if ((millis() - _prevUpdateTime) > (_brickSpeed * _speedtetris / 100))
if (((system_get_time() / 1000) - _prevUpdateTime) > (_brickSpeed * _speedtetris / 100))
{
_prevUpdateTime = millis();
_prevUpdateTime = (system_get_time() / 1000);
shiftActiveBrick(DIR_DOWN);
printField();
}
@@ -74,7 +74,7 @@ void Tetris::loopCycle()
// and create new brick at top of field
checkFullLines();
newActiveBrick();
_prevUpdateTime = millis(); // Reset update time to avoid brick dropping two spaces
_prevUpdateTime = (system_get_time() / 1000); // Reset update time to avoid brick dropping two spaces
}
break;
case GAME_STATE_PAUSED:
@@ -85,17 +85,17 @@ void Tetris::loopCycle()
if (_tetrisGameOver == true)
{
_tetrisGameOver = false;
(*_logger).log_string("Tetris: end");
_logger->log_string("Tetris: end");
everythingRed();
_tetrisshowscore = millis();
_tetrisshowscore = (system_get_time() / 1000);
}
if (millis() > (_tetrisshowscore + RED_END_TIME))
if ((system_get_time() / 1000) > (_tetrisshowscore + RED_END_TIME))
{
resetLEDs();
_score = _nbRowsTotal;
showscore();
_gameStatet = GAME_STATE_READY;
_gameState = GAME_STATE_READY;
}
break;
}
@@ -107,10 +107,10 @@ void Tetris::loopCycle()
*/
void Tetris::ctrlStart()
{
if (millis() > _lastButtonClick + DEBOUNCE_TIME)
if ((system_get_time() / 1000) > _lastButtonClick + DEBOUNCE_TIME)
{
_lastButtonClick = millis();
_gameStatet = GAME_STATE_INIT;
_lastButtonClick = (system_get_time() / 1000);
_gameState = GAME_STATE_INIT;
}
}
@@ -120,20 +120,20 @@ void Tetris::ctrlStart()
*/
void Tetris::ctrlPlayPause()
{
if (millis() > _lastButtonClick + DEBOUNCE_TIME)
if ((system_get_time() / 1000) > _lastButtonClick + DEBOUNCE_TIME)
{
_lastButtonClick = millis();
if (_gameStatet == GAME_STATE_PAUSED)
_lastButtonClick = (system_get_time() / 1000);
if (_gameState == GAME_STATE_PAUSED)
{
(*_logger).log_string("Tetris: continue");
_logger->log_string("Tetris: continue");
_gameStatet = GAME_STATE_RUNNING;
_gameState = GAME_STATE_RUNNING;
}
else if (_gameStatet == GAME_STATE_RUNNING)
else if (_gameState == GAME_STATE_RUNNING)
{
(*_logger).log_string("Tetris: pause");
_logger->log_string("Tetris: pause");
_gameStatet = GAME_STATE_PAUSED;
_gameState = GAME_STATE_PAUSED;
}
}
}
@@ -144,9 +144,9 @@ void Tetris::ctrlPlayPause()
*/
void Tetris::ctrlRight()
{
if (millis() > _lastButtonClick + DEBOUNCE_TIME && _gameStatet == GAME_STATE_RUNNING)
if ((system_get_time() / 1000) > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING)
{
_lastButtonClick = millis();
_lastButtonClick = (system_get_time() / 1000);
shiftActiveBrick(DIR_RIGHT);
printField();
}
@@ -158,9 +158,9 @@ void Tetris::ctrlRight()
*/
void Tetris::ctrlLeft()
{
if (millis() > _lastButtonClick + DEBOUNCE_TIME && _gameStatet == GAME_STATE_RUNNING)
if ((system_get_time() / 1000) > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING)
{
_lastButtonClick = millis();
_lastButtonClick = (system_get_time() / 1000);
shiftActiveBrick(DIR_LEFT);
printField();
}
@@ -172,9 +172,9 @@ void Tetris::ctrlLeft()
*/
void Tetris::ctrlUp()
{
if (millis() > _lastButtonClick + DEBOUNCE_TIME && _gameStatet == GAME_STATE_RUNNING)
if ((system_get_time() / 1000) > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING)
{
_lastButtonClick = millis();
_lastButtonClick = (system_get_time() / 1000);
rotateActiveBrick();
printField();
}
@@ -187,10 +187,10 @@ void Tetris::ctrlUp()
void Tetris::ctrlDown()
{
// longer debounce time, to prevent immediate drop
if (millis() > _lastButtonClickr + DEBOUNCE_TIME * 5 && _gameStatet == GAME_STATE_RUNNING)
if ((system_get_time() / 1000) > _lastButtonClickr + DEBOUNCE_TIME * 5 && _gameState == GAME_STATE_RUNNING)
{
_allowdrop = true;
_lastButtonClickr = millis();
_lastButtonClickr = (system_get_time() / 1000);
}
}
@@ -221,7 +221,7 @@ void Tetris::resetLEDs()
*/
void Tetris::tetrisInit()
{
(*_logger).log_string("Tetris: init");
_logger->log_string("Tetris: init");
clearField();
_brickSpeed = INIT_SPEED;
@@ -230,9 +230,9 @@ void Tetris::tetrisInit()
_tetrisGameOver = false;
newActiveBrick();
_prevUpdateTime = millis();
_prevUpdateTime = (system_get_time() / 1000);
_gameStatet = GAME_STATE_RUNNING;
_gameState = GAME_STATE_RUNNING;
}
/**
@@ -318,7 +318,7 @@ void Tetris::newActiveBrick()
if (checkFieldCollision(&_activeBrick))
{
_tetrisGameOver = true;
_gameStatet = GAME_STATE_END;
_gameState = GAME_STATE_END;
}
}