Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vertical speed hotfix #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 20 additions & 14 deletions src/Entities/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ void Game::update()
// Forcing Snake to move if enough time has passed
// (time based on current level)
this->timerSnake.pause();
int delta = this->getDelay(this->currentScore->speed);
int delta = this->getDelay(this->currentScore->speed, this->player->getDirection());

if (this->timerSnake.delta_ms() >= delta)
{
Expand Down Expand Up @@ -324,25 +324,31 @@ bool Game::willReturnToMenu()
{
return this->userAskedToGoToMenu;
}
int Game::getDelay(int speed)
int Game::getDelay(int speed, int direction)
{
int delay = 0;
// returning delay in milliseconds
if (speed < 1) return 800;
if (speed < 1) delay = 800;

switch (speed)
{
case 1: return 800;
case 2: return 600;
case 3: return 500;
case 4: return 300;
case 5: return 200;
case 6: return 150;
case 7: return 125;
case 8: return 100;
case 9: return 80;
case 10: return 50;
case 1: delay = 800; break;
case 2: delay = 600; break;
case 3: delay = 500; break;
case 4: delay = 300; break;
case 5: delay = 200; break;
case 6: delay = 150; break;
case 7: delay = 125; break;
case 8: delay = 100; break;
case 9: delay = 80; break;
case 10: delay = 50; break;
}
return 50;
delay = 50;
/* If the direction is UP or DOWN slow down the delay using a 2:1 ration */
if(direction == 3 || direction == 4) {
delay = delay * 1;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work? Am I missing something, or should this be delay = delay * 2?

}
return delay;
}
void Game::pause(bool option)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Game

/// Returns how much time (millisseconds) we need to wait
/// for a specific #speed.
int getDelay(int speed);
int getDelay(int speed, int direction);

void pause(bool option);

Expand Down
15 changes: 15 additions & 0 deletions src/Entities/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ bool Player::isAlive()
{
return (this->alive);
}
int Player::getDirection()
{
switch(this->currentDirection)
{
case Player::RIGHT:
return 1;
case Player::LEFT:
return 2;
case Player::UP:
return 3;
case Player::DOWN:
return 4;
}
return 0;
}
int Player::getSize()
{
return (int)(this->body.size());
Expand Down
1 change: 1 addition & 0 deletions src/Entities/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Player
bool isAlive();
int getSize();

int getDirection();
int getX(); ///< Returns the head's x position.
int getY(); ///< Returns the head's y position.

Expand Down