-Increased speed of the snake

-Added FPS counter (can be toggled with F)
This commit is contained in:
Robert Altner 2018-06-05 20:10:55 +02:00
parent f6d931f024
commit a54b58d332
5 changed files with 47 additions and 12 deletions

View file

@ -3,13 +3,17 @@
sf::RenderWindow* Framework::window = nullptr; //Set Window object to nullptr. If window is nullptr, the framework was not initialized.
sf::Event Framework::e = sf::Event();
sf::Clock Framework::c = sf::Clock();
sf::Font Framework::m_font = sf::Font();
sf::Text Framework::m_gameOverText = sf::Text();
sf::Text Framework::m_FPS = sf::Text();
Field Framework::m_field = Field();
Snake Framework::m_snake = Snake();
bool Framework::m_gameOver = false; // When the game starts, the game is not over
bool Framework::m_showFrametime = true;
double Framework::m_frametime = 0;
Framework::~Framework()
@ -30,15 +34,26 @@ ErrorType Framework::Initialize(unsigned int width, unsigned int height, std::st
// Set window properties
window = new sf::RenderWindow(sf::VideoMode(width, height), title, flags);
window->setFramerateLimit(-1);
window->setVerticalSyncEnabled(false);
//Load Font and create GameOver text
m_font.loadFromFile("font.ttf");
m_gameOverText.setFont(m_font);
m_gameOverText.setString("Game\nOver");
m_gameOverText.setCharacterSize(140);
m_gameOverText.setPosition(100, 200);
m_gameOverText.setColor(sf::Color(0, 0, 110));
m_FPS.setFont(m_font);
m_FPS.setString(std::to_string(std::floor(1 / m_frametime)));
m_FPS.setCharacterSize(32);
m_FPS.setPosition(5, 40);
m_FPS.setColor(sf::Color(0, 0, 0));
c.restart();
return NONE;
}
@ -55,12 +70,7 @@ ErrorType Framework::Run()
while (window->isOpen())
{
if (Handle() == ERROR) return ERROR;
// Only update when the game is not over
// It keeps the snake from moving
if(!m_gameOver)
if (Update() == ERROR) return ERROR;
if (Update() == ERROR) return ERROR;
if (Render() == ERROR) return ERROR;
}
@ -78,7 +88,7 @@ ErrorType Framework::Handle()
window->close();
break;
// When a key gets pressed
// When a key gets pressed
// Only used to update the snakes direction
case sf::Event::KeyPressed:
{
@ -93,6 +103,9 @@ ErrorType Framework::Handle()
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
m_snake.setDirection(Vector2D(1, 0));
if (sf::Keyboard::isKeyPressed(sf::Keyboard::F))
m_showFrametime = !m_showFrametime;
}
}
}
@ -104,8 +117,13 @@ ErrorType Framework::Update()
{
// If the snake's update returns false, it crashed
// The game is over
if (!m_snake.Update(m_field))
m_gameOver = true;
if(!m_gameOver)
if (!m_snake.Update(m_field))
m_gameOver = true;
m_FPS.setString(std::to_string((int)(1 / m_frametime)));
m_frametime = c.getElapsedTime().asSeconds();
c.restart();
return NONE;
}
@ -123,6 +141,9 @@ ErrorType Framework::Render()
if (m_gameOver)
window->draw(m_gameOverText);
if(m_showFrametime)
window->draw(m_FPS);
// Display everything to the screen
window->display();

View file

@ -61,13 +61,18 @@ private:
static sf::RenderWindow* window; // The window object
static sf::Event e; // An event holder, which saves current events
static sf::Clock c;
static Field m_field; // The gamefield (The Field is a grid of white tiles)
static Snake m_snake; // The snake (a collection of black tiles)
static sf::Font m_font; // Font Holder
static sf::Text m_gameOverText; // GameOver Text
static sf::Text m_FPS;
static bool m_gameOver;
static double m_frametime;
static bool m_showFrametime;
};

View file

@ -22,7 +22,7 @@ Snake::~Snake()
bool Snake::Update(Field& field)
{
// The snake gets updated every second
if (m_clock.getElapsedTime().asMilliseconds() >= 250)
if (m_clock.getElapsedTime().asMilliseconds() >= 50)
{
//remove the last tile in the vector, which thereby is the last piece of the snake
m_snake.pop_back();
@ -73,4 +73,13 @@ void Snake::Render(sf::RenderWindow& window)
{
window.draw(it.tile);
}
}
void Snake::setDirection(Vector2D dir)
{
if (dir.x != m_direction.x
&& dir.y != m_direction.y)
{
m_direction = dir;
}
}

View file

@ -34,7 +34,7 @@ public:
@param dir the new direction
*/
void setDirection(Vector2D dir) { m_direction = dir; }
void setDirection(Vector2D dir);
private:
std::vector<SnakePiece> m_snake;

View file

@ -3,7 +3,7 @@
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
Framework::Initialize(WIDTH, HEIGHT, "SnakePlusPlus v1.1", sf::Style::Close); // Initialize Framework
Framework::Initialize(WIDTH, HEIGHT, "SnakePlusPlus v1.2", sf::Style::Close); // Initialize Framework
Framework::Run(); // Enter program loop
return 0;