This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.c
114 lines (89 loc) · 1.82 KB
/
graphics.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <ncurses.h>
#include <time.h>
#include <stdio.h>
#include "graphics.h"
#include "constants.h"
static void init_colors(void);
void init_graphics(void)
{
initscr();
keypad(stdscr, true);
cbreak();
noecho();
nodelay(stdscr, true);
curs_set(0);
init_colors();
clear();
}
int check_win_size(void)
{
int wx, wy;
wx = getmaxx(stdscr);
wy = getmaxy(stdscr);
return !(wx < MIN_WINDOW_WIDTH || wy < MIN_WINDOW_HEIGHT);
}
void draw_arena(int width, int height)
{
attron(COLOR_PAIR(WALL_COLOR));
int i, j;
for (i = 0; i < width + 2; i++) {
addstr("\u2585");
}
addstr("\n");
for (j = 0; j < height; j++) {
addstr("\u2588");
for (i = 0; i < width; i++) {
addch(' ');
}
addstr("\u2588\n");
}
addstr("\u2588");
for (i = 0; i < width; i++) {
addstr("\u2585");
}
addstr("\u2588");
attroff(COLOR_PAIR(WALL_COLOR));
}
void draw_apple(int x, int y)
{
attron(COLOR_PAIR(APPLE_COLOR));
move(y, x);
addch('o');
attroff(COLOR_PAIR(APPLE_COLOR));
}
void draw_snake(queue *snake)
{
attron(COLOR_PAIR(SNAKE_COLOR));
struct node *p = snake->first;
for (; p; p = p->next) {
int x = p->x;
int y = p->y;
move(y, x);
addch('*');
}
attroff(COLOR_PAIR(SNAKE_COLOR));
}
void draw_text(char *text)
{
addstr(text);
}
void close(void)
{
endwin();
}
static void init_colors(void)
{
start_color();
init_pair(WALL_COLOR, WALL_COLOR, COLOR_BLACK);
init_pair(SNAKE_COLOR, SNAKE_COLOR, COLOR_BLACK);
init_pair(APPLE_COLOR, APPLE_COLOR, COLOR_BLACK);
init_pair(5, COLOR_WHITE, COLOR_RED);
}
void color_on(int color)
{
attron(COLOR_PAIR(color));
}
void color_off(int color)
{
attroff(COLOR_PAIR(WALL_COLOR));
}