-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
249 lines (206 loc) · 6.36 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
https://tetris.fandom.com/wiki/TGM_legend
https://tetris.fandom.com/wiki/Drop
https://strategywiki.org/wiki/Tetris/Controls
https://www.sfml-dev.org/tutorials/2.5/window-inputs.php
*/
#include "libs.hpp"
#include "tetris.hpp"
const bool useGrid = false;
void pad0(string& s, int len)
{
while (s.size() < len)
s.insert(s.begin(), '0');
}
void updateGame()
{
if (lost)
{
drawText("YOU LOST", fontSize, Vector2f(lostX, pausePos.y + fontSize * lostFontMul), Color::White, win);
// draw score
string s = to_string(score);
pad0(s, 6);
drawText("SCORE: "+s, fontSize, Vector2f(scorePos.x, pausePos.y + fontSize * (lostFontMul + 2)), Color::White, win);
// draw lines
s = to_string(lines);
pad0(s, 3);
drawText("LINES: "+s, fontSize, Vector2f(linesPos.x, pausePos.y + fontSize * (lostFontMul + 4)), Color::White, win);
// draw level
s = to_string(level);
pad0(s, 2);
drawText("LEVEL: "+s, fontSize, Vector2f(levelPos.x, pausePos.y + fontSize * (lostFontMul + 6)), Color::White, win);
// draw prompt
drawText("PRESS ENTER TO", fontSize, Vector2f(pressX, pausePos.y + fontSize * (lostFontMul + 9)), Color::White, win);
drawText("PLAY AGAIN", fontSize, Vector2f(linesPos.x, pausePos.y + fontSize * (lostFontMul + 11)), Color::White, win);
return;
}
// update game if timer > frame count (depending on if soft dropping)
if (!paused && frameTimer >= (softDrop ? softFrames[level] : frames[level]))
{
if (collisionCheck(0, 0))
tetPos = Vector2f(tetPos.x, tetPos.y - 1);
else
placeTet();
frameTimer = 0;
}
// draw board outline
RectangleShape board(boardSize);
board.setFillColor(Color::Black);
board.setOutlineThickness(2);
board.setOutlineColor(Color::White);
board.setPosition(boardPos);
win.draw(board);
// draw grid
if (useGrid)
{
RectangleShape line(Vector2f(1, tileSize * boardDim.y));
line.setFillColor(gridColor);
for (int x = 1; x < boardDim.x; ++x)
{
line.setPosition(Vector2f(boardPos.x + x * tileSize, boardPos.y));
win.draw(line);
}
line.setSize(Vector2f(tileSize * boardDim.x, 1));
for (int y = 1; y < boardDim.y; ++y)
{
line.setPosition(Vector2f(boardPos.x, boardPos.y + y * tileSize));
win.draw(line);
}
}
// draw score
string s = to_string(score);
pad0(s, 6);
drawText("SCORE: "+s, fontSize, scorePos, Color::White, win);
// draw lines
s = to_string(lines);
pad0(s, 3);
drawText("LINES: "+s, fontSize, linesPos, Color::White, win);
// draw level
s = to_string(level);
pad0(s, 2);
drawText("LEVEL: "+s, fontSize, levelPos, Color::White, win);
if (paused)
drawText("PAUSED", fontSize, pausePos, Color::White, win);
else
{
// draw tiles
for (int y = 0; y < boardDim.y; ++y)
for (int x = 0; x < boardDim.x; ++x)
if (tiles[y][x] != N)
{
int ypos = boardDim.y - y - 1;
tile.setFillColor(COLORS[tiles[y][x]]);
tile.setPosition(boardPos + Vector2f(tileSize * x, tileSize * ypos));
win.draw(tile);
}
// draw falling tet
drawTet(tetPos.x, tetPos.y, currentTet, rotation, true);
// draw next
drawTet(nextTetPos.x, nextTetPos.y, nextTet, displayRot, false);
// draw held
if (heldTet != N)
drawTet(heldTetPos.x, heldTetPos.y, heldTet, displayRot, false);
}
}
void init()
{
frameTimer = 0;
rotation = 0;
heldTet = N;
score = 0;
level = 0;
lines = 0;
usedHeld = false;
softDrop = false;
paused = false;
lost = false;
tetPos = spawnPos;
tile.setOutlineThickness(0);
win.setFramerateLimit(60);
// init array
tiles.clear();
for (int y = 0; y < boardDim.y; ++y)
{
vector<int> temp;
for (int x = 0; x < boardDim.x; ++x)
temp.push_back(N);
tiles.push_back(temp);
}
nextTet = rand() % N;
currentTet = rand() % N;
}
void input(int code)
{
if (lost)
{
if (code == Keyboard::Enter)
init();
return;
}
if (!paused)
{
if ((code == Keyboard::Up || code == Keyboard::W || code == Keyboard::X) && collisionCheck(1, 0))
// clockwise
++rotation;
else if (code == Keyboard::Z && collisionCheck(-1, 0))
// counterclockwise
rotation += 3;
else if ((code == Keyboard::Left || code == Keyboard::A) && collisionCheck(0, -1))
// move left
tetPos = Vector2f(tetPos.x - 1, tetPos.y);
else if ((code == Keyboard::Right || code == Keyboard::D) && collisionCheck(0, 1))
// move right
tetPos = Vector2f(tetPos.x + 1, tetPos.y);
else if (code == Keyboard::Space)
{
// hard drop (drop until collision)
while (collisionCheck(0, 0))
tetPos = Vector2f(tetPos.x, tetPos.y - 1);
placeTet();
}
else if (code == Keyboard::Down || code == Keyboard::S)
softDrop = true;
else if (code == Keyboard::C && !usedHeld)
{ // hold piece
if (heldTet != N)
{
int temp = heldTet;
heldTet = currentTet;
currentTet = temp;
}
else
{
heldTet = currentTet;
currentTet = nextTet;
nextTet = rand() % N;
}
tetPos = spawnPos;
rotation = 0;
usedHeld = true;
}
}
if (code == Keyboard::P || code == Keyboard::Escape)
paused = !paused;
}
int main()
{
srand(time(NULL)); // seed generator with time
Event event;
init();
while (win.isOpen())
{
softDrop = false;
while (win.pollEvent(event))
{
if (event.type == Event::KeyPressed)
input(event.key.code);
else if (event.type == Event::Closed)
win.close();
}
win.clear();
updateGame();
win.display();
++frameTimer;
}
return 0;
}