35 lines
784 B
C
35 lines
784 B
C
#include "world.h"
|
|
#include <stdlib.h>
|
|
|
|
struct state* create_world(int width, int height) {
|
|
struct state* st = malloc(sizeof(struct state));
|
|
if (st == NULL) {
|
|
return NULL;
|
|
}
|
|
st->width = width;
|
|
st->height = height;
|
|
st->sx = 1;
|
|
st->sy = 0;
|
|
st->snake = add_snake(NULL, width / 2, height / 2);
|
|
|
|
for (int i = 0; i < FOOD_COUNT; i++) {
|
|
st->foodx[i] = rand() % width;
|
|
st->foody[i] = rand() % height;
|
|
}
|
|
return st;
|
|
}
|
|
|
|
void free_world(struct state* st) {
|
|
if (st != NULL) {
|
|
free_snake(st->snake);
|
|
free(st);
|
|
}
|
|
}
|
|
|
|
void change_direction(struct state* st, int dx, int dy) {
|
|
if (st->sx != -dx || st->sy != -dy) {
|
|
st->sx = dx;
|
|
st->sy = dy;
|
|
}
|
|
}
|