Aktualizovat a3/world.c

This commit is contained in:
Mykola Syniavskyi 2025-05-02 10:14:11 +00:00
parent dee86b1655
commit 743129a302

View File

@ -0,0 +1,34 @@
#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;
}
}