-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtetris.c
230 lines (197 loc) · 6.09 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
#include <conio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include "tetris.h"
const int tetrominoes[7][16] = {
{0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, // I
{0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0}, // O
{0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0}, // S
{0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0}, // Z
{0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0}, // T
{0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0}, // L
{0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0} // J
};
int arena[A_HEIGHT][A_WIDTH];
uint32_t score = 0;
bool gameOver = false;
int currTetrominoIdx;
int currRotation = 0;
int currX = A_WIDTH / 2;
int currY = 0;
int main() {
printf("\e[2J\e[H"); // clear screen & reset cursor position
memset(arena, 0, sizeof(arena[0][0]) * A_HEIGHT * A_WIDTH);
newTetromino();
const int targetFrameTime = 350;
clock_t lastTime = clock();
while (!gameOver) {
clock_t now = clock();
clock_t elapsed = (now - lastTime) * 1000 / CLOCKS_PER_SEC;
processInputs();
if (elapsed >= targetFrameTime) {
if (!moveDown()) {
addToArena();
checkLines();
newTetromino();
}
lastTime = now;
}
drawArena();
Sleep(10);
}
printf("\e[2J\e[H"); // clear screen & reset cursor position
printf("Game over!\nScore: %d\n", score);
return 0;
}
void newTetromino() {
currTetrominoIdx = rand() % 7;
currRotation = 0;
currX = (A_WIDTH / 2) - (T_WIDTH / 2);
currY = 0;
gameOver = !validPos(currTetrominoIdx, currRotation, currX, currY);
}
bool validPos(int tetromino, int rotation, int posX, int posY) {
for (int x = 0; x < T_WIDTH; x++) {
for (int y = 0; y < T_HEIGHT; y++) {
int index = rotate(x, y, rotation);
if (1 != tetrominoes[tetromino][index]) {
continue;
}
int arenaX = x + posX;
int arenaY = y + posY;
if (0 > arenaX || A_WIDTH <= arenaX || A_HEIGHT <= arenaY) {
return false;
}
int arenaXY = arena[arenaY][arenaX];
if (0 <= arenaY && 1 == arenaXY) {
return false;
}
}
}
return true;
}
int rotate(int x, int y, int rotation) {
switch (rotation % 4) {
case 0:
return x + y * T_WIDTH;
case 1:
return 12 + y - (x * T_WIDTH);
case 2:
return 15 - (y * T_WIDTH) - x;
case 3:
return 3 - y + (x * T_WIDTH);
default:
return 0;
}
}
void processInputs() {
if (!_kbhit()) {
return;
}
while (_kbhit()) {
int key = _getch();
switch (key) {
case 32: // Spacebar
int nextRotation = (currRotation + 1) % 4;
if (validPos(currTetrominoIdx, nextRotation, currX, currY)) {
currRotation = nextRotation;
}
break;
case 75: // Left arrow key
if (validPos(currTetrominoIdx, currRotation, currX - 1, currY)) {
currX--;
}
break;
case 77: // Right arrow key
if (validPos(currTetrominoIdx, currRotation, currX + 1, currY)) {
currX++;
}
break;
case 80: // Down arrow key
if (validPos(currTetrominoIdx, currRotation, currX, currY + 1)) {
currY++;
}
break;
}
}
}
bool moveDown() {
if (validPos(currTetrominoIdx, currRotation, currX, currY + 1)) {
currY++;
return true;
}
return false;
}
void addToArena() {
for (int y = 0; y < T_HEIGHT; y++) {
for (int x = 0; x < T_WIDTH; x++) {
int index = rotate(x, y, currRotation);
if (1 != tetrominoes[currTetrominoIdx][index]) {
continue;
}
int arenaX = currX + x;
int arenaY = currY + y;
bool xInRange = (0 <= arenaX) && (arenaX < A_WIDTH);
bool yInRange = (0 <= arenaY) && (arenaY < A_HEIGHT);
if (xInRange && yInRange) {
arena[arenaY][arenaX] = 1;
}
}
}
}
void checkLines() {
int clearedLines = 0;
for (int y = A_HEIGHT - 1; y >= 0; y--) {
bool lineFull = true;
for (int x = 0; x < A_WIDTH; x++) {
if (0 == arena[y][x]) {
lineFull = false;
break;
}
}
if (!lineFull) {
continue;
}
clearedLines++;
for (int yy = y; yy > 0; yy--) {
for (int xx = 0; xx < A_WIDTH; xx++) {
arena[yy][xx] = arena[yy - 1][xx];
}
}
for (int xx = 0; xx < A_WIDTH; xx++) {
arena[0][xx] = 0;
}
y++;
}
if (0 < clearedLines) {
score += 100 * clearedLines;
}
}
void drawArena() {
printf("\e[?25l\e[H"); // hide cursor & reset cursor position
char buffer[512];
int bufferIndex = 0;
for (int y = 0; y < A_HEIGHT; y++) {
buffer[bufferIndex++] = '|';
for (int x = 0; x < A_WIDTH; x++) {
int rotatedPos = rotate(x - currX, y - currY, currRotation);
bool validX = x >= currX && x < currX + T_WIDTH;
bool validY = y >= currY && y < currY + T_HEIGHT;
bool xyFilled = 1 == tetrominoes[currTetrominoIdx][rotatedPos];
if (1 == arena[y][x] || (validX && validY && xyFilled)) {
buffer[bufferIndex++] = '#';
} else {
buffer[bufferIndex++] = ' ';
}
}
buffer[bufferIndex++] = '|';
buffer[bufferIndex++] = '\n';
}
buffer[bufferIndex] = '\0';
printf("%s\n\nScore: %d\n\n", buffer, score);
}