-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
222 lines (206 loc) · 5.96 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "../include/game.h"
std::vector<sf::Vector2f> appendVector(const std::vector<sf::Vector2f>& a,const std::vector<sf::Vector2f>& b)
{
std::vector<sf::Vector2f> temp = a;
for ( std::vector<sf::Vector2f>::const_iterator iter = b.begin(); iter != b.end(); iter++ )
{
temp.emplace_back(*iter);
}
return temp;
}
Game::level Game::gameLevel = Game::EASY;
Game::state Game::gameState = Game::INMENU;
Game::speed Game::gameSpeed = Game::ONE;
void Game::setGameLevel(const level newGameLevel)
{
gameLevel = newGameLevel;
}
void Game::setGameState(const state newGameState)
{
gameState = newGameState;
}
void Game::setGameSpeed(const speed newGameSpeed)
{
gameSpeed = newGameSpeed;
}
Game::level Game::getGameLevel()
{
return gameLevel;
}
Game::state Game::getGameState()
{
return gameState;
}
Game::speed Game::getGameSpeed()
{
return gameSpeed;
}
Game::Game()
: gameWindow(sf::VideoMode(WINDOW_WIDTH,WINDOW_HEIGHT),WINDOW_TITLE), gameWorm(), gameMenu(), gameFood(),
gameWall()
{
gameWindow.setFramerateLimit(MAXIMUM_FPS);
}
void Game::run()
{
while(gameWindow.isOpen())
{
sf::Event e;
while(gameWindow.pollEvent(e))
{
eventHandler(e);
}
update();
render();
}
}
void Game::initializeGame()
{
gameState = INGAME;
gameMenu.setMenuState(Menu::NONESTATE);
reinitializeGameFood();
gameFood.reset();
gameWorm.reset();
/// speed*15, max of 45 fps for gamespeed.
gameWindow.setFramerateLimit((static_cast<int>(gameSpeed)+1)*15);
}
void Game::reinitializeGameFood()
{
if ( gameLevel == EASY )
{
gameFood.setPositionsToAvoid(gameWorm.getSnakePosition());
///avoid food to spawn in snake position
}
else if ( gameLevel == HARD )
{
gameFood.setPositionsToAvoid(appendVector(gameWorm.getSnakePosition(),gameWall.getWalls()));
///avoid food to spawn in snake and wall position
}
}
#include <iostream>
void Game::eventHandler(sf::Event e)
{
if ( e.type == sf::Event::Closed )
{
gameWindow.close();
}
if ( gameState == INMENU )
{
if ( gameMenu.getMenuState() == Menu::EXITED )
{
gameWindow.close();
}
gameMenu.setMousePosition(sf::Mouse::getPosition(gameWindow)); /// save the mouse position in menu
if ( e.type == sf::Event::MouseButtonPressed )
{
if ( gameMenu.getMenuState() == Menu::START ) /// press any key to start
{
initializeGame();
}
if ( e.mouseButton.button == sf::Mouse::Left )
{
gameMenu.changeStateWhenClicked(); /// menu updater
}
}
else if ( e.type == sf::Event::MouseMoved )
{
gameMenu.changeColorWhenHovered(); /// the text turns red when u hovered it
}
}
if ( e.type == sf::Event::KeyPressed )
{
if ( gameState == INGAME )
{
switch(e.key.code)
{
case sf::Keyboard::Up:
gameWorm.setNewDir(Worm::UP);
break;
case sf::Keyboard::Down:
gameWorm.setNewDir(Worm::DOWN);
break;
case sf::Keyboard::Right:
gameWorm.setNewDir(Worm::RIGHT);
break;
case sf::Keyboard::Left:
gameWorm.setNewDir(Worm::LEFT);
break;
case sf::Keyboard::P: /// pause
gameState = INMENU;
gameMenu.setMenuState(Menu::PAUSED);
break;
default:
break;
}
}
else if ( gameState == INMENU )
{
switch(e.key.code)
{
case sf::Keyboard::Escape: /// go back to previous state
if ( gameMenu.getMenuState() == Menu::PAUSED )
{
gameMenu.setMenuState(Menu::NONESTATE);
gameState = INGAME;
}
else if ( gameMenu.getMenuState() != Menu::GAMEOVER )
{
gameMenu.setMenuState(gameMenu.getPreviousMenuState());
}
break;
default: /// press any key to start
if ( gameMenu.getMenuState() == Menu::START )
{
initializeGame();
}
break;
}
}
}
}
void Game::update()
{
if ( gameState == INGAME )
{
if ( !gameWorm.getIsDead() ) /// check if snake is still alive
{
gameWorm.update();
gameFood.update();
if ( gameWorm.getSprite().getGlobalBounds().intersects(gameFood.getSprite().getGlobalBounds()) )
{
/// check if snake eats food
gameWorm.grow(); ///grow snake tail
reinitializeGameFood(); ///set the positions to avoid
gameFood.reset(); ///respawn food
}
}
else ///set the score if snake is dead
{
gameState = INMENU;
gameMenu.setMenuState(Menu::GAMEOVER);
gameMenu.setScoreText(gameWorm.getTails().size()-gameWorm.getDefaultTailSize()-1);
}
}
}
void Game::render()
{
gameWindow.clear();
if ( gameState == INMENU ) /// draw menu objects
{
gameMenu.draw(gameWindow);
if ( gameMenu.getMenuState() == Menu::GAMEOVER )
{
gameWorm.showDead(gameWindow);
}
}
else if ( gameState == INGAME ) /// draw in-game objects
{
if ( gameLevel == HARD )
{
gameWall.draw(gameWindow);
}
gameWorm.draw(gameWindow);
gameFood.draw(gameWindow);
}
gameWindow.display();
}