-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmenu.go
161 lines (143 loc) · 4.67 KB
/
menu.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"math/rand"
"strings"
"time"
"github.com/nsf/termbox-go"
)
type Point struct {
x int
y int
}
type Star struct {
Point
vx int
vy int
}
const (
menuPad = 10
fgMenu = red
bgMenu = termbox.ColorBlack
fgMenuHighlight = termbox.ColorBlack
bgMenuHighlight = neonGreen
fgStar = termbox.ColorWhite
bgStar = termbox.ColorBlack
numStars = 50
starSymbol = "."
logoY = 3
logo = `_______________ ________________ ____ _______________ _______________
I I I I / \ I I I I
I I I ------- I / /\ \ I I I I
I --------- I I I I / / \ \ I I I I
I I I ------- I / / \ \ I ------- I -------
I I I I / /______\ \ I I I I
I I I --------- / \ I I I -------
I --------- I I / __ \ I I I I
I I I I / / \ \ I I I I
I I I I / / \ \ I I I -------
I I I I / / \ \ I I I I
------ I I I / / \ \ I ------- I -------
I I I I / / \ \ I I I I
I I I I I I I I I I I I
I I I I I I I I I I I I
--------------- -------- ---------- --------- --------------- ---------------
________ _____ ____ __ __ ____ --------| --------- ___________
I I I \ I I \ \ / / /\ I \ I ----| I ----- I I I
-- -- I \ I I \ \ / / / \ I I\ \ I I I I I I I -----
I I I I\ \ I I \ \ / / / oo \ I I/ / I ----| I ----- \ I I
I I I I \ \I I \ \ / / / __ \ I / I ----| I ____ \ I I
-- -- I I \ \ I \ \/ / / / \ \ I / I I I I \ \ ------ I
I I I I \ I \ / / / \ \ I / I ----| I I \ \ I I
-------- ----- ---- \/ /__/ \__\ I/ --------| ----- --- -----------`
)
const (
FirstMenuItem = 0
Play int = iota - 1
Highscores
Howto
NumMenuItems
)
var (
menuItems = map[int]string{Play: "PLAY", Highscores: "HIGHSCORES", Howto: "HOWTO"}
logoLines = strings.Split(logo, "\n")
logoLineLength = len(logoLines[0])
logoHeight = len(logoLines)
stars = make([]*Star, 0, numStars)
)
func PrintLogo(x, y int, fg, bg termbox.Attribute, lines []string) {
for _, line := range lines {
tbprint(x, y, fg, bg, line)
y++
}
}
func (g *Game) DrawMenu() {
x := g.w/2 - logoLineLength/2
y := logoY
PrintLogo(x, y, fgMenu, bgMenu, logoLines)
length := 0
i := 0
for _, v := range menuItems {
length += len(v)
if i+1 != len(menuItems) {
length += menuPad
}
i++
}
x = g.w/2 - length/2
y += logoHeight + 5
for i := FirstMenuItem; i < NumMenuItems; i++ {
v := menuItems[i]
if i == g.hmi {
tbprint(x, y, fgMenuHighlight, bgMenuHighlight, v)
} else {
tbprint(x, y, fgMenu, bgMenu, v)
}
x += len(v) + menuPad
}
for _, s := range stars {
tbprint(s.x, s.y, fgStar, bgStar, starSymbol)
}
}
func (g *Game) UpdateMenu() {
if len(stars) != cap(stars) && g.fc%3 == 0 {
n := len(stars)
stars = stars[0 : n+1]
stars[n] = NewStar(g.w, rand.Intn(g.h))
}
for i, s := range stars {
s.x += s.vx
s.y += s.vy
if s.x < 0 || s.x > g.w || s.y < 0 || s.y > g.h {
stars[i] = NewStar(g.w, rand.Intn(g.h))
}
}
}
func NewStar(x, y int) *Star {
rand.Seed(time.Now().UTC().UnixNano())
vx, vy := -1*(1+rand.Intn(3)), 0
return &Star{Point{x, y}, vx, vy}
}
func (g *Game) HandleKeyMenu(k termbox.Key) {
switch k {
case termbox.KeyArrowLeft:
// because of Go's bad mod operator, have to add the length here
g.hmi = (g.hmi - 1 + NumMenuItems) % NumMenuItems
case termbox.KeyArrowRight:
g.hmi = (g.hmi + 1) % NumMenuItems
case termbox.KeyEnter:
switch g.hmi {
case Highscores:
g.GoHighscores()
case Howto:
g.GoHowto()
case Play:
g.GoPlay()
}
}
}
func (g *Game) GoMenu() {
g.state = MenuState
g.cfg = fgMenu
g.cbg = bgMenu
g.hmi = FirstMenuItem
}