From 6e262115dc576aad05ab383c3d62d1da0e9a082b Mon Sep 17 00:00:00 2001 From: lshprung Date: Fri, 18 Dec 2020 12:23:54 -0800 Subject: Added fruit and scoring functionality --- draw.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'draw.c') diff --git a/draw.c b/draw.c index 25355c7..5ffead6 100644 --- a/draw.c +++ b/draw.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "include/body.h" @@ -14,7 +15,7 @@ //#define DEBUG 1 void updateScore(int which, int score); -void updateScreen(SNAKE *sp, int dir, bool *collision); +void updateScreen(SNAKE *sp, int dir, bool *collision, int *fruit_loc, bool *fruit, int *score); #ifdef DEBUG void count_up(void); @@ -27,10 +28,14 @@ int main(){ int input; int dir; bool collision = false; + int fruit_loc[2]; //index 0 is x, 1 is y + bool fruit_bool = false; int i; //TODO consider removing score2 altogether + srand(time(NULL)); + //TODO implement code to retreive existing high score highscore = 0; @@ -87,7 +92,7 @@ int main(){ updateScore(0, score); updateScore(1, highscore); updateScore(2, score2); - updateScreen(player, dir, &collision); + updateScreen(player, dir, &collision, fruit_loc, &fruit_bool, &score); //TODO consider changing SPEED depending on if moving horizontally or vertically refresh(); usleep(SPEED); @@ -141,7 +146,7 @@ void updateScore(int which, int score){ return; } -void updateScreen(SNAKE *sp, int dir, bool *collision){ +void updateScreen(SNAKE *sp, int dir, bool *collision, int *fruit_loc, bool *fruit, int *score){ char eyes; //switch case to decide "eyes" char @@ -162,11 +167,20 @@ void updateScreen(SNAKE *sp, int dir, bool *collision){ attron(A_REVERSE); mvaddch(getSnakeTaily(sp), getSnakeTailx(sp), ' '); //update the snake position - updateSnake(sp, dir, getmaxx(stdscr), getmaxy(stdscr), collision); + updateSnake(sp, dir, getmaxx(stdscr), getmaxy(stdscr), collision, fruit_loc, fruit, score); //turn on front mvaddch(getSnakeTaily(sp), getSnakeTailx(sp), eyes); attroff(A_REVERSE); + //TODO add logic to prevent fruit from spawning inside snake + //draw a new fruit, if necessary (do not draw it over scoreboard) + if(!(*fruit)){ + *fruit_loc = rand() % getmaxx(stdscr); + *(fruit_loc+1) = rand() % (getmaxy(stdscr)-2) + 2; + mvaddch(*(fruit_loc+1), *fruit_loc, '@'); + *fruit = true; + } + return; } -- cgit