-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
151 lines (133 loc) · 3.71 KB
/
main.c
File metadata and controls
151 lines (133 loc) · 3.71 KB
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
#define _POSIX_C_SOURCE 199309
#include <ctype.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
void sleep_ms(int ms) {
nanosleep(&((struct timespec) { ms / 1000, ms % 1000 * 1'000'000 }), nullptr);
}
typedef struct {
int x;
int y;
} pos_t;
constexpr pos_t game_size = { 20, 20 };
pos_t rand_pos() {
return (pos_t) { rand() % game_size.x, rand() % game_size.y };
}
void set_color_at(pos_t pos, uint_least32_t color) {
printf("\x1B[%i;%iH\x1B[48;2;%u;%u;%um ", pos.y + 2, pos.x * 2 + 1, (color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF);
}
bool run() {
srand((unsigned int)time(nullptr));
pos_t apple = rand_pos();
pos_t snake_head = rand_pos();
pos_t snake_direction = { 0, 0 };
static constexpr size_t snake_size = (size_t)(game_size.x * game_size.y);
pos_t snake_body[snake_size] = { snake_head };
size_t snake_start = 0;
size_t snake_end = 0;
size_t score = 0;
while (true) {
printf("\x1B[0m\x1B[HScore: %zu", score);
if (score >= snake_size) {
return true;
}
set_color_at(snake_head, 0x00FF00);
snake_head = (pos_t) {
(snake_head.x + snake_direction.x + game_size.x) % game_size.x,
(snake_head.y + snake_direction.y + game_size.y) % game_size.y
};
snake_start = (snake_start + 1) % snake_size;
snake_body[snake_start] = snake_head;
if ((snake_head.x == apple.x) && (snake_head.y == apple.y)) {
++score;
apple = rand_pos();
} else {
set_color_at(snake_body[snake_end], 0x007FFF);
snake_end = (snake_end + 1) % snake_size;
}
set_color_at(apple, 0xFF0000);
set_color_at(snake_head, 0x00FF00);
for (size_t i = 0; i < score; ++i) {
const pos_t snake_part = snake_body[(snake_end + i) % snake_size];
if ((snake_head.x == snake_part.x) && (snake_head.y == snake_part.y)) {
return false;
}
}
fflush(stdout);
sleep_ms(100);
const bool can_move_x = !snake_direction.x || !score;
const bool can_move_y = !snake_direction.y || !score;
while (true) {
const int input = getchar();
if (toupper(input) == 'Q') {
return false;
}
if (input < 1) {
break;
}
if ((input == '\x1B') && (getchar() == '[')) {
switch (getchar()) {
case 'C':
if (can_move_x) {
snake_direction = (pos_t) { 1, 0 };
}
break;
case 'D':
if (can_move_x) {
snake_direction = (pos_t) { -1, 0 };
}
break;
case 'B':
if (can_move_y) {
snake_direction = (pos_t) { 0, 1 };
}
break;
case 'A':
if (can_move_y) {
snake_direction = (pos_t) { 0, -1 };
}
break;
}
}
}
}
}
int main() {
struct termios cooked_mode;
tcgetattr(STDIN_FILENO, &cooked_mode);
struct termios raw_mode = cooked_mode;
raw_mode.c_iflag &= ~(tcflag_t)(ICRNL | IXON);
raw_mode.c_lflag &= ~(tcflag_t)(ICANON | ECHO | IEXTEN | ISIG);
raw_mode.c_oflag &= ~(tcflag_t)(OPOST);
tcsetattr(STDIN_FILENO, TCSANOW, &raw_mode);
const int block_mode = fcntl(STDIN_FILENO, F_GETFL);
fcntl(STDIN_FILENO, F_SETFL, block_mode | O_NONBLOCK);
fputs("\x1B[s\x1B[?47h\x1B[?25l\x1B[2J\x1B[2H\x1B[48;2;0;127;255m", stdout);
for (int y = 0; y < game_size.y; ++y) {
for (int x = 0; x < game_size.x; ++x) {
fputs(" ", stdout);
}
fputs("\v\r", stdout);
}
printf("\x1B[0m\x1B[%iHUse arrow keys to move, press Q to quit", game_size.y + 2);
const bool win = run();
printf("\x1B[0m\x1B[%iH\x1B[2K", game_size.y + 2);
if (win) {
fputs("You win! ", stdout);
}
fputs("Press any key to exit", stdout);
fflush(stdout);
sleep_ms(500);
while (getchar() > 0);
fcntl(STDIN_FILENO, F_SETFL, block_mode);
getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &cooked_mode);
fputs("\x1B[?25h\x1B[?47l\x1B[u", stdout);
}