-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtetris.cpp
More file actions
273 lines (252 loc) · 6.03 KB
/
Copy pathtetris.cpp
File metadata and controls
273 lines (252 loc) · 6.03 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "common.h"
#include "view.h"
#include "main.h"
// scoreはglobal変数
int score = 0;
typedef struct
{
int x;
int y;
block b;
} active_block;
int freq=100;
void move_block();
void rotate_block(active_block, int);
void block_down();
void generateblock();
int check_collision(active_block);
void switch_block(active_block, int, int);
void delete_line();
void drop_blocks(int n);
void game_over();
int is_can_delete(int n);
active_block active;
block next;
board_element board[BOARD_HEIGHT][BOARD_WIDTH];
block blocks[8] = {
{{{0, 0}, {0, 0}, {0, 0}, {0 ,0}}, COLOR_WHITE}, // null
{{{0, 0}, {0, -1}, {0, 1}, {0 ,2}}, COLOR_GREEN}, // tetris
{{{0, 0}, {0, -1}, {0, 1}, {1 ,1}}, COLOR_GREENYELLOW}, // L1
{{{0, 0}, {0, -1}, {0, 1}, {-1,1}}, COLOR_NAVY}, // L2
{{{0, 0}, {0, -1}, {1, 0}, {1 ,1}}, COLOR_ORANGE}, // key1
{{{0, 0}, {0, -1}, {-1,0}, {-1,1}}, COLOR_YELLOW}, // key2
{{{0, 0}, {0, 1}, {1, 0}, {1 ,1}}, COLOR_RED}, // square
{{{0, 0}, {0, -1}, {1, 0}, {-1 ,0}}, COLOR_BLUE} // T
};
// テトリスの状態を初期化する.
void init_tetris()
{
score = 0;
freq = INIT_FREQ;
int i, n;
// 全部消す
for (i = 0; i < BOARD_HEIGHT; i++)
{
for (n = 0; n < BOARD_WIDTH; n++)
{
board[i][n].flg = 0;
board[i][n].color = COLOR_BLACK;
}
}
// 左の壁を作る
for(i=0; i<BOARD_HEIGHT; i++)
{
board[i][0].flg = 1;
}
// 右の壁を作る
for(i=0; i<BOARD_HEIGHT; i++)
{
board[i][BOARD_WIDTH-1].flg = 1;
}
// 下の壁を作る
for(i=0; i<BOARD_WIDTH; i++)
{
board[BOARD_HEIGHT-1][i].flg = 1;
}
next = blocks[rand()%7+1];
generateblock();
}
// テトリスを動かす. 毎フレーム呼ばれる.
void tetris_update()
{
move_block();
if(cnt % freq == 0)
block_down();
assign_block(board);
}
// ブロックを下へ移動させる
void block_down()
{
int i;
// 今のブロックを消しておく(衝突判定に含まれないように)
int x,y;
switch_block(active, 0, COLOR_BLACK);
// 一旦下に落とす
active.y++;
// ぶつかってないかの判定
if(!check_collision(active))
{
// 新しいブロックを置く
switch_block(active, 1, active.b.color);
}
else
{
// 元に戻す
active.y--;
switch_block(active, 1, active.b.color);
delete_line();
generateblock();
}
}
// キーボードの入力に合わせてブロックを移動させる
void move_block()
{
if(button[BUTTON_ROTATE]==1)
{
switch_block(active, 0, COLOR_BLACK);
rotate_block(active, 1);
if(check_collision(active))
rotate_block(active, 0);
switch_block(active, 1, active.b.color);
}
if(button[BUTTON_LEFT]==1)
{
switch_block(active, 0, COLOR_BLACK);
active.x--;
if(check_collision(active))
active.x++;
switch_block(active, 1, active.b.color);
}
if(button[BUTTON_RIGHT]==1)
{
switch_block(active, 0, COLOR_BLACK);
active.x++;
if(check_collision(active))
active.x--;
switch_block(active, 1, active.b.color);
}
}
// 消すべき行があれば消して,ブロックを一つずつづらす
void delete_line()
{
int i;
for(i=4; i< BOARD_HEIGHT-1; i++)
{
if(is_can_delete(i))
{
drop_blocks(i);
// スコアを足して,落ちる速さも大きくしてみる.
score += 100;
if(freq > 5)
freq -= 2;
}
}
}
// n行目を消し,上のブロックを落とす
void drop_blocks(int n)
{
int x,y;
for(x=1; x<BOARD_WIDTH-1; x++)
{
board[n][x].flg = 0;
}
for(y=n; 4<y; y--)
{
for(x=1; x<BOARD_WIDTH-1; x++)
{
board[y][x].flg = board[y-1][x].flg;
board[y][x].color = board[y][x].color;
}
}
}
// n行目は消すべきかどうかを判断する
int is_can_delete(int n)
{
int i;
for(i=1; i<BOARD_WIDTH-1; i++)
{
if(!board[n][i].flg)
return 0;
}
return 1;
}
// ブロックを回転させる clockwise=1で時計回り,0で反時計回り
void rotate_block(active_block b, int clockwise)
{
int i,x,y;
for(i=0; i<BLOCK_NUM; i++)
{
if(clockwise==1)
{
x = active.b.child[i].x;
y = active.b.child[i].y;
active.b.child[i].x = -y;
active.b.child[i].y = x;
}
else if(clockwise==0)
{
x = active.b.child[i].x;
y = active.b.child[i].y;
active.b.child[i].x = y;
active.b.child[i].y = -x;
}
}
}
// ブロックのフラグと色を一括して変更する
void switch_block(active_block a,int flg, int color)
{
int i, x, y;
for(i=0; i<BLOCK_NUM; i++)
{
x = active.x+active.b.child[i].x;
y = active.y+active.b.child[i].y;
board[y][x].flg = flg;
board[y][x].color = color;
}
}
// 何かにぶつかってるかの判定 ぶつかってたら1を返す
int check_collision(active_block a)
{
int i, x, y;
for(i=0; i<BLOCK_NUM; i++)
{
x = active.x+active.b.child[i].x;
y = active.y+active.b.child[i].y;
// なにかがあるとき
if(board[y][x].flg)
{
return 1;
}
}
return 0;
}
// 新しくブロックを作る
void generateblock()
{
active.x = 5;
active.y = 3;
active.b = next;
if(check_collision(active))
{
game_over();
return;
}
next = blocks[rand()%7+1];
assign_next_block(next);
}
// ゲームオーバー時. 全てのブロックを白くする.
void game_over()
{
switch_state(GAMEOVER);
int x,y;
for(y=0; y<BOARD_HEIGHT; y++)
{
for(x=0; x<BOARD_WIDTH; x++)
{
if(board[y][x].flg)
{
board[y][x].color = COLOR_WHITE;
}
}
}
}