-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
108 lines (89 loc) · 2.6 KB
/
Game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// Created by Sebastian2 on 1/28/23.
//
#include "Game.h"
// Constructor
Game::Game() : m_window("Snake", sf::Vector2u(800, 600)), m_snake(m_world.GetBlockSize()),
m_world(sf::Vector2u(800, 600))
{
RestartClock();
//srand(time(nullptr));
m_elapsedTime = 0.0f;
}
// -------------------------------------------------------------------------
// Deconstructor
Game::~Game()
{
}
// -------------------------------------------------------------------------
// Updates the game
void Game::Update()
{
m_window.Update();
// Fixed time step
float timeStep = 1.0f / m_snake.GetSpeed();
if (m_elapsedTime >= timeStep)
{
m_snake.Tick();
m_world.Update(m_snake);
m_elapsedTime -= timeStep;
if (m_snake.HasLost())
{
m_snake.Reset();
}
}
}
// -------------------------------------------------------------------------
// Handles the user input based on the key they pressed and sets the direction of the snake.
void Game::HandleInput()
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && m_snake.GetPhysicalDirection() != Direction::Down)
{
m_snake.SetDirection(Direction::Up);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && m_snake.GetPhysicalDirection() != Direction::Up)
{
m_snake.SetDirection(Direction::Down);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && m_snake.GetPhysicalDirection() != Direction::Right)
{
m_snake.SetDirection(Direction::Left);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && m_snake.GetPhysicalDirection() != Direction::Left)
{
m_snake.SetDirection(Direction::Right);
}
}
// -------------------------------------------------------------------------
// Draws the elements to the window
void Game::Render()
{
m_window.BeginDraw();
m_world.Render(*m_window.GetRenderWindow());
m_snake.Render(*m_window.GetRenderWindow());
m_window.EndDraw();
}
// -------------------------------------------------------------------------
/* Retrieves the window
*
* @return window
*/
Window *Game::GetWindow()
{
return &m_window;
}
// -------------------------------------------------------------------------
/* Retrieves the elapsed time since the last time the clock was restarted
*
* @return elapsed time since clock was restarted
*/
sf::Time Game::getElapsedTime()
{
return m_clock.getElapsedTime();
}
// -------------------------------------------------------------------------
// Restarts the clock
void Game::RestartClock()
{
m_elapsedTime += m_clock.restart().asSeconds();
}