This commit is contained in:
Roman Khaliavka 2025-04-23 14:17:10 +00:00
parent 2c557acd39
commit c4abb93dd5

52
a3/world.c Normal file
View File

@ -0,0 +1,52 @@
#include <ncurses.h>
#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;
}
}