This commit is contained in:
Roman Khaliavka 2025-04-23 14:42:36 +00:00
parent 380c0a5779
commit d452cb33e3

View File

@ -1,52 +1,21 @@
#include <ncurses.h> #include <stdio.h>
#include "world.h" #include <stdlib.h>
#include "snake.h"
void render(const struct state* state){ void render(const struct state* state) {
clear(); system("clear");
for(int x = 0; x <= state->width + 1; x++){ // Малюємо поле
mvaddch(0, x, '#'); for (int y = 0; y < state->height; y++) {
mvaddch(state->height + 1, x, '#'); for (int x = 0; x < state->width; x++) {
} if (is_snake(state->snake, x, y)) {
printf("O");
for(int y = 1; y <= state->height; y++){ } else if (is_food_at(state, x, y)) {
mvaddch(y, 0, '#'); printf("@");
mvaddch(y, state->width + 1, '#'); } else {
} printf(" ");
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, ' ');
}
} }
} }
} printf("\n");
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;
} }
} }