Update 'du7/snake.c'
This commit is contained in:
parent
162d18ab49
commit
054c9c9036
20
du7/snake.c
20
du7/snake.c
@ -35,7 +35,7 @@ struct snake* remove_snake(struct snake* snake){
|
|||||||
|
|
||||||
|
|
||||||
void free_snake(struct snake* sn){
|
void free_snake(struct snake* sn){
|
||||||
struct snake* current = snake;
|
struct snake* current = sn;
|
||||||
while (current != NULL) {
|
while (current != NULL) {
|
||||||
struct snake* next = current->next;
|
struct snake* next = current->next;
|
||||||
free(current);
|
free(current);
|
||||||
@ -60,22 +60,22 @@ int is_snake(struct snake* snake,int x,int y){
|
|||||||
int step_state(struct state* st){
|
int step_state(struct state* st){
|
||||||
int nx = (st->snake->x + st->sx);
|
int nx = (st->snake->x + st->sx);
|
||||||
int ny = (st->snake->y + st->sy);
|
int ny = (st->snake->y + st->sy);
|
||||||
if (new_head_x < 0 || new_head_x >= state->width || new_head_y < 0 || new_head_y >= state->height) {
|
if (nx < 0 || nx >= st->width || ny < 0 || ny >= st->height) {
|
||||||
return END_WALL;
|
return END_WALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if snake hit itself
|
// Check if snake hit itself
|
||||||
if (is_snake(state->snake, new_head_x, new_head_y)) {
|
if (is_snake(st->snake, nx, ny)) {
|
||||||
return END_SNAKE;
|
return END_SNAKE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if snake ate food
|
// Check if snake ate food
|
||||||
int food_eaten = 0;
|
int food_eaten = 0;
|
||||||
for (int i = 0; i < FOOD_COUNT; i++) {
|
for (int i = 0; i < FOOD_COUNT; i++) {
|
||||||
if (state->foodx[i] >= 0 && state->foodx[i] == new_head_x && state->foody[i] == new_head_y) {
|
if (st->foodx[i] >= 0 && st->foodx[i] == nx && st->foody[i] == ny) {
|
||||||
state->foodx[i] = -1;
|
st->foodx[i] = -1;
|
||||||
state->foody[i] = -1;
|
st->foody[i] = -1;
|
||||||
state->snake = add_snake(state->snake, new_head_x, new_head_y);
|
st->snake = add_snake(st->snake, nx, ny);
|
||||||
food_eaten = 1;
|
food_eaten = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -85,7 +85,7 @@ int step_state(struct state* st){
|
|||||||
if (!food_eaten) {
|
if (!food_eaten) {
|
||||||
int food_left = 0;
|
int food_left = 0;
|
||||||
for (int i = 0; i < FOOD_COUNT; i++) {
|
for (int i = 0; i < FOOD_COUNT; i++) {
|
||||||
if (state->foodx[i] >= 0) {
|
if (st->foodx[i] >= 0) {
|
||||||
food_left = 1;
|
food_left = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -94,9 +94,9 @@ int step_state(struct state* st){
|
|||||||
return END_FOOD;
|
return END_FOOD;
|
||||||
}
|
}
|
||||||
// Remove the last snake part
|
// Remove the last snake part
|
||||||
state->snake = remove_snake(state->snake);
|
st->snake = remove_snake(st->snake);
|
||||||
// Add new snake part
|
// Add new snake part
|
||||||
state->snake = add_snake(state->snake, new_head_x, new_head_y);
|
st->snake = add_snake(st->snake, nx, ny);
|
||||||
}
|
}
|
||||||
|
|
||||||
return END_CONTINUE;
|
return END_CONTINUE;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user