-
Notifications
You must be signed in to change notification settings - Fork 0
/
apple.go
45 lines (38 loc) · 1.04 KB
/
apple.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
package main
import (
"math/rand"
"github.com/gdamore/tcell/v2"
)
type Apple struct {
style tcell.Style
symbol rune
eatan bool
position Position
}
func newApple(maxWidth, maxHeight, boardOffsetX, boardOffsetY int) *Apple {
return &Apple{
style: tcell.StyleDefault.Foreground(tcell.ColorRed.TrueColor()),
symbol: 'a',
eatan: false,
position: newApplePosition(maxWidth, maxHeight, boardOffsetX, boardOffsetY),
}
}
func newApplePosition(maxWidth, maxHeight, boardOffsetX, boardOffsetY int) Position {
minX := 1 + boardOffsetX
minY := 1 + boardOffsetY
maxWidth--
maxHeight--
return Position{
x: rand.Intn(maxWidth-minX) + minX,
y: rand.Intn(maxHeight-minY) + minY,
}
}
func (a *Apple) updateApplePosition(maxWidth, maxHeight, boardOffsetX, boardOffsetY int) {
a.position = newApplePosition(maxWidth, maxHeight, boardOffsetX, boardOffsetY)
a.eatan = false
}
func (a *Apple) displayApple(s tcell.Screen, maxWidth, maxHeight int) {
if !a.eatan {
s.SetContent(a.position.x, a.position.y, a.symbol, nil, a.style)
}
}