-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (75 loc) · 1.65 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
package main
import (
"github.com/nsf/termbox-go"
"time"
)
func main() {
// set up the game
grid := newGrid(30, 15)
input := newInput()
view := newView(grid)
snakeHead := newSnake()
snakeHead.SetPosition(grid.CenterPosition())
grid.Cell(grid.CenterPosition()).s = snakeHead
game := newGame(grid, snakeHead)
err := termbox.Init()
// termbox.SetOutputMode(termbox.Output256)
// termbox.SetInputMode(termbox.InputAlt | termbox.InputMouse)
if err != nil {
panic(err)
}
defer termbox.Close()
input.start()
defer input.stop()
quit := make(chan struct{})
go func() {
for {
select {
case ev := <- input.queue:
if ev.Type == termbox.EventKey {
switch ev.Key {
case termbox.KeyArrowLeft:
snakeHead.direction = directionLeft
case termbox.KeyArrowRight:
snakeHead.direction = directionRight
case termbox.KeyArrowUp:
snakeHead.direction = directionUp
case termbox.KeyArrowDown:
snakeHead.direction = directionDown
case termbox.KeySpace:
game.move()
case termbox.KeyEsc, termbox.KeyCtrlC, termbox.KeyCtrlD:
close(quit)
return
default:
switch ev.Ch {
case 'q':
close(quit)
return
case 's':
game.start()
// case 'p':
// game.pause()
}
}
}
case <- game.moveTimer.C:
game.move()
}
}
}()
clock := time.Now()
loop:
for {
select {
default:
tickTime := time.Now()
view.delta = tickTime.Sub(clock).Seconds()
clock = tickTime
view.draw()
time.Sleep(time.Duration((tickTime.Sub(time.Now()).Seconds()*1000.0)+1000.0/view.fps) * time.Millisecond)
case <- quit:
break loop;
}
}
}