snake can eat, display score

This commit is contained in:
Noah Swerhun 2021-09-18 20:03:46 -05:00
parent 9feeb4e2d5
commit af579fb850
13 changed files with 55 additions and 7 deletions

View file

@ -9,10 +9,10 @@
#define quit_key 'q'
// Starting legnth of the snake
static const size_t starting_len = 15;
static const size_t starting_len = 3;
// Number of apples on the field
static const size_t n_apples = 10;
static const size_t n_apples = 5;
// Speed of snake (milliseconds between moves)
static const int screen_speed = 150;

BIN
nsnake

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -19,7 +19,7 @@ void delete_apple(Apple *a) {
free_s(a);
}
void apple_relocate(Apple *a, int x, int y) {
void apple_move(Apple *a, int x, int y) {
if (!a)
die("apple.c: apple_relocate(): 'a' is null pointer");

View file

@ -34,3 +34,22 @@ void field_print(Field *f) {
for (size_t i = 0; i < f->num; i++)
mvaddch(f->a[i]->y, f->a[i]->x, apple_ch);
}
int field_xy_is_apple(Field *f, int x, int y, size_t *p) {
for (size_t i = 0; i < f->num; i++) {
if (f->a[i]->x == x) {
if (f->a[i]->y == y) {
*p = i;
return 1;
}
}
}
return 0;
}
int field_relocate_apple(Field *f, size_t i) {
if (i > f->num)
return 0;
apple_move(f->a[i], irandom(5, f->x_max - 5), irandom(5, f->y_max - 5));
return 1;
}

View file

@ -9,6 +9,6 @@ typedef struct apple_T {
Apple *new_apple(int, int);
void delete_apple(Apple *);
void apple_relocate(Apple *, int, int);
void apple_move(Apple *, int, int);
#endif

View file

@ -14,5 +14,7 @@ Field *new_field(size_t, int, int);
void delete_field(Field *);
void field_print(Field *);
int field_xy_is_apple(Field *, int, int, size_t *);
int field_relocate_apple(Field *, size_t);
#endif

View file

@ -2,6 +2,7 @@
#define SNAKE_H
#include "segment.h"
#include "field.h"
enum dirn {
UP = 0,
@ -20,6 +21,6 @@ void delete_snake(Snake *);
void snake_forward(Snake *, int);
void snake_print(Snake *);
int snake_eat(Snake *);
int snake_eat(Snake *, Field *);
#endif

View file

@ -56,7 +56,6 @@ int main(void) {
clear();
refresh();
field_print(f);
switch (c) {
case up_key:
if (dirn != DOWN)
@ -78,8 +77,13 @@ int main(void) {
goto quit;
break;
}
snake_forward(s, dirn);
score += snake_eat(s, f);
mvprintw(getmaxy(stdscr) - 1, 0, "score: %d", score);
snake_print(s);
field_print(f);
refresh();
napms(screen_speed);

View file

@ -4,6 +4,8 @@
#include "include/main.h"
#include "include/segment.h"
#include "include/snake.h"
#include "include/field.h"
#include "include/apple.h"
#include "../config.h"
Snake *new_snake(size_t len, int x, int y) {
@ -60,4 +62,24 @@ void snake_print(Snake *s) {
mvaddch(s->n[i]->y, s->n[i]->x, snake_ch);
}
int snake_eat(Snake *);
int snake_eat(Snake *s, Field *f) {
size_t i;
Segment *c;
if (field_xy_is_apple(f, s->n[0]->x, s->n[0]->y, &i)) {
if (!field_relocate_apple(f, i))
return 0;
c = new_segment(s->n[s->len - 1]->prev_x, s->n[s->len - 1]->prev_y);
s->n = (Segment **)realloc(s->n, sizeof(Segment *) * ++(s->len));
if (!s->n)
die("snake.c: snake_eat(): 's->n' is null pointer");
s->n[s->len - 1] = c;
return 1;
}
return 0;
}