-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.odin
195 lines (153 loc) · 3.99 KB
/
snake.odin
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
package snake
import rl "vendor:raylib"
import "core:math"
import "core:fmt"
WINDOW_SIZE :: 1000
GRID_WIDTH :: 20
CELL_SIZE :: 16
CANVAS_SIZE :: GRID_WIDTH*CELL_SIZE
TICK_RATE :: 0.13
Vec2i :: [2]int
MAX_SNAKE_LENGTH :: GRID_WIDTH*GRID_WIDTH
snake: [MAX_SNAKE_LENGTH]Vec2i
snake_length: int
tick_timer: f32 = TICK_RATE
move_direction: Vec2i
game_over: bool
food_pos: Vec2i
place_food :: proc() {
occupied: [GRID_WIDTH][GRID_WIDTH]bool
for i in 0..<snake_length {
occupied[snake[i].x][snake[i].y] = true
}
free_cells := make([dynamic]Vec2i, context.temp_allocator)
for x in 0..<GRID_WIDTH {
for y in 0..<GRID_WIDTH {
if !occupied[x][y] {
append(&free_cells, Vec2i {x, y})
}
}
}
if len(free_cells) > 0 {
random_cell_index := rl.GetRandomValue(0, i32(len(free_cells) - 1))
food_pos = free_cells[random_cell_index]
}
}
restart :: proc() {
start_head_pos := Vec2i { GRID_WIDTH / 2, GRID_WIDTH / 2 }
snake[0] = start_head_pos
snake[1] = start_head_pos - {0, 1}
snake[2] = start_head_pos - {0, 2}
snake_length = 3
move_direction = {0, 1}
game_over = false
place_food()
}
main :: proc() {
rl.SetConfigFlags({.VSYNC_HINT})
rl.InitWindow(WINDOW_SIZE, WINDOW_SIZE, "Snake")
rl.InitAudioDevice()
restart()
food_sprite := rl.LoadTexture("food.png")
head_sprite := rl.LoadTexture("head.png")
body_sprite := rl.LoadTexture("body.png")
tail_sprite := rl.LoadTexture("tail.png")
eat_sound := rl.LoadSound("eat.wav")
crash_sound := rl.LoadSound("crash.wav")
for !rl.WindowShouldClose() {
if rl.IsKeyDown(.UP) {
move_direction = {0, -1}
}
if rl.IsKeyDown(.DOWN) {
move_direction = {0, 1}
}
if rl.IsKeyDown(.LEFT) {
move_direction = {-1, 0}
}
if rl.IsKeyDown(.RIGHT) {
move_direction = {1, 0}
}
if game_over {
if rl.IsKeyPressed(.ENTER) {
restart()
}
} else {
tick_timer -= rl.GetFrameTime()
}
if tick_timer <= 0 {
next_part_pos := snake[0]
snake[0] += move_direction
head_pos := snake[0]
if head_pos.x < 0 || head_pos.y < 0 || head_pos.x >= GRID_WIDTH || head_pos.y >= GRID_WIDTH {
game_over = true
rl.PlaySound(crash_sound)
}
for i in 1..<snake_length {
cur_pos := snake[i]
if cur_pos == head_pos {
game_over = true
rl.PlaySound(crash_sound)
}
snake[i] = next_part_pos
next_part_pos = cur_pos
}
if head_pos == food_pos {
snake_length += 1
snake[snake_length - 1] = next_part_pos
place_food()
rl.PlaySound(eat_sound)
}
tick_timer = TICK_RATE + tick_timer
}
rl.BeginDrawing()
rl.ClearBackground({76, 53, 83, 255})
camera := rl.Camera2D {
zoom = f32(WINDOW_SIZE) / CANVAS_SIZE
}
rl.BeginMode2D(camera)
rl.DrawTextureV(food_sprite, {f32(food_pos.x), f32(food_pos.y)}*CELL_SIZE, rl.WHITE)
for i in 0..<snake_length {
part_sprite := body_sprite
dir: Vec2i
if i == 0 {
part_sprite = head_sprite
dir = snake[i] - snake[i + 1]
} else if i == snake_length - 1 {
part_sprite = tail_sprite
dir = snake[i - 1] - snake[i]
} else {
dir = snake[i - 1] - snake[i]
}
rot := math.atan2(f32(dir.y), f32(dir.x)) * math.DEG_PER_RAD
source := rl.Rectangle {
0, 0,
f32(part_sprite.width), f32(part_sprite.height),
}
dest := rl.Rectangle {
f32(snake[i].x)*CELL_SIZE + 0.5*CELL_SIZE,
f32(snake[i].y)*CELL_SIZE + 0.5*CELL_SIZE,
CELL_SIZE,
CELL_SIZE,
}
rl.DrawTexturePro(part_sprite, source, dest, {CELL_SIZE, CELL_SIZE}*0.5, rot, rl.WHITE)
}
if game_over {
rl.DrawText("Game Over!", 4, 4, 25, rl.RED)
rl.DrawText("Press Enter to play again", 4, 30, 15, rl.BLACK)
}
score := snake_length - 3
score_str := fmt.ctprintf("Score: %v", score)
rl.DrawText(score_str, 4, CANVAS_SIZE - 14, 10, rl.GRAY)
rl.EndMode2D()
rl.EndDrawing()
free_all(context.temp_allocator)
}
rl.UnloadTexture(head_sprite)
rl.UnloadTexture(food_sprite)
rl.UnloadTexture(body_sprite)
rl.UnloadTexture(tail_sprite)
rl.UnloadSound(eat_sound)
rl.UnloadSound(crash_sound)
rl.CloseAudioDevice()
rl.CloseWindow()
}