movement completed

This commit is contained in:
Noah Swerhun 2021-09-17 22:35:02 -05:00
parent 2229fcd888
commit 8ad2e994e7
13 changed files with 147 additions and 18 deletions

View file

@ -11,10 +11,10 @@ INC = $(wildcard $(INCDIR)/*.h)
_OBJ = $(SRC:.c=.o) _OBJ = $(SRC:.c=.o)
OBJ = $(subst $(SRCDIR),$(OBJDIR),$(_OBJ)) OBJ = $(subst $(SRCDIR),$(OBJDIR),$(_OBJ))
$(OUTFILE): $(OBJ) $(OUTFILE): $(OBJ) config.h
$(CC) $(CFLAGS) -o $(OUTFILE) $(OBJ) $(CC) $(CFLAGS) -o $(OUTFILE) $(OBJ)
$(OBJDIR)/%.o: $(SRCDIR)/%.c $(INCDIR)/%.h $(OBJDIR)/%.o: $(SRCDIR)/%.c $(INCDIR)/%.h config.h
@mkdir -p $(@D) @mkdir -p $(@D)
$(CC) $(CFLAGS) -o $@ -c $< $(CC) $(CFLAGS) -o $@ -c $<

20
config.h Normal file
View file

@ -0,0 +1,20 @@
#ifndef CONFIG_H
#define CONFIG_H
// Keybinds
#define up_key 'k'
#define down_key 'j'
#define right_key 'l'
#define left_key 'h'
#define quit_key 'q'
// The starting legnth of the snake
static const size_t starting_len = 15;
// Speed of snake (milliseconds between moves)
static const int screen_speed = 150;
// Character for each snake segment
static const char snake_ch = '#';
#endif

BIN
nsnake Executable file

Binary file not shown.

BIN
obj/apple.o Normal file

Binary file not shown.

BIN
obj/main.o Normal file

Binary file not shown.

BIN
obj/segment.o Normal file

Binary file not shown.

BIN
obj/snake.o Normal file

Binary file not shown.

View file

@ -0,0 +1,20 @@
#include <stdlib.h>
#include "include/main.h"
#include "include/apple.h"
Apple *new_apple(int x, int y) {
Apple *a;
a = (Apple *)malloc(sizeof(Apple));
if (!a)
die("apple.c: new_apple(): 'a' is null pointer");
a->x = x;
a->y = y;
return a;
}
void delete_apple(Apple *a) {
free_s(a);
}

View file

@ -0,0 +1,12 @@
#ifndef APPLE_H
#define APPLE_H
typedef struct apple_T {
int x;
int y;
} Apple;
Apple *new_apple(int, int);
void delete_apple(Apple *);
#endif

View file

@ -1,6 +1,8 @@
#ifndef SNAKE_H #ifndef SNAKE_H
#define SNAKE_H #define SNAKE_H
#include "segment.h"
enum dirn { enum dirn {
UP = 0, UP = 0,
DOWN, DOWN,
@ -9,15 +11,15 @@ enum dirn {
}; };
typedef struct snake_T { typedef struct snake_T {
Segments **n; Segment **n;
size_t len; size_t len;
int head_dirn;
} Snake; } Snake;
Snake *new_snake(size_t, int, int, int); Snake *new_snake(size_t, int, int);
void *delete_snake(Snake *); void delete_snake(Snake *);
void snake_forward(Snake *, int); void snake_forward(Snake *, int);
void snake_print(Snake *);
int snake_eat(Snake *); int snake_eat(Snake *);
#endif #endif

View file

@ -1,8 +1,11 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <ncurses.h>
#include "include/main.h" #include "include/main.h"
#include "include/snake.h"
#include "../config.h"
void die(char *fmt, ...) { void die(char *fmt, ...) {
va_list args; va_list args;
@ -12,7 +15,7 @@ void die(char *fmt, ...) {
vfprintf(stderr, fmt, args); vfprintf(stderr, fmt, args);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
va_end(args) va_end(args);
exit(1); exit(1);
} }
@ -20,3 +23,39 @@ void free_s(void *p) {
free(p); free(p);
p = NULL; p = NULL;
} }
int main(void) {
int c, dirn;
Snake *s;
// init ncurses
initscr();
keypad(stdscr, true);
noecho();
cbreak();
nodelay(stdscr, true);
curs_set(0);
s = new_snake(starting_len, getmaxx(stdscr) / 2, getmaxy(stdscr) / 2);
dirn = UP;
while ((c = getch())) {
switch (c) {
case up_key: dirn = UP; break;
case down_key: dirn = DOWN; break;
case right_key: dirn = RIGHT; break;
case left_key: dirn = LEFT; break;
case quit_key: goto quit;
}
snake_forward(s, dirn);
clear();
refresh();
snake_print(s);
refresh();
napms(screen_speed);
}
quit:
delete_snake(s);
endwin();
}

View file

@ -1,10 +1,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include "include/main.h" #include "include/main.h"
#include "include/segment.h" #include "include/segment.h"
Segment *new_segment(int x, int y) { Segment *new_segment(int x, int y) {
Segment c; Segment *c;
c = (Segment *)malloc(sizeof(Segment)); c = (Segment *)malloc(sizeof(Segment));
if (!c) if (!c)
die("segment.c: new_segment(): 'c' is null pointer"); die("segment.c: new_segment(): 'c' is null pointer");

View file

@ -1,23 +1,58 @@
#include <stdlib.h> #include <stdlib.h>
#include <ncurses.h>
#include "include/main.h" #include "include/main.h"
#include "include/segment.h" #include "include/segment.h"
#include "include/snake.h" #include "include/snake.h"
#include "../config.h"
Snake *new_snake(size_t len, int head_dirn, int x, int y) { Snake *new_snake(size_t len, int x, int y) {
Snake *s; Snake *s;
s = (Snake *)malloc(sizeof(Snake)); s = (Snake *)malloc(sizeof(Snake)); if (!s)
if (!s)
die("snake.c: new_snake(): 's' is null pointer"); die("snake.c: new_snake(): 's' is null pointer");
s->n = (Segment **)malloc(sizeof(Segment *) * len); s->len = len;
s->n = (Segment **)malloc(sizeof(Segment *) * s->len);
if (!s->n) if (!s->n)
die("snake.c: new_snake(): 's->n' is null pointer"); die("snake.c: new_snake(): 's->n' is null pointer");
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < s->len; i++)
s->*n[i] = new_segment(x, y--); s->n[i] = new_segment(x, y--);
}
} return s;
void *delete_snake(Snake *); }
void delete_snake(Snake *s) {
for (size_t i = 0; i < s->len; i++)
delete_segment(s->n[i]);
free_s(s);
}
void snake_forward(Snake *s , int dirn) {
if (!s)
die("snake.c: snake_forward(): 's' is null pointer");
switch (dirn) {
case UP:
segment_move(s->n[0], s->n[0]->x, s->n[0]->y - 1);
break;
case DOWN:
segment_move(s->n[0], s->n[0]->x, s->n[0]->y + 1);
break;
case RIGHT:
segment_move(s->n[0], s->n[0]->x + 1, s->n[0]->y);
break;
case LEFT:
segment_move(s->n[0], s->n[0]->x - 1, s->n[0]->y);
break;
}
for (size_t i = 1; i < s->len; i++)
segment_move(s->n[i], s->n[i - 1]->prev_x,
s->n[i - 1]->prev_y);
}
void snake_print(Snake *s) {
for (size_t i = 0; i < s->len; i++)
mvaddch(s->n[i]->y, s->n[i]->x, snake_ch);
}
void snake_forward(Snake *, int);
int snake_eat(Snake *); int snake_eat(Snake *);