/
tetris.c
266 lines (229 loc) · 5.61 KB
/
tetris.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
#include "tetris.h"
world_data world;
tetromino current_tetromino;
unsigned long tick_time;
void init_game(){
world.points = 0;
world.lines_removed = 0;
world.top_line = WORLD_HEIGHT;
for(int x = 0; x < WORLD_WIDTH; x++){
for(int y = 0; y < WORLD_HEIGHT; y++){
world.field[x][y] = MAKE_INVISIBLE(0);
}
}
fill_tetromino(¤t_tetromino,rand()%7);
tick_time = 700000;
}
void game_loop(int * input_key){
//init game data
init_game();
//control data
struct timeval *begin, *end, *tmp;
unsigned long diff;//in us
int key = 0; //for temporary storage of input_key, b/c of race conditions
int reached_bottom = 0;
begin=malloc(sizeof(struct timeval));
end=malloc(sizeof(struct timeval));
gettimeofday(begin, NULL);
diff = 0;
draw();
while(1){
//input
key=*input_key;
if(key != -1){
*input_key = -1;//input read->reset
switch(key){//TODO move block
case KEY_DOWN:
reached_bottom = go_down(¤t_tetromino, &world);
break;
case KEY_LEFT:
if(!reached_bottom){
go_left(¤t_tetromino, &world);
}
break;
case KEY_RIGHT:
if(!reached_bottom){
go_right(¤t_tetromino, &world);
}
break;
case 'x':
if(!reached_bottom){
rotate(¤t_tetromino, &world, 1);
}
break;
case 'c':
if(!reached_bottom){
rotate(¤t_tetromino, &world, 0);
}
break;
case 27: /*ESC*/
switch(menu(input_key)){
case 0:break;//continue game
case 1:init_game();break;//new game
case -1://quit
free(begin);
free(end);
return;
default:break;
}
gettimeofday(begin, NULL);
break;
case 32: /*Space*/
while(!go_down(¤t_tetromino, &world));
reached_bottom=1;
diff=tick_time;
break;
case -2:
free(begin);
free(end);
return;
default: break;
}
}
//game tick & time
gettimeofday(end, NULL);
diff+=(end->tv_sec-begin->tv_sec)*1000000 + end->tv_usec-begin->tv_usec;
tmp=begin;
begin=end;
end=tmp;
if(diff >= tick_time){
diff -= tick_time;
tick(&reached_bottom);
}
//gui update
draw();
//sleep
usleep(50*1000);
}
}
void draw(){
clear();
draw_base();
draw_world();
draw_block();
refresh();
}
void draw_base(){
move(0,0);
attrset(C(PURE_WHITE));
printw("Tetris - Points: %d, %d lines removed", world.points, world.lines_removed);
attrset(C(E_WHITE));
move(BOARD_START_Y,BOARD_START_X);
printw(" ");//(WORLD_WIDTH+2)*2 spaces, +2 because of border, *2 because of double block size
move(1+BOARD_START_Y+WORLD_HEIGHT,BOARD_START_X);
printw(" ");
for(int i = BOARD_START_Y+1; i < WORLD_HEIGHT+BOARD_START_Y+1; i++){
mvprintw(i,BOARD_START_X," ");
mvprintw(i, WORLD_WIDTH*2 + BOARD_START_X+2," ");
}
}
void draw_world(){
int block;
for(int x = 0; x < WORLD_WIDTH; x++){
for(int y = 0; y < WORLD_HEIGHT; y++){
block = world.field[x][y];
if( VISIBLE(block) ){
attrset( C(COLOR(block)) );
mvprintw(BOARD_START_Y+1+y, BOARD_START_X+2+x*2, " ");
}
}
}
}
void draw_block(){
if(current_tetromino.visible){
//startx/y
int sx = current_tetromino.x;
int sy = current_tetromino.y;
int block;
move(sy,sx);
for(int x = 0; x < current_tetromino.width; x++ ){
for(int y = 0; y < current_tetromino.height; y++ ){
block = current_tetromino.field[x][y];
if( VISIBLE(block) ){
attrset( C(COLOR(block)) );
mvprintw(BOARD_START_Y+1+y+sy, BOARD_START_X+2+(x+sx)*2, " ");
}
}
}
}
}
void tick(int * reached_bottom){
static int filled_lines[4];
static int counter = 0;
if(*reached_bottom){
//new piece reached the bottom or gravity activated and things need to be rechecked
*reached_bottom = 0;
if(counter == 0){//only if call is not coming from gravity stuff
write_to_world(¤t_tetromino, &world);
}
counter = 0;
//check for lines right here & gravity
for(int i = WORLD_HEIGHT-1; i >= world.top_line; i--){
if(check_line(i)){
fill_line(i);
filled_lines[counter] = i;
counter++;
}
}
world.points += 100*counter*counter;
if(counter == 0){
fill_tetromino(¤t_tetromino,rand()%7);
if(check_collision(¤t_tetromino, &world)){
//GAME OVER
init_game();
}
}else{
current_tetromino.visible = 0;//dont display it while removing lines
}
}else if(counter != 0){
//lines to remove, gravity to activate
//remove from top to bottom
for(int i = counter-1; i >= 0; i--){
remove_line(filled_lines[i]);
}
set_tick_time();
*reached_bottom = 1;
}else{
//just a normal turn, m8
*reached_bottom = go_down(¤t_tetromino, &world);//in the next tick: block is placed
}
}
int check_line(int line){
int block;
for(int i = 0; i < WORLD_WIDTH; i++){
block = world.field[i][line];
if(!VISIBLE(block)){
return 0;
}
}
return 1;
}
void fill_line(int line){
for(int i = 0; i < WORLD_WIDTH; i++){
world.field[i][line] = MAKE_VISIBLE(E_WHITE);
}
}
void remove_line(int start_line){
//bottom to top: copy line above to current one
for(int line = start_line; line > world.top_line; line--){
for(int i = 0; i < WORLD_WIDTH; i++){
world.field[i][line] = world.field[i][line-1];
}
}
for(int i = 0; i < WORLD_WIDTH; i++){
world.field[i][world.top_line] = MAKE_INVISIBLE(1);
}
world.top_line++;
world.lines_removed++;
}
void set_tick_time(){
if(world.lines_removed >= 100){
tick_time = 50000;
}else if(world.lines_removed >= 50){
tick_time = 100000;
}else if(world.lines_removed >= 25){
tick_time = 400000;
}else if(world.lines_removed >= 10){
tick_time = 600000;
}
}