-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
334 lines (301 loc) · 12.2 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "main.h"
int main (void)
{
signal(SIGINT, exit_handler);
game.consoleHeight = getConsoleHeight();
game.consoleWidth = getConsoleWidth();
unsigned long long t_now=0, t_lastRender=0;
//random init
pcg32_srandom((uint64_t) time(NULL), (uint64_t) time(NULL)/2);
//GAME INIT
game.playAreaX = 10;
game.playAreaY = 21; //Actual game size in Tetris blocks
game.state = 1; //Main game loop state
game.blockFalling = 0; //Block is falling (if not, drop a new one!)
game.fallTick=0; //We can rotate faster, but drop only so fast!
game.blockNum=0; //Score tracking and color rotation
game.blockSequence=0; //Number defining next 7 blocks
game.fastDrop=false; //press s for the block to fall down fast
game.instantDrop=false; //space for instant drop
game.score=0;
game.fallRate=700; //delay length
game.level=0;
game.targetFPS=50;
game.cyclesPerFrame=0;
#if defined(__linux__)
initTermios(0);
#endif
#if defined(_WIN32) || defined(WIN32)
setvbuf(stdout, NULL, _IOLBF, 1); //attemt to fix Windows slow render...
SetConsoleOutputCP(CP_UTF8);
#endif
(void)setlocale(LC_ALL, "en_GB");
//BOARD INIT
char **board, **staticBoard;
board = malloc(game.playAreaY * sizeof(*board)); //allocate board in memory
staticBoard = malloc(game.playAreaY * sizeof(*staticBoard));
for (short i=0; i<game.playAreaY; i++)
{
board[i] = malloc(game.playAreaX * sizeof *board[i]);
staticBoard[i] = malloc(game.playAreaX * sizeof *staticBoard[i]);
}
for (short i=0; i<game.playAreaY; i++)
{
for (short j=0; j<game.playAreaX; j++)
{
board[i][j] = ' ';
staticBoard[i][j] = ' ';
}
}
///Main menu
#if defined(_WIN32) || defined(WIN32)
pprint(" ", "g", GetStdHandle(STD_OUTPUT_HANDLE));
#elif defined(__linux__)
pprint(" ", "g", 0);
#endif
game.level = mainMenu(game.consoleWidth, game.consoleHeight);
clearScreen();
//Default to biggest scaling
game.scaling=1;
game.consoleHeight = getConsoleHeight(); //Read them again
game.consoleWidth = getConsoleWidth();
//Windows is too slow to render it bigger
#if defined(__linux__)
while(((game.scaling+1)*game.playAreaY + 6 < game.consoleHeight) && ((game.scaling+1)*game.playAreaX + 6 < game.consoleWidth))
game.scaling++;
#endif
//Point[x,y] in console where to start drawing
game.mainWindowStartX = (unsigned short)((game.consoleWidth /2) - (game.playAreaX*game.scaling));
game.mainWindowStartY = (unsigned short)((game.consoleHeight/2) - (game.playAreaY*game.scaling/2));
//GAME LOOP
while(game.state==1)
{
if (!game.blockFalling)
{
//Drop new block
game.blockFalling = true;
game.blockNum++;
game.fastDrop = false;
game.instantDrop = false;
game.blockRotation = (unsigned short) pcg32_random();
blockShape = advanceSequence(&game);
switch (blockShape)
{
case shape_I: game.color='w'; break;
case shape_J: game.color='y'; break;
case shape_L: game.color='m'; break;
case shape_O: game.color='b'; break;
case shape_S: game.color='c'; break;
case shape_T: game.color='g'; break;
case shape_Z: game.color='r'; break;
default: break;
}
game.blockX = (short) ((game.playAreaX/2) - 1);
game.blockY = 0;
for (short i=0; i<game.playAreaY; i++)
for (short j=0; j<game.playAreaX; j++)
staticBoard[i][j] = board[i][j];
//Check full rows
int clearedRows = 0;
for(int y=game.playAreaY-1; y>0; y--)
{
int x=0;
while ((staticBoard[y][x] != ' ') && (x<game.playAreaX)) x++;
if (x==game.playAreaX) //Full row found
{
for(int Y=y; Y>0; Y--)
{
for (int X=0; X<game.playAreaX; X++)
staticBoard[Y][X]=staticBoard[Y-1][X];
}
for (int X=0; X<game.playAreaX; X++) staticBoard[0][X]=' ';
y++;
clearedRows++;
}
}
switch (clearedRows)
{
case 0: break;
case 1: game.score+=(game.level+1)*40; break;
case 2: game.score+=(game.level+1)*100; break;
case 3: game.score+=(game.level+1)*300; break;
case 4: game.score+=(game.level+1)*1200; break;
default: break;
}
}//new block
//Key press
if(!kbhit()) game.fastDrop = false;
else
{
game.pressedKey = parseKey();
switch(game.pressedKey)
{
case 'p': if (game.scaling<3) game.scaling++; goto reScale;
case 'm': if (game.scaling>1) game.scaling--; goto reScale;
case 's': game.fastDrop=true; break;
case ' ': game.instantDrop=true; break;
case 'w': //Rotate
{
bool rotate=true;
game.blockRotation++; //assume legal move
for(short y=0; y<4; y++)
{
for(short x=0; x<4; x++)
{
//check for collisions
if (blockPosition(&game, y, x) && ((x + game.blockX) < 0))
{
rotate = false;
break;
}
if(x+game.blockX>=game.playAreaX || y+game.blockY>=game.playAreaY)
{
rotate = false;
break;
}
if(staticBoard[game.blockY+y][game.blockX+x] != ' ')
{
rotate = false;
break;
}
}
}
if (!rotate) game.blockRotation--;//revert
} break;
case 'a': //Move left
{
bool moveX=true;
game.blockX--; //assume legal move
for(short y=0; y<4; y++)
{
for (short x = 0; x < 4; x++)
{
//check for collisions
if (blockPosition(&game, y, x) && (((x + game.blockX) < 0) || (x + game.blockX >= game.playAreaX) || (staticBoard[game.blockY+y][game.blockX+x] != ' ')))
moveX = false;
}
}
if (!moveX) game.blockX++;//revert
} break;
case 'd': //Move right
{
bool moveX=true;
game.blockX++; //assume legal move
for(short y=0; y<4; y++)
{
for (short x = 0; x < 4; x++)
{
//check for collisions
if (blockPosition(&game, y, x) && (((x + game.blockX) < 0) || (x + game.blockX >= game.playAreaX) || (staticBoard[game.blockY+y][game.blockX+x] != ' ')))
moveX = false;
}
}
if (!moveX) game.blockX--;//revert
} break;
#if defined(NDEBUG)
default: break;
#else
default: printf("%c", game.pressedKey); break;
#endif
//Non-reachable code unless you press rescale keys p/m
reScale:
game.consoleHeight = getConsoleHeight();
game.consoleWidth = getConsoleWidth();
game.mainWindowStartX = ((game.consoleWidth>>1) - (game.playAreaX*game.scaling)); //Point[x,y] in console where to start drawing
game.mainWindowStartY = ((game.consoleHeight>>1) - (game.playAreaY*game.scaling>>1));
clearScreen();
}
}//keypress
//Calculate dynamic block
if (game.fallTick>=game.fallRate || (game.fastDrop && game.fallTick>=25) || game.instantDrop)
{
//drop 1 down
game.blockY++;
game.fallTick=0;
}
//Making Board matrix from static and dynamic parts
///
drawBlocks:
///
for (short i=0; i<game.playAreaY; i++)
for (short j=0; j<game.playAreaX; j++)
board[i][j] = staticBoard[i][j];
for (short y=0; y<=game.playAreaY; y++)
{
for (short x=0; x<game.playAreaX; x++)
{
//radius of block
if (((x >= game.blockX) && (x < game.blockX+4)) && ((y >= game.blockY) && (y < game.blockY+4)))
{
//Check if there should be a block based on shape and position
if (blockPosition(&game, y-game.blockY, x-game.blockX))
{
//place the block
if ((staticBoard[y%game.playAreaY][x] == ' ') && (y != game.playAreaY))
{
board[y][x] = game.color;
}
else
{
//We've hit something
if(game.blockY==0) game.state=0;
else
{
if(game.instantDrop) game.fastDrop = false; //Only one bonus is applied
game.score+=(game.fastDrop+1)*(2*game.instantDrop+1)*(game.level+1)*4;
game.blockY--;
game.blockFalling=false;
goto drawBlocks; //re-draw
}
}
}
}
}
}
if(game.instantDrop && game.blockFalling)
{
game.blockY++;
goto drawBlocks; //continue dropping
}
if (game.state) ///Still not game over?
{
usElapsed(&t_now);
if(t_now >= t_lastRender+(1e6/(game.targetFPS*1.0))) ///Draw :)
{
drawBoard(board, &game);
game.fallTick += (unsigned)((t_now - t_lastRender)*( 0.9*pow(1.2, 1.4*game.level+1) )/1000.0);
t_lastRender = t_now;
game.cyclesPerFrame=0;
}
else game.cyclesPerFrame++;
#if defined(__linux__) //Windows too slow
msDelay(2); ///Let the CPU sleep for a bit
#endif
}
}//main game loop
///Game over
#if defined(__linux__)
exitTermios();
#endif
clearScreen();
printf("Game over! Thanks for playing.\n\nScore: %u\n", game.score);
printf("Enter your name: ");
char name[20];
fflush(stdin);
while(!scanf("%19[^\n]", name))
{
printf("Incorrect input\n");
printf("Enter your name: ");
fflush(stdin);
}
if(addScore(game.score, game.level, name)) printf("Error saving your score :(\n");
displayScoreBoard(15);
printf("\nPress 'q' to exit or 'r' to restart the game\n");
#if defined(__linux__)
initTermios(0); kbhit();
#endif
do game.pressedKey = getch();
while (!(game.pressedKey=='q' || game.pressedKey=='r'));
if(game.pressedKey=='r') main();
exit_handler(0);
}