apple field added
This commit is contained in:
parent
8ad2e994e7
commit
68c2ae2498
13 changed files with 97 additions and 5 deletions
7
config.h
7
config.h
|
@ -8,13 +8,18 @@
|
||||||
#define left_key 'h'
|
#define left_key 'h'
|
||||||
#define quit_key 'q'
|
#define quit_key 'q'
|
||||||
|
|
||||||
// The starting legnth of the snake
|
// Starting legnth of the snake
|
||||||
static const size_t starting_len = 15;
|
static const size_t starting_len = 15;
|
||||||
|
|
||||||
|
// Number of apples on the field
|
||||||
|
static const size_t n_apples = 10;
|
||||||
|
|
||||||
// Speed of snake (milliseconds between moves)
|
// Speed of snake (milliseconds between moves)
|
||||||
static const int screen_speed = 150;
|
static const int screen_speed = 150;
|
||||||
|
|
||||||
// Character for each snake segment
|
// Character for each snake segment
|
||||||
static const char snake_ch = '#';
|
static const char snake_ch = '#';
|
||||||
|
// Character for apples
|
||||||
|
static const char apple_ch = '*';
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
BIN
nsnake
BIN
nsnake
Binary file not shown.
BIN
obj/apple.o
BIN
obj/apple.o
Binary file not shown.
BIN
obj/field.o
Normal file
BIN
obj/field.o
Normal file
Binary file not shown.
BIN
obj/main.o
BIN
obj/main.o
Binary file not shown.
BIN
obj/snake.o
BIN
obj/snake.o
Binary file not shown.
|
@ -18,3 +18,11 @@ Apple *new_apple(int x, int y) {
|
||||||
void delete_apple(Apple *a) {
|
void delete_apple(Apple *a) {
|
||||||
free_s(a);
|
free_s(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void apple_relocate(Apple *a, int x, int y) {
|
||||||
|
if (!a)
|
||||||
|
die("apple.c: apple_relocate(): 'a' is null pointer");
|
||||||
|
|
||||||
|
a->x = x;
|
||||||
|
a->y = y;
|
||||||
|
}
|
||||||
|
|
36
src/field.c
Normal file
36
src/field.c
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <ncurses.h>
|
||||||
|
|
||||||
|
#include "include/main.h"
|
||||||
|
#include "include/field.h"
|
||||||
|
#include "include/apple.h"
|
||||||
|
#include "../config.h"
|
||||||
|
|
||||||
|
Field *new_field(size_t num, int x_max, int y_max) {
|
||||||
|
Field *f;
|
||||||
|
f = (Field *)malloc(sizeof(Field));
|
||||||
|
if (!f)
|
||||||
|
die("field.c: new_field(): 'f' is null pointer");
|
||||||
|
|
||||||
|
f->num = num;
|
||||||
|
f->x_max = x_max;
|
||||||
|
f->y_max = y_max;
|
||||||
|
f->a = (Apple **)malloc(sizeof(Apple *) * f->num);
|
||||||
|
if (!f->a)
|
||||||
|
die("field.c: new_field(): 'f->a' is null pointer");
|
||||||
|
for (size_t i = 0; i < f->num; i++)
|
||||||
|
f->a[i] = new_apple(irandom(5, f->x_max - 5), irandom(5, f->y_max - 5));
|
||||||
|
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
void delete_field(Field *f) {
|
||||||
|
for (size_t i = 0; i < f->num; i++)
|
||||||
|
delete_apple(f->a[i]);
|
||||||
|
free_s(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
|
@ -9,4 +9,6 @@ typedef struct apple_T {
|
||||||
Apple *new_apple(int, int);
|
Apple *new_apple(int, int);
|
||||||
void delete_apple(Apple *);
|
void delete_apple(Apple *);
|
||||||
|
|
||||||
|
void apple_relocate(Apple *, int, int);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
18
src/include/field.h
Normal file
18
src/include/field.h
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
#ifndef FIELD_H
|
||||||
|
#define FIELD_H
|
||||||
|
|
||||||
|
#include "apple.h"
|
||||||
|
|
||||||
|
typedef struct field_T {
|
||||||
|
Apple **a;
|
||||||
|
size_t num;
|
||||||
|
int x_max;
|
||||||
|
int y_max;
|
||||||
|
} Field;
|
||||||
|
|
||||||
|
Field *new_field(size_t, int, int);
|
||||||
|
void delete_field(Field *);
|
||||||
|
|
||||||
|
void field_print(Field *);
|
||||||
|
|
||||||
|
#endif
|
|
@ -3,5 +3,6 @@
|
||||||
|
|
||||||
void die(char *, ...);
|
void die(char *, ...);
|
||||||
void free_s(void *);
|
void free_s(void *);
|
||||||
|
int irandom(int, int);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
23
src/main.c
23
src/main.c
|
@ -2,9 +2,11 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include "include/main.h"
|
#include "include/main.h"
|
||||||
#include "include/snake.h"
|
#include "include/snake.h"
|
||||||
|
#include "include/field.h"
|
||||||
#include "../config.h"
|
#include "../config.h"
|
||||||
|
|
||||||
void die(char *fmt, ...) {
|
void die(char *fmt, ...) {
|
||||||
|
@ -24,9 +26,18 @@ void free_s(void *p) {
|
||||||
p = NULL;
|
p = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int irandom(int lower, int upper) {
|
||||||
|
int i;
|
||||||
|
i = (rand() % (upper - lower + 1)) + lower;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
int c, dirn;
|
int c, dirn, score;
|
||||||
Snake *s;
|
Snake *s;
|
||||||
|
Field *f;
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
// init ncurses
|
// init ncurses
|
||||||
initscr();
|
initscr();
|
||||||
|
@ -37,9 +48,15 @@ int main(void) {
|
||||||
curs_set(0);
|
curs_set(0);
|
||||||
|
|
||||||
s = new_snake(starting_len, getmaxx(stdscr) / 2, getmaxy(stdscr) / 2);
|
s = new_snake(starting_len, getmaxx(stdscr) / 2, getmaxy(stdscr) / 2);
|
||||||
|
f = new_field(n_apples, getmaxx(stdscr), getmaxy(stdscr));
|
||||||
dirn = UP;
|
dirn = UP;
|
||||||
|
score = 0;
|
||||||
|
|
||||||
while ((c = getch())) {
|
while ((c = getch())) {
|
||||||
|
clear();
|
||||||
|
refresh();
|
||||||
|
|
||||||
|
field_print(f);
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case up_key: dirn = UP; break;
|
case up_key: dirn = UP; break;
|
||||||
case down_key: dirn = DOWN; break;
|
case down_key: dirn = DOWN; break;
|
||||||
|
@ -48,14 +65,14 @@ int main(void) {
|
||||||
case quit_key: goto quit;
|
case quit_key: goto quit;
|
||||||
}
|
}
|
||||||
snake_forward(s, dirn);
|
snake_forward(s, dirn);
|
||||||
clear();
|
|
||||||
refresh();
|
|
||||||
snake_print(s);
|
snake_print(s);
|
||||||
|
|
||||||
refresh();
|
refresh();
|
||||||
napms(screen_speed);
|
napms(screen_speed);
|
||||||
}
|
}
|
||||||
|
|
||||||
quit:
|
quit:
|
||||||
delete_snake(s);
|
delete_snake(s);
|
||||||
|
delete_field(f);
|
||||||
endwin();
|
endwin();
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ Snake *new_snake(size_t len, int x, int y) {
|
||||||
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 < s->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;
|
return s;
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,7 @@ void delete_snake(Snake *s) {
|
||||||
void snake_forward(Snake *s , int dirn) {
|
void snake_forward(Snake *s , int dirn) {
|
||||||
if (!s)
|
if (!s)
|
||||||
die("snake.c: snake_forward(): 's' is null pointer");
|
die("snake.c: snake_forward(): 's' is null pointer");
|
||||||
|
|
||||||
switch (dirn) {
|
switch (dirn) {
|
||||||
case UP:
|
case UP:
|
||||||
segment_move(s->n[0], s->n[0]->x, s->n[0]->y - 1);
|
segment_move(s->n[0], s->n[0]->x, s->n[0]->y - 1);
|
||||||
|
@ -44,6 +45,7 @@ void snake_forward(Snake *s , int dirn) {
|
||||||
segment_move(s->n[0], s->n[0]->x - 1, s->n[0]->y);
|
segment_move(s->n[0], s->n[0]->x - 1, s->n[0]->y);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 1; i < s->len; i++)
|
for (size_t i = 1; i < s->len; i++)
|
||||||
segment_move(s->n[i], s->n[i - 1]->prev_x,
|
segment_move(s->n[i], s->n[i - 1]->prev_x,
|
||||||
s->n[i - 1]->prev_y);
|
s->n[i - 1]->prev_y);
|
||||||
|
@ -51,6 +53,9 @@ void snake_forward(Snake *s , int dirn) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void snake_print(Snake *s) {
|
void snake_print(Snake *s) {
|
||||||
|
if (!s)
|
||||||
|
die("snake.c: snake_print(): 's' is null pointer");
|
||||||
|
|
||||||
for (size_t i = 0; i < s->len; i++)
|
for (size_t i = 0; i < s->len; i++)
|
||||||
mvaddch(s->n[i]->y, s->n[i]->x, snake_ch);
|
mvaddch(s->n[i]->y, s->n[i]->x, snake_ch);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue