-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfood.go
67 lines (50 loc) · 972 Bytes
/
food.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
package main
import (
"fmt"
"math/rand"
"time"
"github.com/veandco/go-sdl2/sdl"
)
const (
foodColor = 0xfe2e2e
)
type food struct {
x int32
y int32
w int32
h int32
color uint32
}
func (f *food) String() string {
return fmt.Sprintf("Food{x:%v, y:%v}", f.x, f.y)
}
func newFood() *food {
rand.Seed(time.Now().UTC().UnixNano())
f := &food{
x: 0,
y: 0,
w: 1 * cellSize,
h: 1 * cellSize,
color: foodColor,
}
f.update(nil)
return f
}
func (f *food) update(snake *snake) {
col := windowWidth / cellSize
row := windowHeight / cellSize
f.x = int32(rand.Intn(col)) * cellSize
f.y = int32(rand.Intn(row)) * cellSize
// is nil at the very beginning
if snake != nil {
for _, cell := range snake.body {
if f.x == cell.x && f.y == cell.y {
f.update(snake)
}
}
}
}
func (f *food) draw(surface *sdl.Surface) error {
rect := sdl.Rect{X: f.x, Y: f.y, W: f.w, H: f.h}
return surface.FillRect(&rect, f.color)
}