-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
209 lines (189 loc) · 4.33 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
//1. Create Game Window using FSML ... Clear!
//2. Draw a cell ... Clear!
//3. Draw Tetris blocks ... Clear!
//4. move left, right and down ... Clear!
//5. Collisiton detection(ok,ok), Draw World(ok), Timer(ok), Next block(ok) ... Clear!
//6. block rotation ... Clear!
//7. Space bar(ok) and line clear ... Clear!
//Done
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
int block[7][4][4] =
{
1,0,0,0,
1,0,0,0,
1,0,0,0,
1,0,0,0,
1,0,0,0,
1,1,0,0,
0,1,0,0,
0,0,0,0,
0,1,0,0,
1,1,0,0,
1,0,0,0,
0,0,0,0,
1,1,0,0,
1,1,0,0,
0,0,0,0,
0,0,0,0,
1,0,0,0,
1,1,0,0,
1,0,0,0,
0,0,0,0,
1,0,0,0,
1,0,0,0,
1,1,0,0,
0,0,0,0,
0,1,0,0,
0,1,0,0,
1,1,0,0,
0,0,0,0,
};
const Color color_map[] = {
Color::Green, Color::Blue, Color::Red, Color::Yellow,
Color::White, Color::Magenta, Color::Cyan
};
const int cell_size = 40;
const int w_cnt = 10;
const int h_cnt = 20;
int world[h_cnt][w_cnt] = { 0 };
int main(void)
{
RenderWindow window(VideoMode(w_cnt*cell_size, h_cnt*cell_size), "Tetris");
RectangleShape cell(Vector2f(cell_size, cell_size));
int kind; // block kind
int cx; // current x position of block
int cy;
auto new_block = [&]()
{
kind = rand() % 7, cx = w_cnt / 2, cy = 0;
};
new_block();
auto check_block = [&]()
{
for (int y = 0; y < 4; y++)for (int x = 0; x < 4; x++)
{
if (block[kind][y][x] == 0) continue;
if (x + cx < 0 || x + cx >= w_cnt || y + cy >= h_cnt) return false; // hit wall
if (world[cy + y][cx + x]) return false; // collision with world blocks
}
return true;
};
auto clear_lines = [&]()
{
int to = h_cnt - 1;
//from bottom line to top line...
for (int from = h_cnt - 1; from >= 0; from--)
{
int cnt = 0;
for (int x = 0; x < w_cnt; x++)if (world[from][x])cnt++;
//if current line is not full, copy it(survived line)
if (cnt < w_cnt)
{
for (int x = 0; x < w_cnt; x++)world[to][x] = world[from][x];
to--;
}
//otherwise it will be deleted(clear the line)
}
};
auto go_down = [&]()
{
cy++;
if (check_block() == false) // hit bottom
{
cy--;
for (int y = 0; y < 4; y++)for (int x = 0; x < 4; x++)
if (block[kind][y][x])
{
world[cy + y][cx + x] = kind + 1;//+1 for avoiding 0
}
clear_lines();
//start next block
new_block();
return false;
}
return true;
};
auto rotate = [&]()
{
int len = 0; // check rotation block size
for (int y = 0; y < 4; y++)for (int x = 0; x < 4; x++)
if (block[kind][y][x]) len = max(max(x, y) + 1, len);
int d[4][4] = { 0 };
//rotate conter-clock wise 90 degree
for (int y = 0; y < len; y++)for (int x = 0; x < len; x++)
if (block[kind][y][x]) d[len - 1 - x][y] = 1;
for (int y = 0; y < 4; y++)for (int x = 0; x < 4; x++)
block[kind][y][x] = d[y][x];
};
Clock clock;
while (window.isOpen())
{
static float prev = clock.getElapsedTime().asSeconds();
if (clock.getElapsedTime().asSeconds() - prev >= 0.5)
{
prev = clock.getElapsedTime().asSeconds();
go_down();
}
Event e;
while (window.pollEvent(e))
{
if (e.type == Event::Closed) window.close();
if (e.type == Event::KeyPressed)
{
if (e.key.code == Keyboard::Left)
{
cx--;
if (check_block() == false) cx++;
}
else if (e.key.code == Keyboard::Right)
{
cx++;
if (check_block() == false) cx--;
}
else if (e.key.code == Keyboard::Down)
{
go_down();
}
else if (e.key.code == Keyboard::Up)
{
rotate();
if (check_block() == false) { rotate(), rotate(), rotate(); }
}
else if (e.key.code == Keyboard::Space)
{
while (go_down() == true);
}
}
}
window.clear();
auto draw_world = [&]()
{
for (int y = 0; y < h_cnt; y++)for (int x = 0; x < w_cnt; x++)
if (world[y][x])
{
cell.setFillColor(color_map[world[y][x] - 1]);
cell.setPosition(Vector2f(x*cell_size, y*cell_size));
window.draw(cell);
}
};
draw_world();
// define c++11 lambda function
// this function can use all the "outside" variables like kind, cx, cy!
auto draw_block = [&]()
{
cell.setFillColor(color_map[kind]);
for (int y = 0; y < 4; y++)for (int x = 0; x < 4; x++)
if (block[kind][y][x])
{
cell.setPosition(Vector2f((cx + x)*cell_size, (cy + y)*cell_size));
window.draw(cell);
}
};
//call the lambda function above
draw_block();
window.display();
}
return 0;
}