diff options
| author | Preston Pan <preston@nullring.xyz> | 2023-02-06 15:30:56 -0800 |
|---|---|---|
| committer | Preston Pan <preston@nullring.xyz> | 2023-02-06 15:30:56 -0800 |
| commit | e02ba6dc7511c46f017e22f348ac75974f93e15a (patch) | |
| tree | 7463bc61af1788152082f19f9d8d737cb1c13b89 | |
| parent | b0ae48bbc3761820021db07cfec8e06776f0b639 (diff) | |
obstacles almost add collision
| -rw-r--r-- | doc/snake3.1 | 4 | ||||
| -rw-r--r-- | snake.c | 14 |
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. @@ -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; } |
