-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
233 lines (197 loc) · 7.44 KB
/
main.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
// Simple and Fast Multimedia Library
#include <SFML/Graphics.hpp>
#include "grid.hpp"
#include <iostream>
// Define world parameters
const int CellSize = 20;
const int borderSize = 2;
// Define world
namespace
{
void createWindowAndGame(sf::RenderWindow &window, tetris::GameBoard &game)
{
sf::VideoMode desktop = sf::VideoMode::getDesktopMode();
// Define the ASCII art for each letter as vector of strings
std::vector<std::string> tetrisArt = {
" _______ ______ _______ _____ _____ _____ ",
" |__ __| ____|__ __| __ \\_ _|/ ____| ",
" | | | |__ | | | |__) | | | | (___ ",
" | | | __| | | | _ / | | \\___ \\ ",
" | | | |____ | | | | \\ \\ _| |_ ____) | ",
" |_| |______| |_| |_| \\_\\_____|_____/ "};
// Print each line of ASCII art
for (const std::string &line : tetrisArt)
{
std::cout << line << std::endl;
}
std::cout << "Welcome to tetris_cpp!" << std::endl;
std::cout << "Please select your difficulty" << std::endl;
bool valid_conditions = false;
std::string game_type;
int height;
int width;
do
{
std::cout << "Enter game type (E for easy, M for medium, H for hard): ";
std::cin >> game_type;
if (game_type.size() >= 1)
{
char game_choice = tolower(game_type[0]);
switch (game_choice)
{
case 'e':
window.create(sf::VideoMode(15 * CellSize, 25 * CellSize), "Tetris");
width = 15;
height = 25;
valid_conditions = true;
break;
case 'm':
width = 10;
height = 20;
valid_conditions = true;
break;
case 'h':
window.create(sf::VideoMode(7 * CellSize, 15 * CellSize), "Tetris");
width = 7;
height = 15;
valid_conditions = true;
break;
default:
std::cout << "Invalid game type. Please enter E, M, or H.\n";
// Introduce a delay to avoid continuous looping and reduce lag
}
}
else
{
std::cout << "Invalid input. Please enter a valid game type.\n";
// Introduce a delay to avoid continuous looping and reduce lag
}
} while (!valid_conditions);
window.create(sf::VideoMode(width * CellSize, height * CellSize), "Tetris");
game = tetris::GameBoard(height, width);
int windowPosX = (desktop.width - width * CellSize) / 2;
int windowPosY = (desktop.height - height * CellSize) / 2;
window.setPosition(sf::Vector2i(windowPosX, windowPosY));
}
void drawCellWithBorder(sf::RenderWindow &window, int x, int y, const sf::Color &color)
{
sf::RectangleShape cell(sf::Vector2f(CellSize, CellSize));
cell.setFillColor(color);
cell.setPosition(sf::Vector2f(x * CellSize, y * CellSize));
window.draw(cell);
// Draw the border
sf::RectangleShape borderRect;
borderRect.setSize(sf::Vector2f(CellSize, CellSize));
borderRect.setFillColor(sf::Color::Black); // Border color
borderRect.setPosition(x * CellSize, y * CellSize);
// Draw additional rectangles for the border (top, left, right, bottom)
borderRect.setSize(sf::Vector2f(CellSize, 2));
window.draw(borderRect);
borderRect.setSize(sf::Vector2f(borderSize, CellSize + 2 * 2));
window.draw(borderRect);
borderRect.setPosition((x + 1) * CellSize - borderSize, y * CellSize - borderSize);
window.draw(borderRect);
borderRect.setSize(sf::Vector2f(CellSize, borderSize));
borderRect.setPosition(x * CellSize, (y + 1) * CellSize - borderSize);
window.draw(borderRect);
}
}
int main()
{
// a window that can render 2D drawings
sf::RenderWindow window;
// draw a cell in the world
sf::RectangleShape cell(sf::Vector2f(CellSize, CellSize));
tetris::GameBoard game;
// populate a block
createWindowAndGame(window, game);
game.generate_new_piece();
bool gameOver = false;
sf::Clock clock;
while (window.isOpen() && !gameOver)
{
// start clock
static float prev = clock.getElapsedTime().asSeconds();
if (clock.getElapsedTime().asSeconds() - prev >= 0.5)
{
prev = clock.getElapsedTime().asSeconds();
game.move_down();
}
// Define system event
sf::Event e;
// polling event (eg. key pressed)
while (window.pollEvent(e))
{
// close window
if (e.type == sf::Event::Closed)
window.close();
// keyboard interrupt
if (e.type == sf::Event::KeyReleased)
{
if (e.key.code == sf::Keyboard::Left or e.key.code == sf::Keyboard::A)
{
--game.b_x;
if (!game.in_bounds())
++game.b_x;
}
else if (e.key.code == sf::Keyboard::Right or e.key.code == sf::Keyboard::D)
{
++game.b_x;
if (game.in_bounds() == false)
--game.b_x;
}
else if (e.key.code == sf::Keyboard::Down or e.key.code == sf::Keyboard::S)
{
game.move_down();
}
else if (e.key.code == sf::Keyboard::Space)
{
// fall down until reaches the bottom
while (game.move_down() == true);
}
else if (e.key.code == sf::Keyboard::Up or e.key.code == sf::Keyboard::W)
{
game.rotate();
// if rotation hits boundary, do not allow to rotate
if (!game.in_bounds())
{
game.rotate();
game.rotate();
game.rotate();
}
}
}
}
// clear window every frame
window.clear();
for (int y = 0; y < game.getHeight(); ++y)
{
for (int x = 0; x < game.getWidth(); ++x)
{
int cellValue = game.getGameState()[y][x];
if (cellValue)
{
drawCellWithBorder(window, x, y, tetris::colors.at(cellValue));
}
}
}
for (int y = 0; y < 4; ++y)
{
for (int x = 0; x < 4; ++x)
{
if (game.get_current_shape()[y][x])
{
int drawX = game.b_x + x;
int drawY = game.b_y + y;
drawCellWithBorder(window, drawX, drawY, tetris::colors.at(game.getBlock()));
}
}
}
// display rendered object on screen
window.display();
gameOver = game.is_game_over();
}
std::cout << "Your Score was " << game.get_score() << std::endl;
std::cout << "You cleared " << game.lines_cleared_count() << " line(s)" << std::endl;
return 0;
}