-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainGame.cpp
More file actions
233 lines (220 loc) · 5.7 KB
/
MainGame.cpp
File metadata and controls
233 lines (220 loc) · 5.7 KB
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
223
224
225
226
227
228
229
230
231
232
233
#include "MainGame.h"
#include "Snake.h"
#include <ctime>
#include <cstdlib>
#include <cassert>
CGame::CGame()
: m_iScore(0)
, m_eDirection(EDIRECTION::UNDEFINED)
, m_bInitialized(false)
, m_bGameRunning(false)
, m_bGameOver(false)
, m_bNewDirectionSet(false)
, m_bPaused(false)
, m_bSoundEnabled(true)
{
}
void CGame::Play()
{
if (!m_bInitialized)
{
return;
}
if (!m_bGameRunning)
{
return;
}
if (m_bPaused)
{
return;
}
if (m_bGameOver)
{
return;
}
// *************************************************
// Play game:
// 1: read direction (done in SetDirection())
m_bNewDirectionSet = false;
// 2: move snake in direction
CGridElement NewSnakeHead = *(m_vSnake.cbegin());
switch (m_eDirection)
{
case EDIRECTION::RIGHT:
{
NewSnakeHead.AddX(1);
break;
}
case EDIRECTION::LEFT:
{
NewSnakeHead.AddX(-1);
break;
}
case EDIRECTION::UP:
{
NewSnakeHead.AddY(-1);
break;
}
case EDIRECTION::DOWN:
{
NewSnakeHead.AddY(1);
break;
}
default:
// Somehow error something
assert(false && "Direction is not found!");
break;
};
// 3: check gameover (snakehead touching snake)
if (TestElementInSnake(NewSnakeHead, m_vSnake))
{
// Game Over
m_bGameOver = true;
return;
}
// Add new snakehead to the start of the vector
m_vSnake.insert(m_vSnake.begin(), NewSnakeHead);
// 4: check whether food is eaten
if (NewSnakeHead == m_Food)
{
// Food eaten
// 6: if eaten, increase score
m_Food.SetActive(false);
m_iScore+= 5;
}
else
{
// 5: if not eaten, pop
m_vSnake.pop_back();
}
// continues
}
bool CGame::IsGameOver()
{
const bool bFirstTimeGameOver = m_bGameRunning && m_bGameOver;
if (bFirstTimeGameOver)
{
PlaySnakeJazz(false);
}
if (m_bGameOver)
{
m_bGameRunning = false;
}
return bFirstTimeGameOver;
}
void CGame::SetDirection(const EDIRECTION eDirection)
{
if (!m_bGameRunning)
{
m_bSoundEnabled = true;
// We start playing now, or resume from pause
PlaySnakeJazz(true);
}
m_bGameRunning = true;
m_bPaused = false;
// Only set new direction if the direction has not already been changed without the game being called
if (!m_bNewDirectionSet)
{
m_bNewDirectionSet = true;
assert(eDirection != EDIRECTION::UNDEFINED);
switch (eDirection)
{
case EDIRECTION::LEFT: // Fall through
case EDIRECTION::RIGHT:
if (m_eDirection == EDIRECTION::UNDEFINED || m_eDirection == EDIRECTION::UP || m_eDirection == EDIRECTION::DOWN)
{
m_eDirection = eDirection;
}
break;
case EDIRECTION::UP: // Fall through
case EDIRECTION::DOWN:
if (m_eDirection == EDIRECTION::UNDEFINED || m_eDirection == EDIRECTION::LEFT || m_eDirection == EDIRECTION::RIGHT)
{
m_eDirection = eDirection;
}
break;
default:
assert(false);
break;
}
}
}
void CGame::TogglePause(const bool bForcedPause)
{
if (bForcedPause)
{
PlaySnakeJazz(false);
m_bPaused = true;
}
else
{
if (!m_bPaused) // If we were not paused yet, we are going to pause and thus stop the music
{
PlaySnakeJazz(false);
}
else // If we were already paused, we are going to continue and thus restart the music
{
PlaySnakeJazz(true);
}
m_bPaused = !m_bPaused;
}
}
void CGame::InitialiseGame()
{
// *************************************************
// Init:
#ifndef _DEBUG
const unsigned int iRandomInitiator = static_cast<unsigned int>(std::time(nullptr));
std::srand(iRandomInitiator);
#endif
// 1: init snake at some point
m_vSnake.clear();
const int iMiddle = (GRIDSIZE - 1) / 2;
const CGridElement SnakeBeginPoint(iMiddle, iMiddle);
m_vSnake.push_back(SnakeBeginPoint);
// 2: make sure the food is reset, done in separate thread
ResetFood();
// 3: (re)init score, game over and gamerunning
m_iScore = 0;
m_bGameRunning = false;
m_bGameOver = false;
// 4: set initialized to true to let the rest know we are ready
m_bInitialized = true;
}
void CGame::GenerateFood(bool& bKeepRunning, CGame& ActiveGame)
{
while(bKeepRunning)
{
// Check if game is running
if (ActiveGame.IsPlaying())
{
// Generate a food item somewhere
// It cannot be in the snake set!
bool bFoodLocationVerified = ActiveGame.m_Food.GetActive();
while(!bFoodLocationVerified)
{
const int iFoodX = std::rand() % GRIDSIZE;
const int iFoodY = std::rand() % GRIDSIZE;
ActiveGame.m_Food = CGridElement(iFoodX, iFoodY);
// Check validity of the NewFood location, it cannot be inside the snake
bFoodLocationVerified = !TestElementInSnake(ActiveGame.m_Food, ActiveGame.m_vSnake);
if(bFoodLocationVerified)
{
ActiveGame.m_Food.SetActive(true);
break;
}
}
}
}
}
bool CGame::TestElementInSnake(const CGridElement& ToTest, const std::vector<CGridElement>& vSnake)
{
for(const CGridElement& SnakeElement : vSnake)
{
if(ToTest == SnakeElement)
{
return true;
}
}
return false;
}