Add pause key

Also moved render with sleep to start of loop, so you can see first
frame.
fix-mingw 1.0
Nakidai 2023-11-01 19:29:12 +03:00
parent b64ffdb82a
commit 98089c4351
Signed by untrusted user who does not match committer: nakidai
GPG Key ID: 914675D395210A97
1 changed files with 14 additions and 11 deletions

View File

@ -45,15 +45,27 @@ int main(int argc, char **argv)
bool *running = malloc(sizeof(bool)); *running = true; bool *running = malloc(sizeof(bool)); *running = true;
char *key = malloc(sizeof(char)); *key = 0; char *key = malloc(sizeof(char)); *key = 0;
bool stopped = false;
InputArgs input_args = (InputArgs){ key, running }; InputArgs input_args = (InputArgs){ key, running };
thrd_create(&input_thread, input, &input_args); thrd_create(&input_thread, input, &input_args);
while (*running) while (*running)
{ {
screenSet(screen, ' ');
drawPlayer(player, screen);
*screenGetPoint(screen, food.x, food.y) = '@';
resetCoordinates();
screenShow(screen);
for (i = 0; i < SIZE*2; ++i) putchar('-');
printf("\nScore: %d\n", player->score);
thrd_sleep(&(struct timespec){.tv_sec=1}, NULL);
switch (*key) switch (*key)
{ {
case 'q': case 'q':
*running = false; return 0; *running = false; return 0;
case 'p':
stopped = !stopped; break;
case 'w': case 'w':
if (player->direction == DOWN) break; if (player->direction == DOWN) break;
player->direction = UP; break; player->direction = UP; break;
@ -66,7 +78,8 @@ int main(int argc, char **argv)
case 'a': case 'a':
if (player->direction == RIGHT) break; if (player->direction == RIGHT) break;
player->direction = LEFT; break; player->direction = LEFT; break;
} } *key = 0;
if (stopped) continue;
if (playerDoTick(player, food) && player->score < SIZE*SIZE - 1) if (playerDoTick(player, food) && player->score < SIZE*SIZE - 1)
food = generateFood(player); food = generateFood(player);
@ -77,16 +90,6 @@ int main(int argc, char **argv)
*running = false; *running = false;
break; break;
} }
screenSet(screen, ' ');
drawPlayer(player, screen);
*screenGetPoint(screen, food.x, food.y) = '@';
resetCoordinates();
screenShow(screen);
for (i = 0; i < SIZE*2; ++i) putchar('-');
printf("\nScore: %d\n", player->score);
thrd_sleep(&(struct timespec){.tv_sec=1}, NULL);
} }
return 0; return 0;
} }