Compare commits
15 Commits
f89b46a60d
...
e9e503d5b2
Author | SHA1 | Date |
---|---|---|
Nakidai | e9e503d5b2 | |
Nakidai | f25cd3ca01 | |
Nakidai | f2e33ad320 | |
Nakidai | 9529eebcbc | |
Nakidai | 2e05977e12 | |
Nakidai | 5f6fc8f212 | |
Nakidai | 317622a253 | |
Nakidai | b23325bfec | |
Nakidai | 94d6a769a1 | |
Nakidai | 2ebe6fccea | |
Nakidai | d6b4f0be32 | |
Nakidai | 84e21cb515 | |
Nakidai | a064af95da | |
Nakidai | b611b54575 | |
Nakidai | 758dbf9cc2 |
7
Makefile
7
Makefile
|
@ -1,9 +1,6 @@
|
||||||
OUT = game
|
include config.mk
|
||||||
CFLAGS =
|
|
||||||
LDFLAGS =
|
|
||||||
INCLUDE = -Iinclude
|
INCLUDE = -Iinclude
|
||||||
CC = cc
|
|
||||||
LD = ld
|
|
||||||
RM = rm -f
|
RM = rm -f
|
||||||
SRCDIR = src
|
SRCDIR = src
|
||||||
OBJDIR = obj
|
OBJDIR = obj
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
if [[ "$*" == *"--help"* ]] || [[ "$*" == *"-h"* ]]
|
||||||
|
then
|
||||||
|
echo "Use environment variables to pass values:
|
||||||
|
CC - compiler (default: cc)
|
||||||
|
CFLAGS - flags for compiler
|
||||||
|
LDFLAGS - flags for linker
|
||||||
|
OUT - out file (default: game
|
||||||
|
SIZE - size of game field
|
||||||
|
DEFX - start x
|
||||||
|
DEFY - start y"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
CC=${CC:-cc}
|
||||||
|
CFLAGS=${CFLAGS:-}
|
||||||
|
LDFLAGS=${LDFLAGS:-}
|
||||||
|
OUT=${OUT:-game}
|
||||||
|
SIZE=${SIZE:-10}
|
||||||
|
DEFX=${DEFX:-0}
|
||||||
|
DEFY=${DEFY:-0}
|
||||||
|
|
||||||
|
echo "Makefile configuration:"
|
||||||
|
echo "Compiler: $CC"
|
||||||
|
echo "CFLAGS: $CFLAGS"
|
||||||
|
echo "LDFLAGS: $LDFLAGS"
|
||||||
|
echo "Out file: $OUT"
|
||||||
|
echo
|
||||||
|
echo "Code configuration:"
|
||||||
|
echo "Size: $SIZE"
|
||||||
|
echo "Start x: $DEFX"
|
||||||
|
echo "Start y: $DEFY"
|
||||||
|
|
||||||
|
echo "CC = $CC
|
||||||
|
CFLAGS = $CFLAGS
|
||||||
|
LDFLAGS = $LDFLAGS
|
||||||
|
OUT = $OUT" > config.mk
|
||||||
|
|
||||||
|
echo "#define SIZE $SIZE
|
||||||
|
#define DEFX $DEFX
|
||||||
|
#define DEFY $DEFY" > include/config.h
|
|
@ -0,0 +1,3 @@
|
||||||
|
#define SIZE 10
|
||||||
|
#define DEFX 0
|
||||||
|
#define DEFY 0
|
|
@ -9,6 +9,6 @@ typedef struct input_args_t
|
||||||
bool *alive;
|
bool *alive;
|
||||||
} InputArgs;
|
} InputArgs;
|
||||||
|
|
||||||
void *input(void *vargp);
|
int input(void *vargp);
|
||||||
|
|
||||||
#endif /* __INPUT_H__ */
|
#endif /* __INPUT_H__ */
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
#ifndef __SCREEN_H__
|
#ifndef __SCREEN_H__
|
||||||
#define __SCREEN_H__
|
#define __SCREEN_H__
|
||||||
|
|
||||||
|
typedef char Point;
|
||||||
typedef struct screen_t
|
typedef struct screen_t
|
||||||
{
|
{
|
||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
char *screen;
|
Point *screen;
|
||||||
} Screen;
|
} Screen;
|
||||||
typedef char Point;
|
|
||||||
|
|
||||||
Screen *screenCreate(int width, int height, Point fill_value);
|
Screen *screenCreate(int width, int height, Point fill_value);
|
||||||
void screenFree(Screen *screen);
|
void screenFree(Screen *screen);
|
||||||
|
|
|
@ -22,7 +22,7 @@ char getch(void)
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *input(void *vargp)
|
int input(void *vargp)
|
||||||
{
|
{
|
||||||
char *out = ((InputArgs *)vargp)->out;
|
char *out = ((InputArgs *)vargp)->out;
|
||||||
bool *alive = ((InputArgs *)vargp)->alive;
|
bool *alive = ((InputArgs *)vargp)->alive;
|
||||||
|
@ -31,5 +31,5 @@ void *input(void *vargp)
|
||||||
{
|
{
|
||||||
*out = getch();
|
*out = getch();
|
||||||
}
|
}
|
||||||
return NULL;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
28
src/main.c
28
src/main.c
|
@ -1,14 +1,14 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <pthread.h>
|
#include <threads.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <time.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"
|
#include "food.h"
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
void drawPlayer(Player *player, Screen *screen)
|
void drawPlayer(Player *player, Screen *screen)
|
||||||
{
|
{
|
||||||
|
@ -22,18 +22,24 @@ Food generateFood(Player *player)
|
||||||
Food food;
|
Food food;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
food = (Food){random() % 10, random() % 10};
|
food = (Food){random() % SIZE, random() % SIZE};
|
||||||
} while (playerCheckFoodCollision(player, food));
|
} while (playerCheckFoodCollision(player, food));
|
||||||
return food;
|
return food;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void resetCoordinates(void)
|
||||||
|
{
|
||||||
|
printf("\e[1;1H\e[2J");
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
srandom(time(NULL));
|
srandom(time(NULL));
|
||||||
Player *player = playerCreate(DOWN, 0, 0, 0);
|
Player *player = playerCreate(DOWN, DEFX, DEFY, 0);
|
||||||
Screen *screen = screenCreate(10, 10, ' ');
|
Screen *screen = screenCreate(SIZE, SIZE, ' ');
|
||||||
PlayerNode *node;
|
PlayerNode *node;
|
||||||
pthread_t input_thread;
|
thrd_t input_thread;
|
||||||
|
int i;
|
||||||
int head_x, head_y;
|
int head_x, head_y;
|
||||||
Food food = generateFood(player);
|
Food food = generateFood(player);
|
||||||
|
|
||||||
|
@ -41,7 +47,7 @@ int main(int argc, char **argv)
|
||||||
char *key = malloc(sizeof(char)); *key = 0;
|
char *key = malloc(sizeof(char)); *key = 0;
|
||||||
InputArgs input_args = (InputArgs){ key, running };
|
InputArgs input_args = (InputArgs){ key, running };
|
||||||
|
|
||||||
pthread_create(&input_thread, NULL, input, &input_args);
|
thrd_create(&input_thread, input, &input_args);
|
||||||
while (*running)
|
while (*running)
|
||||||
{
|
{
|
||||||
switch (*key)
|
switch (*key)
|
||||||
|
@ -56,7 +62,6 @@ int main(int argc, char **argv)
|
||||||
player->direction = DOWN; break;
|
player->direction = DOWN; break;
|
||||||
case 'a':
|
case 'a':
|
||||||
player->direction = LEFT; break;
|
player->direction = LEFT; break;
|
||||||
sleep(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (playerDoTick(player, food))
|
if (playerDoTick(player, food))
|
||||||
|
@ -68,7 +73,7 @@ int main(int argc, char **argv)
|
||||||
}
|
}
|
||||||
head_x = player->head->x;
|
head_x = player->head->x;
|
||||||
head_y = player->head->y;
|
head_y = player->head->y;
|
||||||
if (head_x >= 10 || head_x < 0 || head_y >= 10 || head_y < 0)
|
if (head_x >= SIZE || head_x < 0 || head_y >= SIZE || head_y < 0)
|
||||||
{
|
{
|
||||||
*running = false;
|
*running = false;
|
||||||
break;
|
break;
|
||||||
|
@ -77,9 +82,12 @@ int main(int argc, char **argv)
|
||||||
screenSet(screen, ' ');
|
screenSet(screen, ' ');
|
||||||
drawPlayer(player, screen);
|
drawPlayer(player, screen);
|
||||||
*screenGetPoint(screen, food.x, food.y) = '@';
|
*screenGetPoint(screen, food.x, food.y) = '@';
|
||||||
|
resetCoordinates();
|
||||||
screenShow(screen);
|
screenShow(screen);
|
||||||
|
for (i = 0; i < SIZE*2; ++i) putchar('-');
|
||||||
|
printf("\nScore: %d\n", player->score);
|
||||||
|
|
||||||
sleep(1);
|
thrd_sleep(&(struct timespec){.tv_sec=1}, NULL);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,8 @@ Player *playerCreate(Direction direction, int x, int y, int score)
|
||||||
player->head = head;
|
player->head = head;
|
||||||
player->score = score;
|
player->score = score;
|
||||||
player->direction = direction;
|
player->direction = direction;
|
||||||
|
|
||||||
|
return player;
|
||||||
}
|
}
|
||||||
|
|
||||||
void playerFree(Player *player)
|
void playerFree(Player *player)
|
||||||
|
@ -34,7 +36,7 @@ bool playerCheckSelfCollision(Player *player)
|
||||||
PlayerNode *nodei, *nodej;
|
PlayerNode *nodei, *nodej;
|
||||||
for (nodei = player->tail; nodei != NULL; nodei = nodei->next)
|
for (nodei = player->tail; nodei != NULL; nodei = nodei->next)
|
||||||
for (nodej = nodei->next; nodej != NULL; nodej = nodej->next)
|
for (nodej = nodei->next; nodej != NULL; nodej = nodej->next)
|
||||||
if (nodei->x == nodej->x && nodei->y == nodej->x)
|
if (nodei->x == nodej->x && nodei->y == nodej->y)
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,7 @@ void screenShow(Screen *screen)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void screenSet(Screen *screen, char fill_value)
|
void screenSet(Screen *screen, Point fill_value)
|
||||||
{
|
{
|
||||||
memset(screen->screen, fill_value, screen->width * screen->height * sizeof(char));
|
memset(screen->screen, fill_value, screen->width * screen->height * sizeof(char));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue