-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.c
244 lines (221 loc) · 5.64 KB
/
snake.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "particles/particles.h"
#include <raylib.h>
#include <raymath.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#define FACTOR 80
#define SCREEN_W FACTOR * 16
#define SCREEN_H FACTOR * 9
#define TILE_SIZE 40
#define ROWS 1280 / TILE_SIZE
#define COLS 720 / TILE_SIZE
typedef enum { SNAKE, FOOD, AIR } TileType;
typedef struct node {
int x;
int y;
struct node *prev;
struct node *next;
} SnakeNode;
typedef struct {
SnakeNode *head;
SnakeNode *tail;
int len;
int dir_x;
int dir_y;
} Snake;
typedef struct {
Snake snake;
Vector2 food;
TileType board[ROWS][COLS];
int food_eaten;
} Game;
SnakeNode *create_node(int x, int y) {
SnakeNode *node = (SnakeNode *)malloc(sizeof(SnakeNode));
node->x = x;
node->y = y;
node->prev = NULL;
node->next = NULL;
return node;
}
void push_front(Snake *snake, SnakeNode *node) {
SnakeNode *cur = snake->head;
if (snake->head == NULL)
snake->tail = node;
cur->prev = node;
node->next = cur;
snake->head = node;
snake->len++;
return;
}
void delete_tail(Snake *snake) {
SnakeNode *prev = snake->tail->prev;
free(prev->next);
prev->next = NULL;
snake->tail = prev;
snake->len--;
return;
}
void draw_snake(Snake *snake) {
SnakeNode *cur = snake->head;
while (cur != NULL) {
DrawRectangle(cur->x * TILE_SIZE, cur->y * TILE_SIZE, TILE_SIZE, TILE_SIZE,
GRAY);
cur = cur->next;
}
return;
}
Vector2 GetRandomCirclePoint(int radius) {
int ry = GetRandomValue(-radius, radius);
int rx = GetRandomValue(-radius, radius);
int theta = GetRandomValue(-radius, radius) * 2 * PI;
return (Vector2){ry * cosf(theta), rx * sinf(theta)};
}
Snake init_snake() {
Snake snake;
snake.head = create_node(1, 1);
snake.tail = snake.head;
snake.dir_x = 1;
snake.dir_y = 0;
snake.len = 1;
push_front(&snake, create_node(2, 1));
push_front(&snake, create_node(3, 1));
push_front(&snake, create_node(4, 1));
return snake;
}
void init_board(TileType board[ROWS][COLS]) {
for (int x = 0; x < ROWS; x++) {
for (int y = 0; y < COLS; y++) {
board[x][y] = AIR;
}
}
}
void update_game(Game *game) {
while (game->food.x == -1 && GetRandomValue(0, 1) == 0) {
int x = GetRandomValue(0, ROWS - 1);
int y = GetRandomValue(0, COLS - 1);
if (game->board[x][y] == AIR) {
game->board[x][y] = FOOD;
game->food.x = x;
game->food.y = y;
break;
}
}
Snake *snake = &game->snake;
Vector2 *food = &game->food;
SnakeNode *cur = snake->head;
int x = cur->x + snake->dir_x;
int y = cur->y + snake->dir_y;
if (x < 0)
x = ROWS - 1;
if (x > ROWS)
x = 0;
if (y < 0)
y = COLS - 1;
if (y > COLS)
y = 0;
int check_snake = game->board[x][y] == SNAKE;
if (check_snake != 0) {
*snake = init_snake();
init_board(game->board);
food->x = -1;
food->y = -1;
return;
}
int check_food = game->board[x][y] == FOOD && x == food->x && y == food->y;
SnakeNode *new_head = create_node(x, y);
game->board[new_head->x][new_head->y] = SNAKE;
push_front(snake, new_head);
if (check_food == 0) {
game->board[snake->tail->x][snake->tail->y] = AIR;
delete_tail(snake);
} else {
game->food_eaten = 1;
}
return;
}
Game init_game() {
Game game = {
.board = {0},
.food = (Vector2){.x = -1, .y = -1},
.snake = init_snake(),
};
init_board(game.board);
game.board[1][1] = SNAKE;
game.board[2][1] = SNAKE;
game.board[3][1] = SNAKE;
game.board[4][1] = SNAKE;
return game;
}
int main() {
Game game = init_game();
SetConfigFlags(FLAG_MSAA_4X_HINT);
SetConfigFlags(FLAG_VSYNC_HINT);
InitWindow(SCREEN_W, SCREEN_H, "Snake");
InitAudioDevice();
int frame_counter = 0;
float time_step = 0;
int allow_input = 1;
Sound sound = LoadSound("./resources/short-bell.ogg");
char *score = malloc(25 * sizeof(char));
ParticleState particle_state = {0};
particle_state.pool_index = NUM_PARTICLES - 1;
while (!WindowShouldClose()) {
sprintf(score, "Score: %d", game.snake.len);
const float delta = GetFrameTime();
time_step += delta;
if (IsKeyDown(KEY_W) && game.snake.dir_y != 1 && allow_input != 0) {
game.snake.dir_x = 0;
game.snake.dir_y = -1;
allow_input = 0;
} else if (IsKeyDown(KEY_A) && game.snake.dir_x != 1 && allow_input != 0) {
game.snake.dir_x = -1;
game.snake.dir_y = 0;
allow_input = 0;
} else if (IsKeyDown(KEY_S) && game.snake.dir_y != -1 && allow_input != 0) {
game.snake.dir_x = 0;
game.snake.dir_y = 1;
allow_input = 0;
} else if (IsKeyDown(KEY_D) && game.snake.dir_x != -1 && allow_input != 0) {
game.snake.dir_x = 1;
game.snake.dir_y = 0;
allow_input = 0;
}
if (time_step > 0.15f) {
allow_input = 1;
time_step = 0;
update_game(&game);
}
update_particles(&particle_state, delta);
BeginDrawing();
ClearBackground(RAYWHITE);
draw_snake(&game.snake);
DrawCircle(game.food.x * TILE_SIZE + (TILE_SIZE / 2),
game.food.y * TILE_SIZE + (TILE_SIZE / 2), 10, RED);
draw_particles(&particle_state);
if (game.food_eaten) {
Particle particle = {
.pos = Vector2Multiply(game.food, (Vector2){TILE_SIZE, TILE_SIZE}),
.size_begin = 10,
.size_end = 5,
.lifetime = 1.0f,
.life_remaining = 1.0f,
};
for (size_t i = 0; i < 10; i++) {
emit_particle(&particle_state, particle);
}
game.food.x = -1;
game.food.y = -1;
game.food_eaten = 0;
PlaySound(sound);
}
DrawText(score, 8, 8, 20, BLACK);
EndDrawing();
}
CloseAudioDevice();
CloseWindow();
return 0;
}
/*
* TODO: Sound
* */