Browse Source

refactoring: drawing snake

remotes/origin/David
David Moeller 11 months ago
parent
commit
d1418a1344
  1. 29
      src/main/c/Snake/snake_start.c
  2. 6
      src/main/c/Snake/snake_start.h

29
src/main/c/Snake/snake_start.c

@ -17,6 +17,7 @@ void main_menu();
void game();
Snake initialize_game();
void draw(Snake *snake);
int part_of_snake(Snake *snake, unsigned char tile);
void snake_start(){
@ -83,24 +84,24 @@ void draw(Snake *snake){
for(int i = 1; i <= HEIGHT; i++){
printf("|");
for(int j = 1; j <= WIDTH; j++){
//printf("%d:", i * 16 + j);
for(int pos = 0; pos < snake->length; pos++){
//printf(" %d", snake->segments[pos]);
if(i * 16 + j == snake->segments[pos]){
if(pos == 0){
printf("0");
}else{
printf("O");
}
break;
}else if(pos == snake->length - 1){
printf(" ");
}
}
int index = part_of_snake(snake, i * 16 + j);
if(index == 0){printf("0");}
else if(index > 0){printf("O");}
else{printf(" ");}
}
printf("|\n");
}
printf("+");
for(int i = 0; i < WIDTH; i++){printf("-");}
printf("+\n");
}
//returns index of segments which is identical to tile; -1 if not found
int part_of_snake(Snake *snake, unsigned char tile){
for(int i = 0; i < snake->length; i++){
if(snake->segments[i] == tile){
return i;
}
}
return -1;
}

6
src/main/c/Snake/snake_start.h

@ -0,0 +1,6 @@
#ifndef SNAKE_START_H
#define SNAKE_START_H
void snake_start();
#endif // SNAKE_START_H
Loading…
Cancel
Save