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, '#');
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 y = 0; y < state->height; y++) {
for (int x = 0; x < state->width; x++) { 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)) { if (is_snake(state->snake, x, y)) {
attron(COLOR_PAIR(1)); printf("O");
mvaddch(draw_y, draw_x, 'O'); } else if (is_food_at(state, x, y)) {
attroff(COLOR_PAIR(1)); printf("@");
} else { } else {
int food = 0; printf(" ");
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;
} }
} }