From 743129a302b8f3e6ee52d526454eb6419ee9ff6c Mon Sep 17 00:00:00 2001 From: Mykola Syniavskyi Date: Fri, 2 May 2025 10:14:11 +0000 Subject: [PATCH] Aktualizovat a3/world.c --- a3/world.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/a3/world.c b/a3/world.c index e69de29..56c6d64 100644 --- a/a3/world.c +++ b/a3/world.c @@ -0,0 +1,34 @@ +#include "world.h" +#include + +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; + } +}