-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.h
137 lines (95 loc) · 2.07 KB
/
main.h
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifndef MAIN_H
#define MAIN_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <termios.h>
#include <pthread.h>
#include <time.h>
#include "art.h"
/* graphics */
#define RED "\x1B[31m"
#define GRN "\x1B[32m"
#define YEL "\x1B[33m"
#define BLU "\x1B[34m"
#define MAG "\x1B[35m"
#define CYN "\x1B[36m"
#define WHT "\x1B[37m"
#define RESET "\x1B[0m"
#define BOX "\u2588\u2588"
#define BOX_LIGHT_3 "\u2593\u2593"
#define BOX_LIGHT_2 "\u2592\u2592"
#define BOX_LIGHT_1 "\u2591\u2591"
/* playground dimensions */
#define ROWS 20
#define COLUMNS 41
/* init snake position & size */
#define INIT_SNAKE_POS_Y 1
#define INIT_SNAKE_POS_X 1
#define INIT_SNAKE_SIZE 5
/* symbols */
#define SNAKE_HEAD '*'
#define SNAKE_BODY '.'
#define BLOCK '#'
#define FOOD '$'
/* status */
typedef struct status_s
{
unsigned int score;
bool game_over;
} status_t;
extern status_t status;
/* points */
typedef struct point
{
int x;
int y;
} point_t;
typedef struct pointnode_s
{
point_t point;
struct pointnode_s *prev;
struct pointnode_s *next;
} pointnode_t;
pointnode_t *new_point(int y, int x);
void free_points(pointnode_t *head);
/* snake */
typedef enum
{
RIGHT,
LEFT,
UP,
DOWN
} direction_t;
typedef struct snake_s
{
pointnode_t *head;
pointnode_t *tail;
direction_t requested_direction;
direction_t direction;
} snake_t;
int init_snake();
int increase_snake(snake_t *snake);
void move_snake(snake_t *snake);
void change_snake_direction(snake_t *snake, direction_t direction);
/* food */
void random_food(point_t *food);
/* playground */
typedef struct playground_s
{
char grid[ROWS][COLUMNS];
snake_t snake;
point_t food;
} playground_t;
int init_playground(playground_t *playground);
void clear_playground(playground_t *playground);
void refresh_playground(playground_t *playground);
void render_playground(playground_t *playground);
/* terminal mode */
void terminal_row_mode();
void terminal_normal_mode();
/* handlers */
void *keys_handler(void *playground);
void *render_handler(void *playground);
#endif /* MAIN_H */