a3
This commit is contained in:
parent
380c0a5779
commit
d452cb33e3
61
a3/world.c
61
a3/world.c
@ -1,52 +1,21 @@
|
||||
#include <ncurses.h>
|
||||
#include "world.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "snake.h"
|
||||
|
||||
void render(const struct state* state){
|
||||
clear();
|
||||
void render(const struct state* state) {
|
||||
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 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, ' ');
|
||||
}
|
||||
// Малюємо поле
|
||||
for (int y = 0; y < state->height; y++) {
|
||||
for (int x = 0; x < state->width; x++) {
|
||||
if (is_snake(state->snake, x, y)) {
|
||||
printf("O");
|
||||
} else if (is_food_at(state, x, y)) {
|
||||
printf("@");
|
||||
} else {
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user