import rl "vendor:raylib"
GAME_TITLE :: "Snake game"
// Define a struct to handle the snake's state
body: [SNAKE_MAXLENGTH]Vec2i,
// Initialize the snake with a starting position and length
snake_init :: proc() -> Snake {
return Snake{head = Vec2i{0, 0}, length = 1}
// Update snake position and handle wrapping around the screen
snake_update_position :: proc(snake: ^Snake) {
if rl.IsKeyDown(.RIGHT) {
snake.head.x += SNAKE_STEP
if snake.head.x >= WIN_WIDTH {
} else if rl.IsKeyDown(.LEFT) {
snake.head.x -= SNAKE_STEP
snake.head.x = WIN_WIDTH - SNAKE_STEP
} else if rl.IsKeyDown(.UP) {
snake.head.y -= SNAKE_STEP
snake.head.y = WIN_HEIGHT - SNAKE_STEP
} else if rl.IsKeyDown(.DOWN) {
snake.head.y += SNAKE_STEP
if snake.head.y >= WIN_HEIGHT {
// Move body segments, keeping length within SNAKE_MAXLENGTH
for i := snake.length - 1; i > 0; i -= 1 {
snake.body[i] = snake.body[i - 1]
snake.body[0] = prev_head
if snake.length < SNAKE_MAXLENGTH {
// Render the snake on the screen
snake_draw :: proc(snake: ^Snake) {
for i in 0 ..< snake.length {
color = rl.GREEN // Head color
color = rl.BLUE // Body color
rl.DrawRectangle(pos.x, pos.y, SNAKE_STEP, SNAKE_STEP, color)
rl.DrawRectangle(snake.head.x, snake.head.y, SNAKE_STEP, SNAKE_STEP, rl.GREEN)
rl.InitWindow(WIN_WIDTH, WIN_HEIGHT, GAME_TITLE)
for !rl.WindowShouldClose() {
snake_update_position(&snake)
rl.ClearBackground(rl.BLACK)