-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
281 lines (252 loc) · 8.8 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package main
import (
"embed"
"image"
"image/gif"
"image/color/palette"
"log"
"time"
"github.com/metalix2/pigames/scenarios"
"periph.io/x/conn/v3/i2c/i2creg"
"periph.io/x/devices/v3/ssd1306"
"periph.io/x/host/v3"
"periph.io/x/conn/v3/gpio"
"periph.io/x/conn/v3/gpio/gpioreg"
)
//go:embed images
var image_path embed.FS
type Difficulty struct {
Width int
Height int
Level int
Time time.Duration
}
var difficulty = []Difficulty{{ Width:128, Height: 64, Level: 1, Time: 0.0}, { Width:128, Height: 128, Level: 2, Time: 0.0}, { Width: 384, Height: 64, Level: 3, Time: 0.0}, { Width:256, Height: 128, Level: 4, Time: 0.0}, { Width:256, Height: 192, Level: 5, Time: 0.0}, { Width: 128, Height: 64, Level: 0, Time: 0.0}}
var level int
var titleShown = false
var introShown = false
var showLevel = false
var introFrames = 0
var a_event = 0
func main() {
// Load all the drivers:
if _, err := host.Init(); err != nil {
log.Fatal(err)
}
// Open a handle to the first available I²C bus:
bus, err := i2creg.Open("")
// Open a handle to a ssd1306 connected on the I²C bus:
dev, err := ssd1306.NewI2C(bus, &ssd1306.DefaultOpts)
titlePath := "images/title.gif"
f, err := image_path.Open(titlePath)
if err != nil {
log.Fatal(err)
}
titleGif, err := gif.DecodeAll(f)
f.Close()
if err != nil {
log.Fatal(err)
}
avatarpath := "images/avatar.gif"
avatarReader, err := image_path.Open(avatarpath)
if err != nil {
log.Fatal(err)
}
avatarGif, err := gif.DecodeAll(avatarReader)
f.Close()
if err != nil {
log.Fatal(err)
}
matetopath := "images/mateto.gif"
matetoReader, err := image_path.Open(matetopath)
if err != nil {
log.Fatal(err)
}
matetoGif, err := gif.DecodeAll(matetoReader)
f.Close()
if err != nil {
log.Fatal(err)
}
heartpath := "images/heart.gif"
heartReader, err := image_path.Open(heartpath)
if err != nil {
log.Fatal(err)
}
heartGif, err := gif.DecodeAll(heartReader)
f.Close()
if err != nil {
log.Fatal(err)
}
// Lookup a pin by its number:
p_r := gpioreg.ByName("GPIO23")
p_l := gpioreg.ByName("GPIO27")
p_u := gpioreg.ByName("GPIO17")
p_d := gpioreg.ByName("GPIO22")
p_c := gpioreg.ByName("GPIO4")
p_a := gpioreg.ByName("GPIO6")
// p_a := gpioreg.ByName("GPIO6")
// Set it as input, with an internal pull up resistor:
if err := p_r.In(gpio.PullUp, gpio.BothEdges); err != nil {
log.Fatal(err)
}
if err := p_l.In(gpio.PullUp, gpio.BothEdges); err != nil {
log.Fatal(err)
}
if err := p_u.In(gpio.PullUp, gpio.BothEdges); err != nil {
log.Fatal(err)
}
if err := p_d.In(gpio.PullUp, gpio.BothEdges); err != nil {
log.Fatal(err)
}
if err := p_c.In(gpio.PullUp, gpio.BothEdges); err != nil {
log.Fatal(err)
}
if err := p_a.In(gpio.PullUp, gpio.BothEdges); err != nil {
log.Fatal(err)
}
// Intial co-ordinates we need to capture previous co-ord and next co-ords:
prev_coords := map[string]int{"x": 2, "y": 2}
next_coords := map[string]int{"x": 2, "y": 2}
// Global params
var dir = 1
var fps = 10
level = 0
screenX := 0
screenY := 0
ts := time.Now();
p_counter := 0 // reset's when passes 5
a_counter := 0
index := 0
// Display the frames in a loop:
for i := 1; ; {
// fps currently 300ms per avatar frame so ~3FPS for avatar animation; screen refreshes every 100ms so 10FPS for each cycle.
c := time.After(time.Duration(10*fps) * time.Millisecond)
if titleShown && introShown && difficulty[level].Level == 0 {
// end game
index = i % len(avatarGif.Image)
// movement
if p_r.Read() == gpio.Low {
dir = 1
next_coords["x"] += 2
} else if p_u.Read() == gpio.Low {
next_coords["y"] -= 2
}
if p_l.Read() == gpio.Low {
dir = 0
next_coords["x"] -= 2
} else if p_d.Read() == gpio.Low {
next_coords["y"] += 2
}
var img *image.Paletted
var coords map[string]int
img, coords = scenarios.DrawEnding(difficulty[level].Width, difficulty[level].Height, avatarGif.Image[index], matetoGif.Image[index], heartGif.Image[index], prev_coords, next_coords, dir)
prev_coords["x"] = coords["x"]
prev_coords["y"] = coords["y"]
next_coords["x"] = coords["x"]
next_coords["y"] = coords["y"]
dev.Draw(img.Bounds(), img, image.Point{0, 0})
<-c
} else if showLevel {
introFrames += 1
img := image.NewPaletted(image.Rect(0, 0, 128, 64), palette.Plan9)
scenarios.DrawLevelText(img, difficulty[level].Level, introFrames)
dev.Draw(img.Bounds(), img, image.Point{0, 0})
if p_a.Read() == gpio.Low {
a_counter += 1
if a_counter < 2{
showLevel = false
}
}
if p_a.Read() == gpio.High {
a_counter = 0
}
<-c
} else if titleShown && introShown && difficulty[level].Level != 0 {
index = i % len(avatarGif.Image)
if ts.Add(time.Duration(10 * avatarGif.Delay[index]) * time.Millisecond).Sub(time.Now()) < time.Duration(10 * 1) * time.Millisecond {
i++
ts = time.Now();
index = i % len(avatarGif.Image)
}
//reset mechanic
if p_c.Read() == gpio.Low {
p_counter += 1
}
if p_c.Read() != gpio.Low {
p_counter = 0
}
if p_counter > 5 && p_counter < 10 {
screenX = 0
screenY = 0
prev_coords = map[string]int{"x": 2, "y": 2}
next_coords = map[string]int{"x": 2, "y": 2}
} else if p_counter > 10 {
level = 0
// currentMaze = createMaze(int(math.Round(float64(difficulty[level].Width/14))), int(math.Round(float64(difficulty[level].Height/16))))
screenX = 0
screenY = 0
prev_coords = map[string]int{"x": 2, "y": 2}
next_coords = map[string]int{"x": 2, "y": 2}
}
// movement
if p_r.Read() == gpio.Low {
dir = 1
next_coords["x"] += 2
} else if p_u.Read() == gpio.Low {
next_coords["y"] -= 2
}
if p_l.Read() == gpio.Low {
dir = 0
next_coords["x"] -= 2
} else if p_d.Read() == gpio.Low {
next_coords["y"] += 2
}
img := image.NewPaletted(image.Rect(0, 0, difficulty[level].Width, difficulty[level].Height), palette.Plan9)
var coords map[string]int
var x, y, l, iFrames int
var sLevel bool
coords, x, y, l, sLevel, iFrames = scenarios.DrawCanvas(img, avatarGif.Image[index], prev_coords, next_coords, dir, screenX, screenY, difficulty[level].Width, difficulty[level].Height, level, showLevel, introFrames)
level = l
showLevel = sLevel
introFrames = iFrames
screenX = x
screenY = y
prev_coords["x"] = coords["x"]
prev_coords["y"] = coords["y"]
next_coords["x"] = coords["x"]
next_coords["y"] = coords["y"]
if showLevel {
continue
}
dev.Draw(img.Bounds(), img, image.Point{screenX, screenY})
<-c
} else {
// We wait on title until button pressed
if p_a.Read() == gpio.Low {
a_counter += 1
if a_counter < 2 {
a_event += 1
introFrames = 0
}
}
if p_a.Read() == gpio.High {
a_counter = 0
}
if i > len(titleGif.Image) - 2 {
i = len(titleGif.Image) - 2
}
if ts.Add(time.Duration(10 * titleGif.Delay[i]) * time.Millisecond).Sub(time.Now()) < time.Duration(10 * 1) * time.Millisecond {
i++
ts = time.Now();
}
img := image.NewPaletted(image.Rect(0, 0, 128, 64), palette.Plan9)
tShown, iShown, iFrames, sLevel := scenarios.DrawIntro(img, titleGif.Image[i], a_event, titleShown, introShown, introFrames, showLevel)
titleShown = tShown
introShown = iShown
introFrames = iFrames
showLevel = sLevel
dev.Draw(img.Bounds(), img, image.Point{0, 0})
<-c
}
}
}