forked from nakidai/csnake
1
0
Fork 0

Add gameplay

- wasd - turn snake
- q - leave
master
Nakidai 2023-10-30 01:55:20 +03:00
parent abda36c1c3
commit b402dc177c
Signed by untrusted user who does not match committer: nakidai
GPG Key ID: 914675D395210A97
6 changed files with 99 additions and 8 deletions

9
include/food.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef __FOOD_H__
#define __FOOD_H__
typedef struct food_t
{
int x, y;
} Food;
#endif /* __FOOD_H__ */

View File

@ -2,6 +2,7 @@
#define __PLAYER_H__ #define __PLAYER_H__
#include <stdbool.h> #include <stdbool.h>
#include "food.h"
#define UP 0 #define UP 0
#define RIGHT 1 #define RIGHT 1
@ -28,6 +29,8 @@ struct player_t
Player *playerCreate(Direction direction, int x, int y, int score); Player *playerCreate(Direction direction, int x, int y, int score);
void playerFree(Player *player); void playerFree(Player *player);
void playerDoTick(Player *player, bool food_collision); bool playerCheckSelfCollision(Player *player);
bool playerCheckFoodCollision(Player *player, Food food);
bool playerDoTick(Player *player, Food food);
#endif /* __PLAYER_H__ */ #endif /* __PLAYER_H__ */

View File

@ -14,5 +14,6 @@ void screenFree(Screen *screen);
Point *screenGetPoint(Screen *screen, int x, int y); Point *screenGetPoint(Screen *screen, int x, int y);
void screenShow(Screen *screen); void screenShow(Screen *screen);
void screenSet(Screen *screen, char fill_value);
#endif /* __SCREEN_H__ */ #endif /* __SCREEN_H__ */

View File

@ -3,10 +3,12 @@
#include <stdbool.h> #include <stdbool.h>
#include <pthread.h> #include <pthread.h>
#include <unistd.h> #include <unistd.h>
#include <time.h>
#include "input.h" #include "input.h"
#include "screen.h" #include "screen.h"
#include "player.h" #include "player.h"
#include "food.h"
void drawPlayer(Player *player, Screen *screen) void drawPlayer(Player *player, Screen *screen)
{ {
@ -15,11 +17,25 @@ void drawPlayer(Player *player, Screen *screen)
*screenGetPoint(screen, node->x, node->y) = '#'; *screenGetPoint(screen, node->x, node->y) = '#';
} }
Food generateFood(Player *player)
{
Food food;
do
{
food = (Food){random() % 10, random() % 10};
} while (playerCheckFoodCollision(player, food));
return food;
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
srandom(time(NULL));
Player *player = playerCreate(DOWN, 0, 0, 0); Player *player = playerCreate(DOWN, 0, 0, 0);
Screen *screen = screenCreate(10, 10, ' '); Screen *screen = screenCreate(10, 10, ' ');
PlayerNode *node;
pthread_t input_thread; pthread_t input_thread;
int head_x, head_y;
Food food = generateFood(player);
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;
@ -28,7 +44,42 @@ int main(int argc, char **argv)
pthread_create(&input_thread, NULL, input, &input_args); pthread_create(&input_thread, NULL, input, &input_args);
while (*running) while (*running)
{ {
if (*key == 'q') *running = false; switch (*key)
{
case 'q':
*running = false; return 0;
case 'w':
player->direction = UP; break;
case 'd':
player->direction = RIGHT; break;
case 's':
player->direction = DOWN; break;
case 'a':
player->direction = LEFT; break;
sleep(1);
}
if (playerDoTick(player, food))
food = generateFood(player);
if (playerCheckSelfCollision(player))
{
*running = false;
break;
}
head_x = player->head->x;
head_y = player->head->y;
if (head_x >= 10 || head_x < 0 || head_y >= 10 || head_y < 0)
{
*running = false;
break;
}
screenSet(screen, ' ');
drawPlayer(player, screen);
*screenGetPoint(screen, food.x, food.y) = '@';
screenShow(screen);
sleep(1);
} }
return 0; return 0;
} }

View File

@ -1,6 +1,7 @@
#include "player.h"
#include <stdlib.h> #include <stdlib.h>
#include "player.h"
Player *playerCreate(Direction direction, int x, int y, int score) Player *playerCreate(Direction direction, int x, int y, int score)
{ {
Player *player = (Player *)malloc(sizeof(Player)); Player *player = (Player *)malloc(sizeof(Player));
@ -20,8 +21,27 @@ void playerFree(Player *player)
{ {
} }
void playerDoTick(Player *player, bool food_collision) bool playerCheckFoodCollision(Player *player, Food food)
{ {
for (PlayerNode *node = player->tail; node != NULL; node = node->next)
if (node->x == food.x && node->y == food.y)
return true;
return false;
}
bool playerCheckSelfCollision(Player *player)
{
PlayerNode *nodei, *nodej;
for (nodei = player->tail; nodei != NULL; nodei = nodei->next)
for (nodej = nodei->next; nodej != NULL; nodej = nodej->next)
if (nodei->x == nodej->x && nodei->y == nodej->x)
return true;
return false;
}
bool playerDoTick(Player *player, Food food)
{
bool food_collision;
PlayerNode *new_head = (PlayerNode *)malloc(sizeof(PlayerNode)); PlayerNode *new_head = (PlayerNode *)malloc(sizeof(PlayerNode));
int head_x = player->head->x; int head_x = player->head->x;
int head_y = player->head->y; int head_y = player->head->y;
@ -49,13 +69,15 @@ void playerDoTick(Player *player, bool food_collision)
player->head->next = new_head; player->head->next = new_head;
player->head = new_head; player->head = new_head;
if (!food_collision) food_collision = (new_head->x == food.x && new_head->y == food.y);
if (food_collision)
{
++player->score;
} else
{ {
PlayerNode *new_tail = player->tail->next; PlayerNode *new_tail = player->tail->next;
free(player->tail); free(player->tail);
player->tail = new_tail; player->tail = new_tail;
} else
{
++player->score;
} }
return food_collision;
} }

View File

@ -43,3 +43,8 @@ void screenShow(Screen *screen)
putchar('\n'); putchar('\n');
} }
} }
void screenSet(Screen *screen, char fill_value)
{
memset(screen->screen, fill_value, screen->width * screen->height * sizeof(char));
}