-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.go
More file actions
54 lines (44 loc) · 981 Bytes
/
snake.go
File metadata and controls
54 lines (44 loc) · 981 Bytes
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
package main
import (
"image/color"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
)
type Direction = int
const (
DIR_UP Direction = iota
DIR_DOWN
DIR_LEFT
DIR_RIGHT
)
const CELL_SIZE int = 20
type Position struct {
x int
y int
}
type Snake struct {
segments []Position
direction Direction
}
func (s *Snake) Draw(screen *ebiten.Image) {
for i := range s.segments {
vector.DrawFilledRect(screen, float32(CELL_SIZE*s.segments[i].x), float32(CELL_SIZE*s.segments[i].y), float32(CELL_SIZE), float32(CELL_SIZE), color.White, false)
}
}
func (s *Snake) Update() {
// Shift every segment to its previous element
for i := len(s.segments) - 1; i > 0; i-- {
s.segments[i] = s.segments[i-1]
}
// Increment or decrement head position according to its direction
switch s.direction {
case DIR_RIGHT:
s.segments[0].x++
case DIR_LEFT:
s.segments[0].x--
case DIR_UP:
s.segments[0].y--
case DIR_DOWN:
s.segments[0].y++
}
}