forked from nakidai/csnake
1
0
Fork 0

Add pause key

Also moved render with sleep to start of loop, so you can see first
frame.
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;
char *key = malloc(sizeof(char)); *key = 0;
bool stopped = false;
InputArgs input_args = (InputArgs){ key, running };
thrd_create(&input_thread, input, &input_args);
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)
{
case 'q':
*running = false; return 0;
case 'p':
stopped = !stopped; break;
case 'w':
if (player->direction == DOWN) break;
player->direction = UP; break;
@ -66,7 +78,8 @@ int main(int argc, char **argv)
case 'a':
if (player->direction == RIGHT) break;
player->direction = LEFT; break;
}
} *key = 0;
if (stopped) continue;
if (playerDoTick(player, food) && player->score < SIZE*SIZE - 1)
food = generateFood(player);
@ -77,16 +90,6 @@ int main(int argc, char **argv)
*running = false;
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;
}