-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (72 loc) · 1.61 KB
/
main.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
// Example showing how to test pressed buttons of game controllers.
package main
import (
"embed"
"github.com/elgopher/pi"
"github.com/elgopher/pi/ebitengine"
)
// colors
const (
left = 10
up = 9
down = 13
right = 11
btnO = 4
btnX = 3
dpadInactive = 5
buttonInactive = 8
active = 14
yellow = 10
)
//go:embed sprite-sheet.png
var resources embed.FS
func main() {
pi.Load(resources)
pi.Draw = func() {
pi.Cls()
drawPlayerController(0, 2, 20)
drawPlayerController(1, 2, 70)
drawPlayerController(2, 67, 20)
drawPlayerController(3, 67, 70)
}
ebitengine.MustRun()
}
func drawPlayerController(player, x, y int) {
pi.Pal.Reset()
// make all buttons inactive:
pi.Pal[left] = dpadInactive
pi.Pal[up] = dpadInactive
pi.Pal[down] = dpadInactive
pi.Pal[right] = dpadInactive
pi.Pal[btnO] = buttonInactive
pi.Pal[btnX] = buttonInactive
// make pressed buttons active:
if pi.BtnPlayer(pi.O, player) {
pi.Pal[btnO] = active
}
if pi.BtnPlayer(pi.X, player) {
pi.Pal[btnX] = active
}
if pi.BtnPlayer(pi.Left, player) {
pi.Pal[left] = active
}
if pi.BtnPlayer(pi.Up, player) {
pi.Pal[up] = active
}
if pi.BtnPlayer(pi.Down, player) {
pi.Pal[down] = active
}
if pi.BtnPlayer(pi.Right, player) {
pi.Pal[right] = active
}
// draw gamepad sprite:
pi.SprSize(0, x, y, 8.0, 4.0)
// additionally draw a player number (1 light - player 0, 2 lights - player 1)
drawPlayerNumber(x, y, player)
}
func drawPlayerNumber(x int, y int, player int) {
pi.Pal.Reset()
for i := 0; i <= player; i++ {
pi.Set(x+50-i*2, y+8, yellow)
}
}