@@ -29,6 +29,11 @@ const (
29
29
HEIGHTBLOCKS = 13
30
30
)
31
31
32
+ type Point struct {
33
+ x int16
34
+ y int16
35
+ }
36
+
32
37
var (
33
38
// Those variable are there for a more easy reading of the apple shape.
34
39
re = colors [APPLE ] // red
@@ -51,15 +56,20 @@ var (
51
56
)
52
57
53
58
type Snake struct {
54
- body [208 ][ 2 ] int16
59
+ body [208 ]Point
55
60
length int16
61
+ // direction should take one onf the folloing positions:
62
+ // 0 for left
63
+ // 1 for up
64
+ // 2 for down
65
+ // 3 for right
56
66
direction int16
57
67
}
58
68
59
69
type Game struct {
60
70
colors []color.RGBA
61
71
snake Snake
62
- appleX , appleY int16
72
+ apple Point
63
73
status uint8
64
74
}
65
75
@@ -114,12 +124,12 @@ func (g *Game) Start() {
114
124
case PLAY :
115
125
display .FillScreen (g .colors [BCK ])
116
126
117
- g .snake .body [0 ][ 0 ] = 0
118
- g .snake .body [0 ][ 1 ] = 3
119
- g .snake .body [1 ][ 0 ] = 0
120
- g .snake .body [1 ][ 1 ] = 2
121
- g .snake .body [2 ][ 0 ] = 0
122
- g .snake .body [2 ][ 1 ] = 1
127
+ g .snake .body [0 ]. x = 0
128
+ g .snake .body [0 ]. y = 3
129
+ g .snake .body [1 ]. x = 0
130
+ g .snake .body [1 ]. y = 2
131
+ g .snake .body [2 ]. x = 0
132
+ g .snake .body [2 ]. y = 1
123
133
124
134
g .snake .length = 3
125
135
g .snake .direction = 3
@@ -168,26 +178,26 @@ func (g *Game) Start() {
168
178
169
179
func (g * Game ) collisionWithSnake (x , y int16 ) bool {
170
180
for i := int16 (0 ); i < g .snake .length ; i ++ {
171
- if x == g .snake .body [i ][ 0 ] && y == g .snake .body [i ][ 1 ] {
181
+ if x == g .snake .body [i ]. x && y == g .snake .body [i ]. y {
172
182
return true
173
183
}
174
184
}
175
185
return false
176
186
}
177
187
178
188
func (g * Game ) createApple () {
179
- g .appleX = int16 (rand .Int31n (16 ))
180
- g .appleY = int16 (rand .Int31n (13 ))
181
- for g .collisionWithSnake (g .appleX , g .appleY ) {
182
- g .appleX = int16 (rand .Int31n (16 ))
183
- g .appleY = int16 (rand .Int31n (13 ))
189
+ g .apple . x = int16 (rand .Int31n (16 ))
190
+ g .apple . y = int16 (rand .Int31n (13 ))
191
+ for g .collisionWithSnake (g .apple . x , g .apple . y ) {
192
+ g .apple . x = int16 (rand .Int31n (16 ))
193
+ g .apple . y = int16 (rand .Int31n (13 ))
184
194
}
185
- g .drawApple (g .appleX , g . appleY )
195
+ g .drawApple (g .apple )
186
196
}
187
197
188
198
func (g * Game ) moveSnake () {
189
- x := g .snake .body [0 ][ 0 ]
190
- y := g .snake .body [0 ][ 1 ]
199
+ x := g .snake .body [0 ]. x
200
+ y := g .snake .body [0 ]. y
191
201
192
202
switch g .snake .direction {
193
203
case 0 :
@@ -222,28 +232,28 @@ func (g *Game) moveSnake() {
222
232
223
233
// draw head
224
234
g .drawSnakePartial (x , y , g .colors [SNAKE ])
225
- if x == g .appleX && y == g .appleY {
235
+ if x == g .apple . x && y == g .apple . y {
226
236
g .snake .length ++
227
237
g .createApple ()
228
238
} else {
229
239
// remove tail
230
- g .drawSnakePartial (g .snake .body [g .snake .length - 1 ][ 0 ] , g .snake .body [g .snake .length - 1 ][ 1 ] , g .colors [BCK ])
240
+ g .drawSnakePartial (g .snake .body [g .snake .length - 1 ]. x , g .snake .body [g .snake .length - 1 ]. y , g .colors [BCK ])
231
241
}
232
242
for i := g .snake .length - 1 ; i > 0 ; i -- {
233
- g .snake .body [i ][ 0 ] = g .snake .body [i - 1 ][ 0 ]
234
- g .snake .body [i ][ 1 ] = g .snake .body [i - 1 ][ 1 ]
243
+ g .snake .body [i ]. x = g .snake .body [i - 1 ]. x
244
+ g .snake .body [i ]. y = g .snake .body [i - 1 ]. y
235
245
}
236
- g .snake .body [0 ][ 0 ] = x
237
- g .snake .body [0 ][ 1 ] = y
246
+ g .snake .body [0 ]. x = x
247
+ g .snake .body [0 ]. y = y
238
248
}
239
249
240
- func (g * Game ) drawApple (x , y int16 ) {
241
- display .FillRectangleWithBuffer (10 * x , 10 * y , 10 , 10 , appleBuf )
250
+ func (g * Game ) drawApple (p Point ) {
251
+ display .FillRectangleWithBuffer (10 * p . x , 10 * p . y , 10 , 10 , appleBuf )
242
252
}
243
253
244
254
func (g * Game ) drawSnake () {
245
255
for i := int16 (0 ); i < g .snake .length ; i ++ {
246
- g .drawSnakePartial (g .snake .body [i ][ 0 ] , g .snake .body [i ][ 1 ] , g .colors [SNAKE ])
256
+ g .drawSnakePartial (g .snake .body [i ]. x , g .snake .body [i ]. y , g .colors [SNAKE ])
247
257
}
248
258
}
249
259
0 commit comments