-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.go
84 lines (72 loc) · 2.7 KB
/
snake.go
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
package entities
import (
"slices"
"github.com/KidPudel/snakegame_go/common"
rl "github.com/gen2brain/raylib-go/raylib"
)
type Snake struct {
// head position
HeadPosition rl.Vector2
Direction rl.Vector2
TailPositions []rl.Vector2
// time trigger to jump to the next cell, based on ellaped time
EllapsedSinceLastJump float32
DeadSound *rl.Sound
}
// separate to own functions, so main function wouldn't be a mess, do not overcomplecate it
func (snake *Snake) HandleInput() {
// move by its size, so even
if rl.IsKeyPressed(rl.KeyD) {
snake.Direction = rl.NewVector2(common.SnakeHeadSize, 0)
} else if rl.IsKeyPressed(rl.KeyA) {
snake.Direction = rl.NewVector2(-common.SnakeHeadSize, 0)
} else if rl.IsKeyPressed(rl.KeyW) {
snake.Direction = rl.NewVector2(0, -common.SnakeHeadSize)
} else if rl.IsKeyPressed(rl.KeyS) {
snake.Direction = rl.NewVector2(0, common.SnakeHeadSize)
}
}
func (snake *Snake) Update(eaten *int) {
if snake.HeadPosition.X < 0 {
snake.HeadPosition.X = float32(common.ScreenWidth) - common.SnakeHeadSize
} else if snake.HeadPosition.X >= float32(common.ScreenWidth) {
snake.HeadPosition.X = 0
}
if snake.HeadPosition.Y < 0 {
snake.HeadPosition.Y = float32(common.ScreenHeight) - common.SnakeHeadSize
} else if snake.HeadPosition.Y >= float32(common.ScreenHeight) {
snake.HeadPosition.Y = 0
}
snake.EllapsedSinceLastJump += rl.GetFrameTime()
if snake.EllapsedSinceLastJump >= rl.GetFrameTime()*2 {
// set tail position
newTailPositions := make([]rl.Vector2, *eaten)
for i := range *eaten {
if i == 0 {
newTailPositions[i] = snake.HeadPosition
} else {
newTailPositions[i] = snake.TailPositions[i-1]
}
}
snake.TailPositions = newTailPositions
// move to to the direction
snake.HeadPosition = rl.Vector2Add(snake.HeadPosition, snake.Direction)
snake.EllapsedSinceLastJump = 0
if slices.ContainsFunc(snake.TailPositions, func(tailPosition rl.Vector2) bool {
return tailPosition == snake.HeadPosition
}) {
*eaten = 0
snake.TailPositions = []rl.Vector2{}
rl.PlaySound(*snake.DeadSound)
}
}
}
// draw frame
func (snake *Snake) Draw() {
rl.DrawRectangleV(snake.HeadPosition, rl.NewVector2(common.SnakeHeadSize, common.SnakeHeadSize), common.SnakeColor)
rl.DrawRectangleLinesEx(rl.NewRectangle(snake.HeadPosition.X, snake.HeadPosition.Y, common.SnakeHeadSize, common.SnakeHeadSize), 2, common.SnakeBorderColor)
for _, followingPosition := range snake.TailPositions {
rl.DrawRectangleV(followingPosition, rl.NewVector2(common.SnakeHeadSize, common.SnakeHeadSize), common.SnakeColor)
rl.DrawRectangleLinesEx(rl.NewRectangle(followingPosition.X, followingPosition.Y, common.SnakeHeadSize, common.SnakeHeadSize), 2, common.SnakeBorderColor)
}
}