Compare commits

..

15 Commits

Author SHA1 Message Date
Nakidai e9e503d5b2
Change length of line to SIZE*2 2023-10-30 15:52:25 +03:00
Nakidai f25cd3ca01
Some changes
- Move config.h to include/
- Move config.mk to root
- Remove $RM from configure
2023-10-30 15:50:54 +03:00
Nakidai f2e33ad320
Add help 2023-10-30 15:50:54 +03:00
Nakidai 9529eebcbc
Fix configure 2023-10-30 15:50:53 +03:00
Nakidai 2e05977e12
Autocreate config dir attempt 2 2023-10-30 15:50:53 +03:00
Nakidai 5f6fc8f212
Autocreate config dir 2023-10-30 15:50:53 +03:00
Nakidai 317622a253
Add code configuraction in configure 2023-10-30 15:50:53 +03:00
Nakidai b23325bfec
Remove config from make clean 2023-10-30 15:50:53 +03:00
Nakidai 94d6a769a1
Move config to config/ 2023-10-30 15:50:53 +03:00
Nakidai 2ebe6fccea
Add configure for make 2023-10-30 15:50:52 +03:00
Nakidai d6b4f0be32
Some fixes
- Now every frame is cleared
- Fix collision with self
2023-10-30 15:35:15 +03:00
Nakidai 84e21cb515
Add move to start of screen every tick 2023-10-30 14:24:34 +03:00
Nakidai a064af95da
Add score and *the great line* 2023-10-30 03:53:48 +03:00
Nakidai b611b54575
Switch to threads.h 2023-10-30 02:51:46 +03:00
Nakidai 758dbf9cc2
Some fixes
- Fix UB in playerCreate (it had no return)
- Now Screen has field of Points, not chars
2023-10-30 02:36:03 +03:00
11 changed files with 33 additions and 33 deletions

1
.gitignore vendored
View File

@ -1,3 +1,2 @@
obj/
game
config/

View File

@ -1,11 +1,6 @@
include config/config.mk
include config.mk
OUT = game
CFLAGS =
LDFLAGS =
INCLUDE = -Iinclude -Iconfig
CC = cc
LD = ld
INCLUDE = -Iinclude
RM = rm -f
SRCDIR = src
OBJDIR = obj

4
config.mk Normal file
View File

@ -0,0 +1,4 @@
CC = cc
CFLAGS =
LDFLAGS =
OUT = game

14
configure vendored
View File

@ -14,7 +14,6 @@ then
fi
CC=${CC:-cc}
RM=${RM:-rm -f}
CFLAGS=${CFLAGS:-}
LDFLAGS=${LDFLAGS:-}
OUT=${OUT:-game}
@ -24,7 +23,6 @@ DEFY=${DEFY:-0}
echo "Makefile configuration:"
echo "Compiler: $CC"
echo "Remove: $RM"
echo "CFLAGS: $CFLAGS"
echo "LDFLAGS: $LDFLAGS"
echo "Out file: $OUT"
@ -34,19 +32,11 @@ echo "Size: $SIZE"
echo "Start x: $DEFX"
echo "Start y: $DEFY"
if ! test -d config; then
if test -f config; then
rm config
fi
mkdir config
fi
echo "CC = $CC
RM = $RM
CFLAGS = $CFLAGS
LDFLAGS = $LDFLAGS
OUT = $OUT" > config/config.mk
OUT = $OUT" > config.mk
echo "#define SIZE $SIZE
#define DEFX $DEFX
#define DEFY $DEFY" > config/config.h
#define DEFY $DEFY" > include/config.h

3
include/config.h Normal file
View File

@ -0,0 +1,3 @@
#define SIZE 10
#define DEFX 0
#define DEFY 0

View File

@ -9,6 +9,6 @@ typedef struct input_args_t
bool *alive;
} InputArgs;
void *input(void *vargp);
int input(void *vargp);
#endif /* __INPUT_H__ */

View File

@ -1,13 +1,13 @@
#ifndef __SCREEN_H__
#define __SCREEN_H__
typedef char Point;
typedef struct screen_t
{
int width;
int height;
char *screen;
Point *screen;
} Screen;
typedef char Point;
Screen *screenCreate(int width, int height, Point fill_value);
void screenFree(Screen *screen);

View File

@ -22,7 +22,7 @@ char getch(void)
return buf;
}
void *input(void *vargp)
int input(void *vargp)
{
char *out = ((InputArgs *)vargp)->out;
bool *alive = ((InputArgs *)vargp)->alive;
@ -31,5 +31,5 @@ void *input(void *vargp)
{
*out = getch();
}
return NULL;
return 0;
}

View File

@ -1,8 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <pthread.h>
#include <unistd.h>
#include <threads.h>
#include <time.h>
#include "input.h"
@ -28,13 +27,19 @@ Food generateFood(Player *player)
return food;
}
void resetCoordinates(void)
{
printf("\e[1;1H\e[2J");
}
int main(int argc, char **argv)
{
srandom(time(NULL));
Player *player = playerCreate(DOWN, DEFX, DEFY, 0);
Screen *screen = screenCreate(SIZE, SIZE, ' ');
PlayerNode *node;
pthread_t input_thread;
thrd_t input_thread;
int i;
int head_x, head_y;
Food food = generateFood(player);
@ -42,7 +47,7 @@ int main(int argc, char **argv)
char *key = malloc(sizeof(char)); *key = 0;
InputArgs input_args = (InputArgs){ key, running };
pthread_create(&input_thread, NULL, input, &input_args);
thrd_create(&input_thread, input, &input_args);
while (*running)
{
switch (*key)
@ -57,7 +62,6 @@ int main(int argc, char **argv)
player->direction = DOWN; break;
case 'a':
player->direction = LEFT; break;
sleep(1);
}
if (playerDoTick(player, food))
@ -78,9 +82,12 @@ int main(int argc, char **argv)
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);
sleep(1);
thrd_sleep(&(struct timespec){.tv_sec=1}, NULL);
}
return 0;
}

View File

@ -15,6 +15,8 @@ Player *playerCreate(Direction direction, int x, int y, int score)
player->head = head;
player->score = score;
player->direction = direction;
return player;
}
void playerFree(Player *player)
@ -34,7 +36,7 @@ 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)
if (nodei->x == nodej->x && nodei->y == nodej->y)
return true;
return false;
}

View File

@ -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));
}