diff --git a/a3/world.c b/a3/world.c new file mode 100644 index 0000000..60d2800 --- /dev/null +++ b/a3/world.c @@ -0,0 +1,52 @@ +#include +#include "world.h" + +void render(const struct state* state){ + clear(); + + for(int x = 0; x <= state->width + 1; x++){ + mvaddch(0, x, '#'); + mvaddch(state->height + 1, x, '#'); + } + + for(int y = 1; y <= state->height; y++){ + mvaddch(y, 0, '#'); + mvaddch(y, state->width + 1, '#'); + } + + for(int y = 0; y < state->height; y++){ + for(int x = 0; x < state->width; x++){ + int draw_x = x + 1; + int draw_y = y + 1; + + if(is_snake(state->snake, x, y)){ + attron(COLOR_PAIR(1)); + mvaddch(draw_y, draw_x, 'O'); + attroff(COLOR_PAIR(1)); + }else{ + int food = 0; + for(int i = 0; i < FOOD_COUNT; i++){ + if (state->foodx[i] == x && state->foody[i] == y) + food = 1; + } + + if(food){ + attron(COLOR_PAIR(2)); + mvaddch(draw_y, draw_x, '@'); + attroff(COLOR_PAIR(2)); + }else{ + mvaddch(draw_y, draw_x, ' '); + } + } + } + } + + refresh(); +} + +void place_food(struct state* state){ + for(int i = 0; i < FOOD_COUNT; i++){ + state->foodx[i] = rand() % state->width; + state->foody[i] = rand() % state->height; + } +}