69 lines
1.3 KiB
C
69 lines
1.3 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include "snake.h"
|
|
#include "world.h"
|
|
|
|
void render(const struct state* state){
|
|
system("clear");
|
|
|
|
for(int y = 0; y < state->height; y++){
|
|
for(int x = 0; x < state->width; x++){
|
|
if(is_snake(state->snake, x, y)){
|
|
printf("O");
|
|
}else if(is_food_at(state, x, y)){
|
|
printf("@");
|
|
}else{
|
|
printf(" ");
|
|
}
|
|
}
|
|
printf("\n");
|
|
}
|
|
}
|
|
|
|
int get_key(){
|
|
int ch;
|
|
if (scanf("%c", &ch) != 1){
|
|
return -1;
|
|
}
|
|
return ch;
|
|
}
|
|
|
|
int main(){
|
|
struct state game;
|
|
init_game(&game);
|
|
int ch;
|
|
while(1){
|
|
render(&game);
|
|
ch = get_key();
|
|
|
|
if(ch == 'q'){
|
|
break;
|
|
}
|
|
|
|
if(ch == 'w'){
|
|
game.sy = -1;
|
|
game.sx = 0;
|
|
}else if(ch == 's'){
|
|
game.sy = 1;
|
|
game.sx = 0;
|
|
}else if(ch == 'a'){
|
|
game.sx = -1;
|
|
game.sy = 0;
|
|
}else if(ch == 'd'){
|
|
game.sx = 1;
|
|
game.sy = 0;
|
|
}
|
|
|
|
int result = step_state(&game);
|
|
if (result != END_CONTINUE) {
|
|
break;
|
|
}
|
|
|
|
usleep(100000);
|
|
}
|
|
|
|
printf("Game Over\n");
|
|
return 0;
|
|
}
|