-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
242 lines (205 loc) · 5.12 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "Game.h"
Game::Game() : m_window{ sf::VideoMode(576, 700), "Tetris" }, m_scoreView{Position(390,595)}
{
m_grid = Grid();
m_texture.loadFromFile("arcade.png");
m_size = m_texture.getSize();
m_sprite.setTexture(m_texture);
m_sprite.move(-450, 0);
m_seconds = 1;
m_score = 0;
m_scoreReachedHundred = false;
m_gameOver = false;
}
void Game::startGame()
{
BlockGenerator blockgen;
auto block = blockgen.getRandomBlock();
std::clock_t start;
double duration;
start = std::clock();
while (m_window.isOpen() && !m_gameOver)
{
while (m_window.pollEvent(m_event))
{
if (m_event.type == sf::Event::Closed)
m_window.close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left))
{
block->move(-35, m_vmatrix);
std::cout << "Live Keyboard Input: Left Key" << std::endl;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right))
{
block->move(35, m_vmatrix);
std::cout << "Live Keyboard Input: Right Key" << std::endl;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up))
{
block->rotate(m_vmatrix);
std::cout << "Live Keyboard Input: Up Key" << std::endl;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down))
{
block->drop(m_vmatrix);
std::cout << "Live Keyboard Input: Down Key" << std::endl;
}
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
{
std::cout << "Live Mouse Input: Left Button" << std::endl;
}
}
// update the game
m_window.clear();
//BACKGROUND
m_window.draw(m_sprite);
draw(m_grid);
draw(*block);
draw(m_scoreView);
drawLandedBlocks();
m_window.display();
duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
if (duration > m_seconds) {
m_seconds += 1;
block->drop(m_vmatrix);
}
if (block->getIsLanded()) {
m_landedBlockStorage.push_back(block);
updateVMatrix(block);
if(m_gameOver){
//m_texture.loadFromFile("/tmp/this-is-so-much-worse-than-the-game-over-screen.jpeg");
//m_size = m_texture.getSize();
//m_sprite.setTexture(m_texture);
//sf::Window window(sf::VideoMode(800, 600), "My window");
stopGame();
}
block = blockgen.getRandomBlock();
clearCompletedRows();
//checkGameOver(block);
m_scoreView.setScoreText(int(m_score));
std::cout << "Row 19 completed: " << m_vmatrix.checkRowComplete(19) << "\n";
}
}
}
inline void Game::draw(sf::Sprite &sprite)
{
m_window.draw(sprite);
}
void Game::stopGame(){
m_grid.~Grid();
m_landedBlockStorage.clear();
Menu menu(m_window.getSize().x, m_window.getSize().y);
while(m_window.isOpen()){
while (m_window.pollEvent(m_event))
{
switch (m_event.type)
{
case sf::Event::KeyReleased:
switch (m_event.key.code)
{
case sf::Keyboard::Up:
menu.MoveUp();
break;
case sf::Keyboard::Down:
menu.MoveDown();
break;
case sf::Keyboard::Return:
switch (menu.GetPressedItem())
{
case 0:
std::cout << "Play button has been pressed" << std::endl;
break;
case 1:
std::cout << "Option button has been pressed" << std::endl;
break;
case 2:
m_window.close();
break;
}
break;
}
break;
case sf::Event::Closed:
m_window.close();
break;
}
}
m_window.clear();
menu.draw(m_window);
m_window.display();
}
}
void Game::draw(ScoreView &scoreView)
{
if (!m_scoreReachedHundred && m_score > 99) {
m_scoreReachedHundred = true;
scoreView.setScorePosition(Position(scoreView.getScorePosition().getX()-17, scoreView.getScorePosition().getY()));
}
m_window.draw(scoreView.getScoreRectangle());
m_window.draw(scoreView.getScoreText());
}
void Game::draw(Grid &grid)
{
for (int row = 0; row != grid.getMatrix().size(); row++) {
for (int col = 0; col != grid.getMatrix().at(row).size(); col++) {
m_window.draw(grid.getCell(row, col).getSquare());
}
}
}
void Game::draw(Movable& m)
{
for (auto& b : m.getBlockArray()) {
m_window.draw(b.getCell().getSquare());
}
}
void Game::drawLandedBlocks()
{
if (!m_landedBlockStorage.empty()) {
for (auto& ptr : m_landedBlockStorage) {
draw(*ptr);
}
}
}
bool Game::checkLanded(Movable &block)
{
return block.getIsLanded();
}
void Game::updateVMatrix(Movable *movable)
{
for (auto &block : movable->getBlockArray()) {
m_gameOver = !m_vmatrix.setBlock(block.getCell().getPosition());
}
m_vmatrix.printMatrix();
}
void Game::clearCompletedRows()
{
float amountRows = 0;
for (int i = 0; i != m_vmatrix.getMatrix().size(); i++) {
if (m_vmatrix.checkRowComplete(i)) {
amountRows += 1;
m_vmatrix.clearCompletedRow(i);
for (auto &movable : m_landedBlockStorage) {
movable->deleteBlocksAtRow(i);
}
m_vmatrix.lowerAllBlocksAboveRow(i);
dropUpperBlocks(i, m_vmatrix);
drawLandedBlocks();
m_vmatrix.printMatrix();
}
}
m_score += calculateScore(amountRows);
std::cout << m_score;
}
void Game::dropUpperBlocks(int row, VirtualMatrix &vmatrix)
{
for (auto& blockCompositions : m_landedBlockStorage) {
blockCompositions->dropBlocksIfHigher(row, vmatrix);
}
}
float Game::calculateScore(const float rows)
{
if (rows == 1)
return 20;
else
return (20 * rows) + ((20*rows)/100)*20;
}