curssses

Snake game for the linux terminal.
git clone git://git.amin.space/curssses.git
Log | Files | Refs | LICENSE

commit 34694d5fea2623a134fcbb8317480695caa8df80
parent e01474c59d233cb31cac5967682a11fc308ba1e1
Author: amin <dev@aminmesbah.com>
Date:   Wed,  7 Dec 2016 19:52:39 +0000

Add edible food that makes the snake grow.

FossilOrigin-Name: 21f4330cc24d2acf3308e0defbf3bba2710ff34e324c5c6c326beca500912800
Diffstat:
Mcurssses.c | 40+++++++++++++++++++++++++++++++++++-----
1 file changed, 35 insertions(+), 5 deletions(-)

diff --git a/curssses.c b/curssses.c @@ -42,6 +42,15 @@ struct Snake Segment *tail; }; +typedef struct Food Food; +struct Food +{ + bool eaten; + char symbol; + int x; + int y; +}; + Snake* snake_init(void) { Segment *first = malloc(sizeof(Segment)); @@ -123,6 +132,14 @@ void snake_move(Snake *s) } } +void food_move(Food *f) +{ + f->symbol = '#'; + f->x = rand() % COLS; + f->y = rand() % LINES; + f->eaten = false; +} + uint64_t get_current_time_ms(void) { struct timespec current; @@ -144,11 +161,12 @@ int main(void) keypad(stdscr, TRUE); curs_set(0); + srand((unsigned int)time(0)); + Snake *s = snake_init(); - for (int i = 0; i < 50; ++i) - { - snake_add_segment(s); - } + + Food f; + food_move(&f); bool debug = false; int input = 0; @@ -208,6 +226,15 @@ int main(void) if (updates % UPDATES_PER_MOVEMENT == 0) { snake_move(s); + if (s->head->x == f.x && s->head->y == f.y) + { + f.eaten = true; + snake_add_segment(s); + } + if (f.eaten) + { + food_move(&f); + } movements++; } updates++; @@ -221,9 +248,11 @@ int main(void) mvprintw( 0, 0, "Frame Time: %dms\nLag: %dms\nUpdates: %d\nFrames: %d\nMovements: %d\n" + "Food: (%d, %d)\n" "Screen: [%d, %d]\nSnake Length: %d\nHead: (%d, %d)\nTail: (%d, %d)\n", elapsed_ms, lag, updates, frames, movements, + f.x, f.y, COLS, LINES, s->length, s->head->x, s->head->y, @@ -237,11 +266,12 @@ int main(void) mvaddch(current->y, current->x, s->symbol); if (debug) { - mvprintw((i%20)+9, (i/20)*25, "Segment %d (%d, %d)", i+1, current->x, current->y); + mvprintw((i%20)+10, (i/20)*25, "Segment %d (%d, %d)", i+1, current->x, current->y); } current = current->next; i++; } + mvaddch(f.y, f.x, f.symbol); refresh(); frames++;