summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2023-02-06 13:29:55 -0800
committerKai Stevenson <kai@kaistevenson.com>2023-02-06 13:30:03 -0800
commit5f06a62aff1499ed0caf68ff7e551c5c02dded61 (patch)
tree9d7f1cd6e2d8ad8227fa21d7ebdb6ac03d92286b
parent28c2b30b4a0693cdc5ce6f6d013e5b4066d1a195 (diff)
dynamic tails
-rw-r--r--snake.c46
1 files changed, 38 insertions, 8 deletions
diff --git a/snake.c b/snake.c
index 7b286ea..e87660e 100644
--- a/snake.c
+++ b/snake.c
@@ -41,7 +41,7 @@ int main(int argc, char **argv) {
printf("for more information, see the license file.\n");
return 0;
}
- else if (strcmp("--help", argv[1]) == 0) {
+ else if(strcmp("--help", argv[1]) == 0) {
printf("usage: snake3 [--version] [--help]\n");
return 0;
}
@@ -113,8 +113,6 @@ 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;
}
- else {
- }
/* check if snake landed on the food */
int ateFood = 0;
if((snakePositions.vectors[0].mx + lastMoveDir.mx == foodPos.mx) && (snakePositions.vectors[0].my + lastMoveDir.my == foodPos.my)) {
@@ -209,14 +207,41 @@ void draw_board() {
for(int c = 0; c < boardSize; c++) {
/* determine the contents of the board position */
int type = 0;
+ /* 0=horizontal, 1=vertical, 2=diagonal slant right, 3=diagonal slant left */
+ int tailOrientation = 0;
for(int i = 0; i < snakePositions.length; i++) {
if(snakePositions.vectors[i].mx == c && snakePositions.vectors[i].my == r) {
- type = i ? 2 : 1;
+ if(i > 0) {
+ type = 2;
+ /* get the delta to the connecting tail */
+ int dx;
+ int dy;
+ if(i < snakePositions.length - 1) {
+ dx = snakePositions.vectors[i - 1].mx - snakePositions.vectors[i + 1].mx;
+ dy = snakePositions.vectors[i - 1].my - snakePositions.vectors[i + 1].my;
+ }
+ else {
+ dx = snakePositions.vectors[i - 1].mx - c;
+ dy = snakePositions.vectors[i - 1].my - r;
+ }
+ if(dy == 0) {
+ tailOrientation = 0;
+ }
+ else if(dx == 0) {
+ tailOrientation = 1;
+ }
+ else if((dx == 1 && dy == 1) || (dx == -1 && dy == -1)) {
+ tailOrientation = 3;
+ }
+ else if((dx == -1 && dy == 1) || (dx == 1 && dy == -1)) {
+ tailOrientation = 2;
+ }
+ }
+ else {
+ type = 1;
+ }
break;
}
- else {
- type = 0;
- }
}
if(foodPos.mx == c && foodPos.my == r) {
type = 3;
@@ -232,7 +257,12 @@ void draw_board() {
}
else if(type == 2) {
/* snake body */
- printf("%s* %s", GRN, COLOR_RESET);
+ char bodyChar = tailOrientation == 0 ? '-'
+ : tailOrientation == 1 ? '|'
+ : tailOrientation == 2 ? '/'
+ : tailOrientation == 3 ? '\\'
+ : '*';
+ printf("%s%c %s", GRN, bodyChar, COLOR_RESET);
}
else if(type == 3) {
/* food */