summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/snake3.14
-rw-r--r--snake.c14
2 files changed, 17 insertions, 1 deletions
diff --git a/doc/snake3.1 b/doc/snake3.1
index 4659ba9..62ac0cf 100644
--- a/doc/snake3.1
+++ b/doc/snake3.1
@@ -23,6 +23,10 @@ keys to navigate. The game becomes faster as you grow.
\fB--speed\fP
\-\- Sets the speed of the snake - up to 10.
+
+\fB--obstacles\fP
+\-\- Sets the amount of obstacles that are generated.
+
.SH AUTHORS
Developed and maintained by Kai Stevenson, with features added by Preston
Pan.
diff --git a/snake.c b/snake.c
index f82642b..bfd1bf7 100644
--- a/snake.c
+++ b/snake.c
@@ -164,6 +164,7 @@ int main(int argc, char **argv) {
while(1) {
/* handle ctrl-c interrupt */
if(intCtrlC) {
+EXIT:
/* unhide cursor */
printf("\033[?25h");
/* kill the listener thread */
@@ -189,8 +190,14 @@ int main(int argc, char **argv) {
if((dir.mx != 0 || dir.my != 0) && !(dir.mx == 1 && lastMoveDir.mx == -1) && !(dir.mx == -1 && lastMoveDir.mx == 1) && !(dir.my == 1 && lastMoveDir.my == -1) && !(dir.my == -1 && lastMoveDir.my == 1)) {
lastMoveDir = dir;
}
- /* check if snake landed on the food */
+ /* check if snake lands on obstacle */
+ for (int i = 0; i < obstaclePositions.length; i ++) {
+ if ((snakePositions.vectors[0].mx + lastMoveDir.mx == obstaclePositions.vectors[i].mx) && (snakePositions.vectors[0].my + lastMoveDir.my == obstaclePositions.vectors[i].mx)) {
+ goto EXIT;
+ }
+ }
int ateFood = 0;
+ /* check if snake landed on the food */
if((snakePositions.vectors[0].mx + lastMoveDir.mx == foodPos.mx) && (snakePositions.vectors[0].my + lastMoveDir.my == foodPos.my)) {
ateFood = 1;
score += 1;
@@ -207,6 +214,11 @@ int main(int argc, char **argv) {
overlap = 1;
}
}
+ for (int i = 0; i < obstaclePositions.length; i ++) {
+ if ((obstaclePositions.vectors[i].mx == foodPos.mx) && (obstaclePositions.vectors[i].my == foodPos.my)) {
+ overlap = 1;
+ }
+ }
if(!overlap) {
break;
}