Fixed code consistency issues

This commit is contained in:
Trimutex 2023-08-18 18:09:09 -05:00
parent c57fbc714c
commit 709cf26f99
4 changed files with 13 additions and 6 deletions

View File

@ -26,6 +26,7 @@ namespace snakeplusplus
player.Reset();
PrepareGameBoard();
isGameOver = 0;
return;
}
void GameEngine::Loop(void)
@ -60,7 +61,7 @@ namespace snakeplusplus
{
char* locationState;
locationState = &gameBoard.at(location.y).at(location.x);
if (*locationState == 'O' && player.body.size() > 1)
if (*locationState == 'O' && (player.body.size() > 1))
isGameOver = true; // Game should end (Snake touching snake)
*locationState = 'O';
player.body.push(locationState);
@ -70,6 +71,7 @@ namespace snakeplusplus
} catch (const std::out_of_range& error) {
isGameOver = true; // Snake ran into edge
}
return;
}
// Generates new food until not colliding with player
@ -77,7 +79,6 @@ namespace snakeplusplus
{
sf::Vector2f newLocation = playerFood.location;
bool isUpdated = false;
// Keep making new food until generating a valid spot
while (gameBoard.at(newLocation.y).at(newLocation.x) == 'O')
{
isUpdated = true;
@ -128,5 +129,6 @@ namespace snakeplusplus
default:
break;
}
return;
}
}

View File

@ -4,4 +4,5 @@ int main(void)
{
snakeplusplus::GameEngine game;
game.Start();
return 0;
}

View File

@ -45,8 +45,8 @@ namespace snakeplusplus
while (true)
{
gameWindow.pollEvent(event);
if ((event.type == sf::Event::Closed) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
if ((event.type == sf::Event::Closed)
|| (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
{
gameWindow.close();
return;
@ -114,8 +114,8 @@ namespace snakeplusplus
sf::Event event;
while (gameWindow.pollEvent(event))
{
if ((event.type == sf::Event::Closed) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
if ((event.type == sf::Event::Closed)
|| (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
gameWindow.close();
}
}

View File

@ -10,6 +10,7 @@ namespace snakeplusplus
{
*(body.front()) = ' ';
body.pop();
return;
}
void Snake::Reset(void)
@ -17,11 +18,13 @@ namespace snakeplusplus
while (!body.empty()) Pop();
speed.x = 0;
speed.y = 0;
return;
}
Food::Food(void)
{
generator.seed(std::random_device{}());
return;
}
// Returns a new food object for the snakeFood
@ -29,6 +32,7 @@ namespace snakeplusplus
{
location.x = GenerateRandomNumber(boundaries.x);
location.y = GenerateRandomNumber(boundaries.y);
return;
}
// Returns a newly generated number