/
gosnake.go
226 lines (190 loc) · 4.14 KB
/
gosnake.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"fmt"
"github.com/banthar/gl"
"github.com/banthar/glu"
"github.com/jteeuwen/glfw"
"os"
)
type Counter struct {
hit, current int
}
func (c *Counter) Tick() bool {
c.current++
if c.current%c.hit == 0 {
return true
}
return false
}
type Coord struct {
x, y int
}
type Snake struct {
coords []Coord
grow bool
}
func (s *Snake) Grow() {
s.grow = true
}
func (s *Snake) Move(dir int) {
head := s.coords[len(s.coords)-1]
switch dir {
case DirUp:
s.coords = append(s.coords, Coord{x: head.x, y: head.y + 1})
case DirDown:
s.coords = append(s.coords, Coord{x: head.x, y: head.y - 1})
case DirLeft:
s.coords = append(s.coords, Coord{x: head.x - 1, y: head.y})
case DirRight:
s.coords = append(s.coords, Coord{x: head.x + 1, y: head.y})
}
if s.grow {
s.grow = false
} else {
s.coords = s.coords[1:]
}
}
const (
Title = "Snake"
Width = 480
Height = 480
)
const (
DirUp = iota
DirDown
DirLeft
DirRight
)
var (
snake Snake
food map[Coord]bool = make(map[Coord]bool)
grow map[Coord]bool = make(map[Coord]bool)
dir int = DirUp
running bool = true
counter Counter = Counter{hit: 6, current: 0}
)
func main() {
var err error
if err = glfw.Init(); err != nil {
fmt.Fprintf(os.Stderr, "[e] %v\n", err)
return
}
defer glfw.Terminate()
if err = glfw.OpenWindow(Width, Height, 8, 8, 8, 8, 0, 8, glfw.Windowed); err != nil {
fmt.Fprintf(os.Stderr, "[e] %v\n", err)
return
}
defer glfw.CloseWindow()
glfw.SetSwapInterval(1)
glfw.SetWindowTitle(Title)
glfw.SetWindowSizeCallback(onResize)
glfw.SetKeyCallback(onKey)
initGL()
initSnake()
initFood()
for running && glfw.WindowParam(glfw.Opened) == 1 {
update()
drawScene()
}
}
func onResize(w, h int) {
if h == 0 {
h = 1
}
gl.Viewport(0, 0, w, h)
gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity()
glu.Perspective(45.0, float64(w)/float64(h), 0.1, 100.0)
gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()
}
func onKey(key, state int) {
// TODO Magic 1?
if state != 1 {
return
}
switch key {
case glfw.KeyEsc:
running = false
case glfw.KeyUp:
dir = DirUp
case glfw.KeyDown:
dir = DirDown
case glfw.KeyLeft:
dir = DirLeft
case glfw.KeyRight:
dir = DirRight
}
}
func initGL() {
gl.ShadeModel(gl.SMOOTH)
gl.ClearColor(0, 0, 0, 0)
gl.ClearDepth(1)
gl.Enable(gl.DEPTH_TEST)
gl.DepthFunc(gl.LEQUAL)
gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST)
}
func initSnake() {
snake.coords = append(snake.coords, Coord{x: 1, y: 2})
snake.coords = append(snake.coords, Coord{x: 2, y: 2})
snake.coords = append(snake.coords, Coord{x: 2, y: 3})
snake.coords = append(snake.coords, Coord{x: 2, y: 4})
snake.coords = append(snake.coords, Coord{x: 3, y: 4})
snake.coords = append(snake.coords, Coord{x: 4, y: 4})
dir = DirUp
}
func initFood() {
food[Coord{x: 3, y: 3}] = true
food[Coord{x: 7, y: 3}] = true
food[Coord{x: 3, y: 5}] = true
food[Coord{x: 6, y: 3}] = true
}
func update() {
if hit := counter.Tick(); !hit {
return
}
head := snake.coords[len(snake.coords)-1]
tail := snake.coords[0]
// Eat food
if _, present := food[head]; present {
delete(food, head)
grow[tail] = true
}
// Move snake
snake.Move(dir)
if _, present := grow[tail]; present {
snake.Grow()
}
}
func drawScene() {
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
// Food
for coord, _ := range food {
gl.LoadIdentity()
gl.Scalef(0.05, 0.05, 1)
gl.Translatef(float32(coord.x)-5, float32(coord.y)-5, -1)
gl.Begin(gl.QUADS)
gl.Color3f(0.5, float32(coord.x)/10, float32(coord.y)/10)
gl.Vertex3f(0.25, 0.75, 0)
gl.Vertex3f(0.75, 0.75, 0)
gl.Color3f(0.3, float32(coord.x)/10-0.2, float32(coord.y)/10-0.2)
gl.Vertex3f(0.75, 0.25, 0)
gl.Vertex3f(0.25, 0.25, 0)
gl.End()
}
//Snake
for _, coord := range snake.coords {
gl.LoadIdentity()
gl.Scalef(0.05, 0.05, 1)
gl.Translatef(float32(coord.x)-5, float32(coord.y)-5, -1)
gl.Begin(gl.QUADS)
gl.Color3f(float32(coord.y)/10, float32(coord.x)/10, 0.5)
gl.Vertex3f(0, 1, 0)
gl.Vertex3f(1, 1, 0)
gl.Color3f(float32(coord.y)/10-0.2, float32(coord.x)/10-0.2, 0.3)
gl.Vertex3f(1, 0, 0)
gl.Vertex3f(0, 0, 0)
gl.End()
}
glfw.SwapBuffers()
}