-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_input.go
56 lines (45 loc) · 1016 Bytes
/
user_input.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
package main
import (
"os"
"snake/game"
"github.com/nsf/termbox-go"
)
func initKeyBard() (chan game.Move, chan command) {
moveEvent := make(chan game.Move)
commmandEvent := make(chan command)
go listenKeyBoard(moveEvent, commmandEvent)
return moveEvent, commmandEvent
}
func listenKeyBoard(eventMove chan game.Move, eventCommand chan command) {
for {
ev := termbox.PollEvent()
switch ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyArrowLeft:
eventMove <- game.Left
case termbox.KeyArrowDown:
eventMove <- game.Down
case termbox.KeyArrowRight:
eventMove <- game.Right
case termbox.KeyArrowUp:
eventMove <- game.Up
case termbox.KeyEsc:
eventCommand <- finish
case termbox.KeySpace:
eventCommand <- pause
case termbox.KeyEnter:
eventCommand <- start
default:
switch ev.Ch {
case 'y':
eventCommand <- start
default:
eventCommand <- finish
}
}
case termbox.EventError:
os.Exit(3)
}
}
}