jag

snake_c

public

Small snake game in C

Star0
Watch0

Commit cd184c572942

tarball

Jaglekon <jaglekon@jaglekon.de>2026-03-23 15:59 UTC

1 file changed+3-12
changing controls
Modified file: snake.c+3 additions−12 deletions
@@ -118,8 +118,7 @@ int main(int argc, char *args[]) {
118118 current_size = init_size;
119119 bool too_small = false;
120120
121- struct timeval start;
122- gettimeofday(&start, NULL);
121+ struct timespec mili100 = {0, 1000000 * SPEED};
123122
124123 /* Menu */
125124
@@ -144,10 +143,7 @@ int main(int argc, char *args[]) {
144143 struct timeval current;
145144 gettimeofday(&current, NULL);
146145
147- uint64_t time_diff = (current.tv_sec - start.tv_sec) * 1000000 +
148- (current.tv_usec - start.tv_usec);
149-
150- if (time_diff >= SPEED * 1000) {
146+ if (true) {
151147
152148 ioctl(STDOUT_FILENO, TIOCGWINSZ, &current_size);
153149
@@ -196,6 +192,7 @@ int main(int argc, char *args[]) {
196192 break;
197193 }
198194 }
195+ nanosleep(&mili100, NULL);
199196 }
200197
201198 /* initialisation of the game */
@@ -234,8 +231,6 @@ int main(int argc, char *args[]) {
234231
235232 /* start of the actual game loop */
236233
237- struct timespec mili100 = {0, 100000000};
238-
239234 while (run) {
240235
241236 /* input handling */
@@ -266,9 +261,6 @@ int main(int argc, char *args[]) {
266261 struct timeval current;
267262 gettimeofday(&current, NULL);
268263
269- uint64_t time_diff = (current.tv_sec - start.tv_sec) * 1000000 +
270- (current.tv_usec - start.tv_usec);
271-
272264 if (/*time_diff >= SPEED * 1000*/ true) {
273265
274266 ioctl(STDOUT_FILENO, TIOCGWINSZ, &current_size);
@@ -283,7 +275,6 @@ int main(int argc, char *args[]) {
283275
284276 if (!paused) {
285277
286- start = current;
287278 if (tmp_direction != NO_DIRECTION &&
288279 ((shift + (DIRECTIONS >> 1)) % DIRECTIONS) != tmp_direction) {
289280 shift = tmp_direction;