-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris_main.c
232 lines (169 loc) · 3.75 KB
/
tetris_main.c
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
#include <stdlib.h>
//#include <time.h>
#include "ui.h"
#include "LCD.h"
#include "Tetris.h"
#include "keypad.h"
#include "lcdstring.h"
#include <util/delay.h>
#define KEY_UP 2
#define KEY_DOWN 10
#define KEY_LEFT 5
#define KEY_RIGHT 6
#define KEY_HARDDROP 9
#define KEY_PAUSE 1
#define DELAY_KEY 3
static bool pause = false; // 游æˆæš‚åœ
static uint8_t level = 1; // 级别
static uint16_t lines = 0; // 消除的行数
static uint16_t score = 0; // 分数
static uint16_t time_count = 0;
static uint8_t delay = 0;
void game_over(void)
{
ui_print_game_over();
return;
}
void game_info_update(void)
{
ui_print_level(level);
ui_print_score(score);
return;
}
void get_preview_brick(const void *info)
{
uint16_t dat = *((uint16_t *)info);
ui_print_preview(dat);
return;
}
void draw_box(uint8_t x, uint8_t y, uint8_t color)
{
if (color == 0)
ui_draw_box(x, y, false);
else
ui_draw_box(x, y, true);
return;
}
uint8_t random_num(void)
{
return (uint8_t)rand();
}
void get_remove_line_num(uint8_t line)
{
lines += line;
switch (line)
{
case 1:
score += 10;
break;
case 2:
score += 25;
break;
case 3:
score += 45;
break;
case 4:
score += 80;
break;
default:
break;
}
level = lines / 25 + 1;
return;
}
void game_pause(void)
{
pause = !pause;
if (pause)
ui_print_game_pause();
else
tetris_sync_all(); // å› ä¸ºæ‰“å°æš‚åœç ´å了地图区显示
// 所以退出时è¦åˆ·æ–°æ•´ä¸ªåœ°å›¾åŒº
return;
}
void game_run(void)
{
uint16_t key;
static bool refresh = false;
uint8_t i;
//delayMS(50);
_delay_ms(30);
if (!pause)
time_count++;
// 级别越高速度越快
if (time_count >= (12 - level))
{
time_count = 0;
refresh = true;
tetris_move(dire_down);
}
//refresh = true;
key = KP_scan();
if (delay > 0)
--delay;
if (key != 0 && delay == 0)
{
delay = DELAY_KEY;
// æš‚åœæ—¶åªå“应回车键
if (pause && key != KEY_PAUSE)
return;
switch (key)
{
case KEY_UP:
tetris_move(dire_rotate);
break;
case KEY_DOWN:
tetris_move(dire_down);
break;
case KEY_LEFT:
tetris_move(dire_left);
break;
case KEY_RIGHT:
tetris_move(dire_right);
break;
case KEY_HARDDROP:
while (tetris_move(dire_down));
break;
case KEY_PAUSE:
game_pause();
_delay_ms(100);
break;
default:
break;
}
refresh = true;
}
if (refresh)
{
refresh = !refresh;
tetris_sync();
// 更新行数, 分数ç‰ä¿¡æ¯
lcd_refresh();
game_info_update();
}
return;
}
int main(void)
{
// éšæœºæ•°ç§å
// srand((int32_t)time(NULL));
ui_init();
KP_Init();
PORTB = 0xFF;
DDRB = 0x00;
tetris_init(&draw_box, &random_num, &get_preview_brick, &get_remove_line_num);
lcd_clr_scr();
lcd_clr_scr();
lcd_paint_scr(title);
while (KP_scan() == 0) ;
lcd_paint_scr(game);
//game_pause();
while (!tetris_is_game_over())
{
game_run();
}
game_over();
// 按回车退出
//while (getch() != 13);
return 0;
}