summaryrefslogtreecommitdiff
path: root/draw.c
diff options
context:
space:
mode:
authorlshprung <lshprung@yahoo.com>2020-12-18 12:23:54 -0800
committerlshprung <lshprung@yahoo.com>2020-12-18 12:23:54 -0800
commit6e262115dc576aad05ab383c3d62d1da0e9a082b (patch)
treec69eaec5e7322e4341b42e344657e6df4d1d0d13 /draw.c
parentbc5885c888f0a4df03a455e1b5c70c2307c676b1 (diff)
Added fruit and scoring functionality
Diffstat (limited to 'draw.c')
-rw-r--r--draw.c22
1 files changed, 18 insertions, 4 deletions
diff --git a/draw.c b/draw.c
index 25355c7..5ffead6 100644
--- a/draw.c
+++ b/draw.c
@@ -2,6 +2,7 @@
#include <ncurses.h>
#include <stdbool.h>
#include <stdlib.h>
+#include <time.h>
#include <unistd.h>
#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;
}