-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (87 loc) · 1.69 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
package main
import (
"os"
"os/signal"
"time"
"github.com/nsf/termbox-go"
)
type sizes struct {
width int
height int
}
func initMatrix(w, h int) [][]*Ship {
matrix := make([][]*Ship, w+1)
for x := 0; x < w+1; x++ {
matrix = append(matrix, make([]*Ship, h+1))
for y := 0; y < h+1; y++ {
matrix[x] = append(matrix[x], nil)
}
}
return matrix
}
func cleanExit() {
termbox.Close()
os.Exit(0)
}
func main() {
err := termbox.Init()
if err != nil {
os.Exit(1)
}
termbox.HideCursor()
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
w, h := termbox.Size()
size := sizes{width: w, height: h}
matrix := initMatrix(w, h)
fpsSleepTime := time.Duration(1000000/30) * time.Microsecond
go func() {
for {
time.Sleep(fpsSleepTime)
termbox.Flush()
}
}()
eventChan := make(chan termbox.Event)
go func() {
for {
event := termbox.PollEvent()
eventChan <- event
}
}()
sigChan := make(chan os.Signal)
signal.Notify(sigChan, os.Interrupt)
signal.Notify(sigChan, os.Kill)
ship := initShip(size, matrix)
go ship.run()
initBarrier(size, matrix)
initFleet(size, matrix)
eventLoop:
for {
select {
case event := <-eventChan:
switch event.Type {
case termbox.EventKey:
switch event.Key {
case termbox.KeyCtrlZ, termbox.KeyCtrlC:
break eventLoop
case termbox.KeyArrowLeft:
ship.moveLeft()
case termbox.KeyArrowRight:
ship.moveRight()
case termbox.KeySpace:
ship.fire()
}
switch event.Ch {
case 'q':
break eventLoop
case 'c':
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
}
case termbox.EventError:
break eventLoop
}
case <-sigChan:
break eventLoop
}
}
cleanExit()
}