forked from nakidai/csnake
1
0
Fork 0
csnake/src/main.c

117 lines
2.9 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#ifdef _WIN32
#include <Windows.h>
#include <process.h>
#else
#include <pthread.h>
#endif
#include "input.h"
2023-10-29 20:03:23 +03:00
#include "screen.h"
#include "player.h"
#include "food.h"
2023-10-30 03:09:32 +03:00
#include "config.h"
void drawPlayer(Player *player, Screen *screen)
{
PlayerNode *node;
for (node = player->tail; node != NULL; node = node->next)
*screenGetPoint(screen, node->x, node->y) = '#';
}
2023-10-29 20:03:23 +03:00
Food generateFood(Player *player)
{
Food food;
do
{
2023-10-31 06:07:03 +03:00
food = (Food){rand() % SIZE, rand() % SIZE};
} while (playerCheckFoodCollision(player, food));
return food;
}
#ifdef _WIN32
void resetCoordinates(void)
{
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(output, (COORD){0});
}
#else
2023-10-30 14:24:34 +03:00
void resetCoordinates(void)
{
printf("\e[1;1H\e[2J");
2023-10-30 14:24:34 +03:00
}
#endif
2023-10-30 14:24:34 +03:00
2023-10-29 20:03:23 +03:00
int main(int argc, char **argv)
{
2023-10-31 06:07:03 +03:00
srand(time(NULL));
2023-10-30 03:09:32 +03:00
Player *player = playerCreate(DOWN, DEFX, DEFY, 0);
Screen *screen = screenCreate(SIZE, SIZE, ' ');
PlayerNode *node;
2023-10-30 03:53:48 +03:00
int i;
int head_x, head_y;
Food food = generateFood(player);
bool *running = malloc(sizeof(bool)); *running = true;
2023-11-24 22:58:33 +03:00
int *key = malloc(sizeof(char)); *key = 0;
bool stopped = false;
InputArgs input_args = (InputArgs){ key, running };
#ifdef _WIN32
_beginthread(input, 0, &input_args);
#else
2023-11-25 00:00:44 +03:00
pthread_t input_thread;
2023-11-24 23:32:42 +03:00
pthread_create(&input_thread, NULL, input, &input_args);
#endif
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);
#ifdef _WIN32
Sleep(1000L);
#else
2023-11-24 23:32:42 +03:00
nanosleep(&(struct timespec){.tv_sec = 1}, NULL);
#endif
switch (*key)
{
case 'q':
*running = false; return 0;
case 'p':
stopped = !stopped; break;
case 'w':
2023-10-31 05:05:10 +03:00
if (player->direction == DOWN) break;
player->direction = UP; break;
case 'd':
2023-10-31 05:05:10 +03:00
if (player->direction == LEFT) break;
player->direction = RIGHT; break;
case 's':
2023-10-31 05:05:10 +03:00
if (player->direction == UP) break;
player->direction = DOWN; break;
case 'a':
2023-10-31 05:05:10 +03:00
if (player->direction == RIGHT) break;
player->direction = LEFT; break;
} *key = 0;
if (stopped) continue;
2023-11-01 19:21:51 +03:00
if (playerDoTick(player, food) && player->score < SIZE*SIZE - 1)
food = generateFood(player);
head_x = player->head->x;
head_y = player->head->y;
2023-11-01 13:27:42 +03:00
if (head_x >= SIZE || head_x < 0 || head_y >= SIZE || head_y < 0 || playerCheckSelfCollision(player))
{
*running = false;
break;
}
}
2023-10-29 20:03:23 +03:00
return 0;
}