-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (45 loc) · 1.22 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
package main
import (
"log"
"os"
"github.com/gdamore/tcell"
)
func main() {
screen, err := tcell.NewScreen()
if err != nil {
log.Fatalf("%+v", err)
}
if err := screen.Init(); err != nil {
log.Fatalf("%+v", err)
}
defStyle := tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorWhite)
screen.SetStyle(defStyle)
game := Game{
Screen: screen,
}
go game.Run()
for {
switch event := game.Screen.PollEvent().(type) {
case *tcell.EventResize:
game.Screen.Sync()
case *tcell.EventKey:
if event.Key() == tcell.KeyEscape || event.Key() == tcell.KeyCtrlC {
game.Screen.Fini()
os.Exit(0)
} else if event.Key() == tcell.KeyUp && game.snakeBody.Yspeed == 0 {
game.snakeBody.ChangeDir(-1, 0)
} else if event.Key() == tcell.KeyDown && game.snakeBody.Yspeed == 0 {
game.snakeBody.ChangeDir(1, 0)
} else if event.Key() == tcell.KeyLeft && game.snakeBody.Xspeed == 0 {
game.snakeBody.ChangeDir(0, -1)
} else if event.Key() == tcell.KeyRight && game.snakeBody.Xspeed == 0 {
game.snakeBody.ChangeDir(0, 1)
} else if event.Rune() == 'y' && game.GameOver {
go game.Run()
} else if event.Rune() == 'n' && game.GameOver {
game.Screen.Fini()
os.Exit(0)
}
}
}
}