-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake_game.rs
More file actions
287 lines (245 loc) · 8.5 KB
/
snake_game.rs
File metadata and controls
287 lines (245 loc) · 8.5 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#[path = "gp2d_math/mod.rs"]
mod math;
mod raylib;
use math::*;
use raylib::*;
// ============================
// Game Constants
// ============================
const GRID_SIZE: i32 = 20;
const CELL_SIZE: i32 = 30;
const WINDOW_WIDTH: i32 = GRID_SIZE * CELL_SIZE;
const WINDOW_HEIGHT: i32 = GRID_SIZE * CELL_SIZE + 60; // Extra space for UI
const MOVE_DELAY: f32 = 0.15; // Seconds between moves
// ============================
// Direction
// ============================
#[derive(Copy, Clone, PartialEq)]
enum Direction {
Up,
Down,
Left,
Right,
}
impl Direction {
fn to_vec2(self) -> Vec2 {
match self {
Direction::Up => Vec2::new(0.0, -1.0),
Direction::Down => Vec2::new(0.0, 1.0),
Direction::Left => Vec2::new(-1.0, 0.0),
Direction::Right => Vec2::new(1.0, 0.0),
}
}
fn opposite(self) -> Direction {
match self {
Direction::Up => Direction::Down,
Direction::Down => Direction::Up,
Direction::Left => Direction::Right,
Direction::Right => Direction::Left,
}
}
}
// ============================
// Snake Game
// ============================
struct SnakeGame {
snake: Vec<Vec2>,
direction: Direction,
next_direction: Direction,
food: Vec2,
score: i32,
game_over: bool,
move_timer: f32,
rng_seed: u64,
}
impl SnakeGame {
fn new() -> Self {
let mut game = Self {
snake: vec![Vec2::new(10.0, 10.0)],
direction: Direction::Right,
next_direction: Direction::Right,
food: Vec2::new(15.0, 10.0),
score: 0,
game_over: false,
move_timer: 0.0,
rng_seed: 12345,
};
game.spawn_food();
game
}
fn spawn_food(&mut self) {
loop {
let x = self.rand_int(0, GRID_SIZE - 1) as f32;
let y = self.rand_int(0, GRID_SIZE - 1) as f32;
let pos = Vec2::new(x, y);
// Check if food spawns on snake
let mut valid = true;
for segment in &self.snake {
if segment.x == pos.x && segment.y == pos.y {
valid = false;
break;
}
}
if valid {
self.food = pos;
break;
}
}
}
fn rand_int(&mut self, min: i32, max: i32) -> i32 {
self.rng_seed = self.rng_seed.wrapping_mul(6364136223846793005).wrapping_add(1);
let f = (self.rng_seed >> 32) as f32 / 4294967296.0;
min + (f * (max - min + 1) as f32) as i32
}
fn update(&mut self, dt: f32) {
if self.game_over {
return;
}
self.move_timer += dt;
if self.move_timer >= MOVE_DELAY {
self.move_timer = 0.0;
self.direction = self.next_direction;
// Calculate new head position
let head = self.snake[0];
let dir = self.direction.to_vec2();
let new_head = Vec2::new(head.x + dir.x, head.y + dir.y);
// Check wall collision
if new_head.x < 0.0 || new_head.x >= GRID_SIZE as f32 ||
new_head.y < 0.0 || new_head.y >= GRID_SIZE as f32 {
self.game_over = true;
return;
}
// Check self collision
for segment in &self.snake {
if segment.x == new_head.x && segment.y == new_head.y {
self.game_over = true;
return;
}
}
// Move snake
self.snake.insert(0, new_head);
// Check food collision
if new_head.x == self.food.x && new_head.y == self.food.y {
self.score += 10;
self.spawn_food();
} else {
self.snake.pop();
}
}
}
fn handle_input(&mut self) {
unsafe {
if IsKeyPressed(KEY_UP) && self.direction != Direction::Down {
self.next_direction = Direction::Up;
}
if IsKeyPressed(KEY_DOWN) && self.direction != Direction::Up {
self.next_direction = Direction::Down;
}
if IsKeyPressed(KEY_LEFT) && self.direction != Direction::Right {
self.next_direction = Direction::Left;
}
if IsKeyPressed(KEY_RIGHT) && self.direction != Direction::Left {
self.next_direction = Direction::Right;
}
if IsKeyPressed(KEY_R) {
*self = Self::new();
}
}
}
fn draw(&self) {
unsafe {
ClearBackground(Color { r: 20, g: 20, b: 30, a: 255 });
// Draw grid
for x in 0..GRID_SIZE {
for y in 0..GRID_SIZE {
let color = if (x + y) % 2 == 0 {
Color { r: 30, g: 30, b: 40, a: 255 }
} else {
Color { r: 25, g: 25, b: 35, a: 255 }
};
DrawRectangle(
x * CELL_SIZE,
y * CELL_SIZE,
CELL_SIZE,
CELL_SIZE,
color
);
}
}
// Draw food
DrawRectangle(
self.food.x as i32 * CELL_SIZE + 2,
self.food.y as i32 * CELL_SIZE + 2,
CELL_SIZE - 4,
CELL_SIZE - 4,
RED
);
// Draw snake
for (i, segment) in self.snake.iter().enumerate() {
let color = if i == 0 {
GREEN // Head
} else {
Color { r: 0, g: 180, b: 40, a: 255 } // Body
};
DrawRectangle(
segment.x as i32 * CELL_SIZE + 1,
segment.y as i32 * CELL_SIZE + 1,
CELL_SIZE - 2,
CELL_SIZE - 2,
color
);
// Draw eyes on head
if i == 0 {
let eye_size = 4.0;
let eye_offset = CELL_SIZE as f32 * 0.3;
let cx = segment.x as f32 * CELL_SIZE as f32 + CELL_SIZE as f32 * 0.5;
let cy = segment.y as f32 * CELL_SIZE as f32 + CELL_SIZE as f32 * 0.5;
let (dx, dy) = match self.direction {
Direction::Right => (eye_offset, -eye_offset / 2.0),
Direction::Left => (-eye_offset, -eye_offset / 2.0),
Direction::Up => (0.0, -eye_offset),
Direction::Down => (0.0, eye_offset),
};
DrawCircle((cx + dx) as i32, (cy + dy - 3.0) as i32, eye_size, BLACK);
DrawCircle((cx + dx) as i32, (cy + dy + 3.0) as i32, eye_size, BLACK);
}
}
// Draw UI
let ui_y = GRID_SIZE * CELL_SIZE + 10;
DrawText(b"SNAKE GAME\0".as_ptr() as *const i8, 10, ui_y, 20, RAYWHITE);
let score_text = format!("Score: {}\0", self.score);
DrawText(score_text.as_ptr() as *const i8, 200, ui_y, 20, YELLOW);
DrawText(b"Arrow Keys: Move | R: Restart\0".as_ptr() as *const i8, 10, ui_y + 25, 16, DARKGRAY);
if self.game_over {
// Draw game over overlay
DrawRectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, Color { r: 0, g: 0, b: 0, a: 180 });
let msg = b"GAME OVER!\0";
DrawText(msg.as_ptr() as *const i8, WINDOW_WIDTH / 2 - 100, WINDOW_HEIGHT / 2 - 40, 40, RED);
let restart = b"Press R to Restart\0";
DrawText(restart.as_ptr() as *const i8, WINDOW_WIDTH / 2 - 100, WINDOW_HEIGHT / 2 + 20, 20, RAYWHITE);
}
}
}
fn reset(&mut self) {
*self = Self::new();
}
}
// ============================
// Main
// ============================
fn main() {
unsafe {
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, b"Snake Game\0".as_ptr() as *const i8);
SetTargetFPS(60);
let mut game = SnakeGame::new();
while !WindowShouldClose() {
let dt = GetFrameTime();
game.handle_input();
game.update(dt);
BeginDrawing();
game.draw();
EndDrawing();
}
CloseWindow();
}
}