summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--body.c26
-rw-r--r--draw.c22
-rw-r--r--include/body.h2
3 files changed, 43 insertions, 7 deletions
diff --git a/body.c b/body.c
index 4c44fab..ad06411 100644
--- a/body.c
+++ b/body.c
@@ -19,6 +19,7 @@ typedef struct snake{
//private prototypes
NODE *makeNode(int x, int y);
void deleteNode(NODE *np);
+void increaseSnake(SNAKE *sp);
//public functions
SNAKE *makeSnake(int startx, int starty, int len){
@@ -59,7 +60,7 @@ void deleteSnake(SNAKE *sp){
return;
}
-void updateSnake(SNAKE *sp, int dir, int maxx, int maxy, bool *collision){
+void updateSnake(SNAKE *sp, int dir, int maxx, int maxy, bool *collision, int *fruit_loc, bool *fruit, int *score){
NODE *np;
if(sp == NULL) return;
@@ -95,7 +96,16 @@ void updateSnake(SNAKE *sp, int dir, int maxx, int maxy, bool *collision){
//update tail pointer
sp->tail = np;
- //TODO write checkSnake() which will check to see if the snake had a collision
+ //check if eating fruit
+ if(*fruit && sp->tail->x == *fruit_loc && sp->tail->y == *(fruit_loc + 1)){
+ *fruit = false;
+ (*score)++;
+
+ //need to increase snake size
+ increaseSnake(sp);
+ }
+
+ //check collision
*collision = checkSnake(sp, maxx, maxy);
return;
@@ -165,3 +175,15 @@ void deleteNode(NODE *np){
return;
}
+
+void increaseSnake(SNAKE *sp){
+ int xdiff = sp->head->next->x - sp->head->x;
+ int ydiff = sp->head->next->y - sp->head->y;
+ NODE *new = makeNode(sp->head->x - xdiff, sp->head->y - ydiff);
+
+ new->next = sp->head;
+ sp->head = new;
+ sp->length++;
+
+ return;
+}
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;
}
diff --git a/include/body.h b/include/body.h
index a1957bc..29241bf 100644
--- a/include/body.h
+++ b/include/body.h
@@ -8,7 +8,7 @@ typedef struct snake SNAKE;
SNAKE *makeSnake(int startx, int starty, int len);
void deleteSnake(SNAKE *sp);
-void updateSnake(SNAKE *sp, int dir, int maxx, int maxy, bool *collision);
+void updateSnake(SNAKE *sp, int dir, int maxx, int maxy, bool *collision, int *fruit_loc, bool *fruit, int *score);
bool checkSnake(SNAKE *sp, int maxx, int maxy);
int getSnakeTailx(SNAKE *sp);