-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.c
409 lines (378 loc) · 11.5 KB
/
pong.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#include <errno.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdbool.h>
#include <inttypes.h>
#include <wchar.h>
#include <locale.h>
#include <math.h>
#ifndef ASCII
/*
Uncomment the following line or use `-D ASCII' when
compiling to use ASCII instead of UTF-8.
*/
//#define ASCII
#endif
char VER[] = "0.5";
#ifndef ASCII
wchar_t* logo[] = {
(wchar_t[]){L"████▄ ▗████▖ █▖ █ ▗███▙"},
(wchar_t[]){L"█ █ ████▒█ ██▖ █ █▘"},
(wchar_t[]){L"████▀ ██████ █▝█▖█ █ ██"},
(wchar_t[]){L"█ ██████ █ ▝██ █▖ █"},
(wchar_t[]){L"█ ▝████▘ █ ▝█ ▝███▛"}
};
#else
wchar_t* logo[] = {
(wchar_t[]){L"#### @@@@ # # ####"},
(wchar_t[]){L"# # @@@@#@ ## # #"},
(wchar_t[]){L"#### @@@@@@ # # # # ##"},
(wchar_t[]){L"# @@@@@@ # ## # #"},
(wchar_t[]){L"# @@@@ # # ####"},
};
#endif
struct timeval time1;
void delay(int ms) {
uint64_t cms = 0;
gettimeofday(&time1, NULL);
uint64_t bms = cms = time1.tv_sec * 1000 + time1.tv_usec / 1000;
while (cms < bms + ms) {
gettimeofday(&time1, NULL);
cms = time1.tv_sec * 1000 + time1.tv_usec / 1000;
}
}
uint64_t timems() {
uint64_t cms = 0;
gettimeofday(&time1, NULL);
return time1.tv_sec * 1000 + time1.tv_usec / 1000;
}
bool rawMode = false;
struct termios term, restore;
static struct termios orig_termios;
void enableRawMode() {
if (rawMode) return;
tcgetattr(0, &term);
tcgetattr(0, &restore);
term.c_lflag &= ~(ICANON|ECHO);
tcsetattr(0, TCSANOW, &term);
rawMode = true;
}
void disableRawMode() {
if (!rawMode) return;
tcsetattr(0, TCSANOW, &restore);
rawMode = false;
}
void c_exit(int err) {
disableRawMode();
exit(err);
}
void z_exit() {
c_exit(0);
}
int height, width;
void getTermSize() {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
height = w.ws_row;
width = w.ws_col;
}
int kbhit() {
static const int STDIN = 0;
static bool initialized = false;
if (!initialized) {
struct termios term;
tcgetattr(STDIN, &term);
term.c_lflag &= ~ICANON;
tcsetattr(STDIN, TCSANOW, &term);
setbuf(stdin, NULL);
initialized = true;
}
int bytesWaiting;
ioctl(STDIN, FIONREAD, &bytesWaiting);
return bytesWaiting;
}
char kbin[256];
void kbget() {
int tmp;
kbin[0] = 0;
if (!(tmp = kbhit())) return;
int obp = 0;
while (obp < tmp) {
kbin[obp] = 0;
kbin[obp] = getchar();
if (kbin[obp] == 0) {break;}
obp++;
}
kbin[obp] = 0;
/*
for (int i = 0; i < obp; i++) {
printf("[%d]: [%d]/{%c} ", i, kbin[i], kbin[i]);
putchar('\n');
}
*/
}
#ifndef ASCII
wchar_t boxchars[] = {L'─', L'│', L'┌', L'┐', L'└', L'┘'};
wchar_t bchar = L'●';
wchar_t pchar = L'█';
#else
wchar_t boxchars[] = {L'-', L'|', L'.', L'.', L'\'', L'\''};
wchar_t bchar = L'O';
wchar_t pchar = L'$';
#endif
void putlogo(int xp, int yp) {
if (!xp || !yp) return;
int size = sizeof(logo) / sizeof(wchar_t*);
int yh = yp + size;
printf("\e[s\e[%d;%dH", yp, xp);
FILE* ret = freopen(NULL, "w", stdout);
(void)ret;
for (int i = 0; i < size; i++) {
fputws(logo[i], stdout);
yp++;
wprintf(L"\e[%d;%dH", yp, xp);
}
ret = freopen(NULL, "w", stdout);
(void)ret;
printf("\e[u");
fflush(stdout);
}
void putbox(int xp, int yp, int xs, int ys) {
if (!xp || !yp || !xs || !ys) return;
int xh = xp + xs, yh = yp + ys;
printf("\e[s\e[%d;%dH", yp, xp);
FILE* ret = freopen(NULL, "w", stdout);
(void)ret;
putwchar(boxchars[2]);
for (int i = xp; i < xh - 2; i++) {
putwchar(boxchars[0]);
}
putwchar(boxchars[3]);
while (yp < yh - 2) {
yp++;
wprintf(L"\e[%d;%dH", yp, xp);
putwchar(boxchars[1]);
wprintf(L"\e[%d;%dH", yp, xh - 1);
putwchar(boxchars[1]);
}
yp++;
wprintf(L"\e[%d;%dH", yp, xp);
putwchar(boxchars[4]);
for (int i = xp; i < xh - 2; i++) {
putwchar(boxchars[0]);
}
putwchar(boxchars[5]);
ret = freopen(NULL, "w", stdout);
(void)ret;
printf("\e[u");
fflush(stdout);
}
void fancyClear() {
bool* ridline = calloc(height * sizeof(bool), sizeof(bool));
for (int i = 0; i < height; i++) {
int rnum = -1;
while (rnum == -1 || ridline[rnum - 1]) {rnum = (rand() % height) + 1;}
ridline[rnum - 1] = true;
printf("\e[s\e[%d;1H\e[2K\e[u", rnum);
fflush(stdout);
delay(50);
}
fflush(stdout);
free(ridline);
}
int ppos[] = {0, 0};
int c[] = {0, 0};
double bx, by, obx, oby, bxs, bys;
int phpercent, pheight, speed, sdelay, score[] = {0, 0};
void drawScreen() {
int pmax = ppos[0] + pheight + 2;
FILE* ret = freopen(NULL, "w", stdout);
(void)ret;
wprintf(L"\e[s\e[2;6H");
for (int i = 2; i < height; i++) {
if (i - 1 > ppos[0] && i < pmax) {
putwchar(pchar);
} else {
putwchar(L' ');
}
wprintf(L"\e[D\e[B");
}
pmax = ppos[1] + pheight + 2;
wprintf(L"\e[2;%dH", width - 5);
for (int i = 2; i < height; i++) {
if (i - 1 > ppos[1] && i < pmax) {
putwchar(pchar);
} else {
putwchar(L' ');
}
wprintf(L"\e[D\e[B");
}
wprintf(L"\e[%d;%dH ", (int)(oby + 2), (int)(obx + 2));
wprintf(L"\e[%d;%dH", (int)(by + 2), (int)(bx + 2), bchar);
putwchar(bchar);
obx = bx;
oby = by;
ret = freopen(NULL, "w", stdout);
(void)ret;
printf("\e[u");
fflush(stdout);
}
void randb() {
bool n = (bool)(bxs < 0);
bxs = (((rand() % 2) * 2 - 1) * ((rand() % 4) + 2)) * (1 - n * 2);
bys = (6 - fabs(bxs)) * ((rand() % 2) * 2 - 1);
}
void resetb() {
bx = (width - 2) / 2 - 1;
if (!(width % 2)) bx += rand() % 2;
by = (height - 2) / 2 - 1;
if (!(height % 2)) by += rand() % 2;
}
void setVars() {
speed = 2;
phpercent = 25;
pheight = ((double)height - 2) / (100 / (double)phpercent);
resetb();
obx = bx; oby = by;
sdelay = 90 / speed;
ppos[0] = ppos[1] = (height - 2) / 2 - (pheight + 1) / 2;
randb();
}
bool key(char k) {
return (kbin[0] == k && !kbin[1]);
}
bool chkp(int p) {
return (by >= ppos[p] && by <= ppos[p] + pheight);
}
void dscore() {
putbox(1, 1, width, height);
printf("\e[s\e[1;3HP1:\e[C[%d]", score[0]);
char buf[16];
sprintf(buf, "%d", score[1]);
printf("\e[1;%dHP2:\e[C[%s]\e[u", width - (int)strlen(buf) - 7, buf);
fflush(stdout);
}
void pscore(int p) {
score[p]++;
putbox(1, 1, width, height);
for (int i = 0; i < 3; i++) {
printf("\e[s\e[1;%dHP%d Scores!\e[u", (int)(width / 2 - 5) + 1, p + 1);
fflush(stdout);
delay(500);
putbox(1, 1, width, height);
delay(500);
}
c[0] = c[1] = 0;
dscore();
}
int main(int argc, char* argv[]) {
if (system("tty -s 1> /dev/null 2> /dev/null")) {
char command[32768];
strcpy(command, "xterm -T Pong -b 0 -bg black -bcn 200 -bcf 200 -e 'echo -e -n \"\x1b[\x33 q\" && ");
strcat(command, argv[0]);
strcat(command, "'");
exit(system(command));
}
bool exit = false;
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
if (!strcmp(argv[i], "--version") || !strcmp(argv[i], "-v")) {
if (argc > 2) {fputs("Incorrect number of options passed.\n", stderr); c_exit(EINVAL);}
printf("Terminal Pong version %s\n", VER);
puts("Copyright (C) 2021 PQCraft");
exit = true;
} else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
if (argc > 2) {fputs("Incorrect number of options passed.\n", stderr); c_exit(EINVAL);}
printf("Usage: %s [options]\n", argv[0]);
puts("Options:");
puts(" -h, --help Shows this help text.");
puts(" -v, --version Shows the version and copyright information.");
exit = true;
} else {
fprintf(stderr, "Invalid option '%s'\n", argv[i]); c_exit(EINVAL);
}
} else {
fprintf(stderr, "Invalid data '%s'\n", argv[i]); c_exit(EINVAL);
}
}
fflush(stdout);
if (exit) c_exit(0);
getTermSize();
if (width < 80 || height < 24) {
fprintf(stderr, "\e[2K\e[0GSorry, you must have a terminal size of at least 80x24 \e[%dG:(\n", width - 1);
c_exit(EIO);
}
setlocale(LC_CTYPE, "");
signal(SIGINT, z_exit);
gettimeofday(&time1, NULL);
srand(time1.tv_usec + time1.tv_sec);
enableRawMode();
fancyClear();
fflush(stdout);
putbox(1, 1, width, height);
putbox(width / 2 - 20, 4, 3, 3);
putbox(width / 2 - 19, 5, 4, 4);
putlogo(width / 2 - 11, 4);
putbox(width / 2 + 17, 4, 3, 3);
putbox(width / 2 + 18, 5, 4, 4);
char* mtxt[] = {
"ENTER = Play, ESC = Exit, BACKSPACE = Toggle Pause",
"P1: q = Up, z = Down, Q = Up 1, Z = Down 1, a/A = Stop",
"P2: p = Up, , = Down, P = Up 1, < = Down 1, l/L = Stop"
};
printf("\e[%d;%dH%s", height / 2 - 2, (int)(width / 2 - strlen(mtxt[0]) / 2) + 1, mtxt[0]);
printf("\e[%d;%dH%s", height / 2 - 1, (int)(width / 2 - strlen(mtxt[1]) / 2) + 1, mtxt[1]);
printf("\e[%d;%dH%s", height / 2, (int)(width / 2 - strlen(mtxt[2]) / 2) + 1, mtxt[2]);
printf("\e[1;1H");
fflush(stdout);
while (1) {
delay(25);
kbget();
if (kbin[0] == '\e' && !kbin[1]) {fancyClear(); c_exit(0);}
if (kbin[strlen(kbin) - 1] == 10 || kbin[strlen(kbin) - 1] == 13) break;
}
fancyClear();
putbox(1, 1, width, height);
setVars();
drawScreen();
dscore();
uint64_t tval = timems() + sdelay;
int d[] = {0, 0};
while (1) {
kbget();
if (key('\e')) break;
if (key('Q') && ppos[0] > 0) {d[0] = -1; c[0] = 0;}
if (key('Z') && ppos[0] + pheight < height - 2) {d[0] = 1; c[0] = 0;}
if ((key('A') || key('a') && !kbin[1])) c[0] = 0;
if ((key('q') || c[0] == 1) && ppos[0] > 0) {d[0] = -1; c[0] = 1;}
if ((key('z') || c[0] == 2) && ppos[0] + pheight < height - 2) {d[0] = 1; c[0] = 2;}
if (key('P') && ppos[1] > 0) {d[1] = -1; c[1] = 0;}
if (key('<') && ppos[1] + pheight < height - 2) {d[1] = 1; c[1] = 0;}
if (key('l') || key('L')) c[1] = 0;
if ((key('p') || c[1] == 1) && ppos[1] > 0) {d[1] = -1; c[1] = 1;}
if ((key(',') || c[1] == 2) && ppos[1] + pheight < height - 2) {d[1] = 1; c[1] = 2;}
if (timems() > tval + sdelay) {
ppos[0] += d[0];
ppos[1] += d[1];
d[0] = d[1] = 0;
bx += (bxs / 5);
if (bx <= 5 && chkp(0) && bxs < 0) {bxs *= -1; randb(); bx = 5; c[0] = 0;}
if (bx > width - 8 && chkp(1) && bxs > 0) {bxs *= -1; randb(); bx = width - 8; c[1] = 0;}
if (bx <= 0) {pscore(1); bxs *= -1; resetb();}
if (bx > width - 3) {pscore(0); bxs *= -1; resetb();}
by += (bys / 10);
if (by <= 0) {bys *= -1; by = 0;}
if (by > height - 3) {bys *= -1; by = height - 3;}
drawScreen();
tval = timems();
}
}
fancyClear();
c_exit(0);
}