-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
274 lines (249 loc) · 6.67 KB
/
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
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
274
/*
* main.c (1.0)
* 游戏主文件
*
* By Eric-JR Chen
* diamont1001@163.com
* 2011-3-4
*/
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
//游戏资源
const char *BG = "▓";//"■"; //背景图案
const char *BODY = "□";//"▓"; //方块图案
static STATUS sys_gs; //游戏状态
static int sys_level = 1; //等级(1-9)
static DIRECTION sys_dir; //控制走向
//控制屏幕刷新
static int JR_isRefresh = 0;
//纹理缓存
static SHOWNODE showBuff[ROW*COL*2];
static int BuffIndex = 0;
/* 将一个 NODE放到纹理缓冲区 */
void showInBuff(SHOWNODE buffer){
showBuff[BuffIndex].x = buffer.x;
showBuff[BuffIndex].y = buffer.y;
strcpy(showBuff[BuffIndex].text, buffer.text);
BuffIndex ++;
}
/* SHOW WELCOME */
void showWelcome(void){
printf("\n\n");
printf("\t\t************ JR_Tetris 1.0.1 *************\n");
printf("\t\t* Welcome to Tetris Gameing. *\n");
printf("\t\t* By Eric-JR Chen 2011-3 *\n");
printf("\t\t* diamont1001@163.com *\n");
printf("\t\t* 【Enter】 Start *\n");
printf("\t\t* 【 Esc 】 Esc *\n");
printf("\t\t******************************************\n");
}
/* 1 : 渲染当前block到buff
* 0 : 擦除当前block
*/
void showBlock(int n){
int i = 0, j = 0;
//移位查看SHAPE
for(i=0; i<2; i++)
for(j=0; j<8; j++)
if(BLOCK_PATTERN[curBlock.index].box[i] & (0x80>>j)){
SHOWNODE node;
node.x = curBlock.x + (j%4) * 2;
node.y = curBlock.y + (j/4) + 2*i;
if(n){
strcpy(node.text, BODY);
}else{
strcpy(node.text, BG);
}
showInBuff(node);
}
}
/* 显示下一个BLOCKS */
void showNextBlock(int x, int y){
int i = 0, j = 0;
JR_SetCursor(x, y);
printf("Next : \t\t");
JR_SetCursor(x, y+1);
printf("\t\t\t");
JR_SetCursor(x, y+2);
printf("\t\t\t");
JR_SetCursor(x, y+3);
printf("\t\t\t");
//移位查看SHAPE
for(i=0; i<2; i++)
for(j=0; j<8; j++)
if(BLOCK_PATTERN[nextBlock.index].box[i] & (0x80>>j)){
SHOWNODE node;
node.x = x + 8 + (j%4)*2;
node.y = y + i*2 + j/4;
strcpy(node.text, BG);
showInBuff(node);
}
}
/* 显示其他提示 */
void showTips(){
int x = COL * 2 + 1;
int y = 5;
JR_SetCursor(x, y++);
printf("Level: %d", sys_level);
JR_SetCursor(x, y++);
printf("Score: %d", TotalScore);
JR_SetCursor(x, y++);
switch(sys_gs){
case GAMING:
printf("status: GAMING\t");
break;
case PAUSE:
printf("status: PAUSE\t");
break;
case DIE:
printf("status: GAME OVER\t");
break;
case STOP:
printf("status: STOPED\t");
break;
}
}
void setGameSpeed(void){
stopTimer();
setTimer(500 - sys_level*40);
startTimer();
}
/* 更新分数 */
void UpdateScore(void){
int level = 1 + TotalScore / 5000; //5000分一个LEVEL
sys_level = level < 10 ? level : 9; //一共9个LEVEL
setGameSpeed();
}
/* 方块向下走一格,否则重新生成新的方块 */
void UpdateBlocks(){
if(!canMoveTo(DOWN)){
if(!checkResult()){
sys_gs = DIE;
}else{ //生成新的方块
getBlocks(-1);
showNextBlock(COL*2+1, 1);
}
UpdateScore();
showTips();
}else { //blocks向下走一格
showBlock(0);
setBlocks(DOWN);
showBlock(1);
}
sys_dir = NONE;
}
/* 屏幕渲染 */
void Render(){
int i = 0;
JR_isRefresh = 0;
switch(sys_gs){
case WELCOME:
showWelcome();
break;
case GAMING:
for(i=0; i<BuffIndex; i++){
JR_SetCursor(showBuff[i].x, showBuff[i].y);
printf("%s", showBuff[i].text);
}
BuffIndex = 0;
break;
case DIE:
sys_gs = STOP;
break;
case PAUSE:
JR_SetCursor(0, ROW);
printf("Pause! Press 【Enter】 to Continue.\t\t\t\t");
break;
case STOP:
JR_SetCursor(0, ROW);
printf("Game over! Press 【Enter】 to Restart or 【Esc】 to Esc.\t\t");
break;
}
JR_SetCursor(0, ROW+1);
}
/* 初始化游戏数据,开始游戏 */
void startGame(void){
BuffIndex = 0;
BLOCKS_TOP = ROW - 1;
sys_gs = GAMING;
sys_dir = NONE;
sys_level = 1;
TotalScore = 0;
setGameSpeed();
initBG();
DrawBG();
initBlocks();
showBlock(1);
showNextBlock(COL*2+1, 1);
showTips();
JR_isRefresh = 1;
JR_SetCursor(0, ROW);
printf("\t\t\t\t\t\t\t");
}
void init(void){
sys_gs = WELCOME;
JR_isRefresh = 1;
}
void UpdateKeys(int ms){
static int ticks = 0;
int t = JR_GetTicks();
if(t - ticks < ms) return ;
ticks = t;
if(JR_AnyKeys()){
sys_dir = NONE;
if(JR_IsKeyDown(JR_KEY_ESC)){ //Esc
sys_gs = STOP;
exit(0);
}else if(JR_IsKeyDown(JR_KEY_ENTER)){ //Pause,Resume
if(sys_gs == WELCOME || sys_gs == STOP){
startGame();
}else if(sys_gs == GAMING){
sys_gs = PAUSE;
showTips();
}else if(sys_gs == PAUSE){
JR_SetCursor(0, ROW);
printf("\t\t\t\t\t\t\t");
sys_gs = GAMING;
showTips();
}
}else if((sys_gs == GAMING) && (JR_IsKeyDown(JR_KEY_UP) || JR_IsKeyDown('w') || JR_IsKeyDown('W'))){ //变形
showBlock(0); //擦除原来blocks
changeBlocks();
showBlock(1); //重画
}else if((sys_gs == GAMING) && (JR_IsKeyDown(JR_KEY_DOWN) || JR_IsKeyDown('s') || JR_IsKeyDown('S'))){ //下
UpdateBlocks();
showBlock(1);
}else if((sys_gs == GAMING) && (JR_IsKeyDown(JR_KEY_LEFT) || JR_IsKeyDown('a') || JR_IsKeyDown('A'))){ //左
sys_dir = LEFT;
showBlock(0);
setBlocks(sys_dir);
showBlock(1);
}else if((sys_gs == GAMING) && (JR_IsKeyDown(JR_KEY_RIGHT) || JR_IsKeyDown('d') || JR_IsKeyDown('D'))){ //右
sys_dir = RIGHT;
showBlock(0);
setBlocks(sys_dir);
showBlock(1);
}
JR_isRefresh = 1;
}
}
/* 每个interval更新一次 */
void mainTimer(void){
if(sys_gs == GAMING){
UpdateBlocks(); //处理自动下跌情况
JR_isRefresh = 1;
}
}
int main()
{
init();
while(1){
UpdateKeys(11);
if(JR_isRefresh){ //按需刷新屏幕
Render();
}
ontimer();
}
return 0;
}