Compare commits
8 Commits
e9e503d5b2
...
f89b46a60d
Author | SHA1 | Date |
---|---|---|
Nakidai | f89b46a60d | |
Nakidai | 42bddb2ee4 | |
Nakidai | b51afc628f | |
Nakidai | d241321aff | |
Nakidai | 40dc962d5e | |
Nakidai | ad64867b40 | |
Nakidai | 35669794a7 | |
Nakidai | d6eef4c404 |
|
@ -1,2 +1,3 @@
|
||||||
obj/
|
obj/
|
||||||
game
|
game
|
||||||
|
config/
|
||||||
|
|
9
Makefile
9
Makefile
|
@ -1,6 +1,11 @@
|
||||||
include config.mk
|
include config/config.mk
|
||||||
|
|
||||||
INCLUDE = -Iinclude
|
OUT = game
|
||||||
|
CFLAGS =
|
||||||
|
LDFLAGS =
|
||||||
|
INCLUDE = -Iinclude -Iconfig
|
||||||
|
CC = cc
|
||||||
|
LD = ld
|
||||||
RM = rm -f
|
RM = rm -f
|
||||||
SRCDIR = src
|
SRCDIR = src
|
||||||
OBJDIR = obj
|
OBJDIR = obj
|
||||||
|
|
|
@ -14,6 +14,7 @@ then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
CC=${CC:-cc}
|
CC=${CC:-cc}
|
||||||
|
RM=${RM:-rm -f}
|
||||||
CFLAGS=${CFLAGS:-}
|
CFLAGS=${CFLAGS:-}
|
||||||
LDFLAGS=${LDFLAGS:-}
|
LDFLAGS=${LDFLAGS:-}
|
||||||
OUT=${OUT:-game}
|
OUT=${OUT:-game}
|
||||||
|
@ -23,6 +24,7 @@ DEFY=${DEFY:-0}
|
||||||
|
|
||||||
echo "Makefile configuration:"
|
echo "Makefile configuration:"
|
||||||
echo "Compiler: $CC"
|
echo "Compiler: $CC"
|
||||||
|
echo "Remove: $RM"
|
||||||
echo "CFLAGS: $CFLAGS"
|
echo "CFLAGS: $CFLAGS"
|
||||||
echo "LDFLAGS: $LDFLAGS"
|
echo "LDFLAGS: $LDFLAGS"
|
||||||
echo "Out file: $OUT"
|
echo "Out file: $OUT"
|
||||||
|
@ -32,11 +34,19 @@ echo "Size: $SIZE"
|
||||||
echo "Start x: $DEFX"
|
echo "Start x: $DEFX"
|
||||||
echo "Start y: $DEFY"
|
echo "Start y: $DEFY"
|
||||||
|
|
||||||
|
if ! test -d config; then
|
||||||
|
if test -f config; then
|
||||||
|
rm config
|
||||||
|
fi
|
||||||
|
mkdir config
|
||||||
|
fi
|
||||||
|
|
||||||
echo "CC = $CC
|
echo "CC = $CC
|
||||||
|
RM = $RM
|
||||||
CFLAGS = $CFLAGS
|
CFLAGS = $CFLAGS
|
||||||
LDFLAGS = $LDFLAGS
|
LDFLAGS = $LDFLAGS
|
||||||
OUT = $OUT" > config.mk
|
OUT = $OUT" > config/config.mk
|
||||||
|
|
||||||
echo "#define SIZE $SIZE
|
echo "#define SIZE $SIZE
|
||||||
#define DEFX $DEFX
|
#define DEFX $DEFX
|
||||||
#define DEFY $DEFY" > include/config.h
|
#define DEFY $DEFY" > config/config.h
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
#define SIZE 10
|
|
||||||
#define DEFX 0
|
|
||||||
#define DEFY 0
|
|
|
@ -9,6 +9,6 @@ typedef struct input_args_t
|
||||||
bool *alive;
|
bool *alive;
|
||||||
} InputArgs;
|
} InputArgs;
|
||||||
|
|
||||||
int input(void *vargp);
|
void *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;
|
||||||
Point *screen;
|
char *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;
|
||||||
}
|
}
|
||||||
|
|
||||||
int input(void *vargp)
|
void *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 @@ int input(void *vargp)
|
||||||
{
|
{
|
||||||
*out = getch();
|
*out = getch();
|
||||||
}
|
}
|
||||||
return 0;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
19
src/main.c
19
src/main.c
|
@ -1,7 +1,8 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <threads.h>
|
#include <pthread.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
@ -27,19 +28,13 @@ Food generateFood(Player *player)
|
||||||
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, DEFX, DEFY, 0);
|
Player *player = playerCreate(DOWN, DEFX, DEFY, 0);
|
||||||
Screen *screen = screenCreate(SIZE, SIZE, ' ');
|
Screen *screen = screenCreate(SIZE, SIZE, ' ');
|
||||||
PlayerNode *node;
|
PlayerNode *node;
|
||||||
thrd_t input_thread;
|
pthread_t input_thread;
|
||||||
int i;
|
|
||||||
int head_x, head_y;
|
int head_x, head_y;
|
||||||
Food food = generateFood(player);
|
Food food = generateFood(player);
|
||||||
|
|
||||||
|
@ -47,7 +42,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 };
|
||||||
|
|
||||||
thrd_create(&input_thread, input, &input_args);
|
pthread_create(&input_thread, NULL, input, &input_args);
|
||||||
while (*running)
|
while (*running)
|
||||||
{
|
{
|
||||||
switch (*key)
|
switch (*key)
|
||||||
|
@ -62,6 +57,7 @@ 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))
|
||||||
|
@ -82,12 +78,9 @@ 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);
|
|
||||||
|
|
||||||
thrd_sleep(&(struct timespec){.tv_sec=1}, NULL);
|
sleep(1);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,8 +15,6 @@ 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)
|
||||||
|
@ -36,7 +34,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->y)
|
if (nodei->x == nodej->x && nodei->y == nodej->x)
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,7 @@ void screenShow(Screen *screen)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void screenSet(Screen *screen, Point fill_value)
|
void screenSet(Screen *screen, char 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