-
Notifications
You must be signed in to change notification settings - Fork 11
/
controller.go
54 lines (48 loc) · 962 Bytes
/
controller.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
package main
import (
"github.com/nsf/termbox-go"
"time"
)
const animationSpeed = 10 * time.Millisecond
func main() {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
eventQueue := make(chan termbox.Event)
go func() {
for {
eventQueue <- termbox.PollEvent()
}
}()
g := NewGame()
render(g)
for {
ev := <-eventQueue
if ev.Type == termbox.EventKey {
switch {
case ev.Key == termbox.KeyArrowUp || ev.Ch == 'k':
g.move(UP)
case ev.Key == termbox.KeyArrowDown || ev.Ch == 'j':
g.move(DOWN)
case ev.Key == termbox.KeyArrowLeft || ev.Ch == 'h':
g.move(LEFT)
case ev.Key == termbox.KeyArrowRight || ev.Ch == 'l':
g.move(RIGHT)
case ev.Ch == 'n':
g.nextLevel()
case ev.Ch == 'p':
g.prevLevel()
case ev.Ch == 'r':
g.reset()
case ev.Ch == 'd':
g.toggleDebug()
case ev.Key == termbox.KeyEsc:
return
}
}
render(g)
time.Sleep(animationSpeed)
}
}