Skip to content

Commit dd89316

Browse files
committed
Use point struct as position
1 parent 6146dfb commit dd89316

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

main.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@ var snakeGame = Game{
2626
color.RGBA{160, 160, 160, 255},
2727
},
2828
snake: Snake{
29-
body: [208][2]int16{
30-
{0, 3},
31-
{0, 2},
32-
{0, 1},
29+
body: [208]Point{
30+
{x: 0, y: 3},
31+
{x: 0, y: 2},
32+
{x: 0, y: 1},
3333
},
3434
length: 3,
3535
direction: 3,
3636
},
37-
appleX: -1,
38-
appleY: -1,
37+
apple: Point{-1, -1},
3938
status: START,
4039
}
4140

snake.go

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ const (
2929
HEIGHTBLOCKS = 13
3030
)
3131

32+
type Point struct {
33+
x int16
34+
y int16
35+
}
36+
3237
var (
3338
// Those variable are there for a more easy reading of the apple shape.
3439
re = colors[APPLE] // red
@@ -51,15 +56,20 @@ var (
5156
)
5257

5358
type Snake struct {
54-
body [208][2]int16
59+
body [208]Point
5560
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
5666
direction int16
5767
}
5868

5969
type Game struct {
6070
colors []color.RGBA
6171
snake Snake
62-
appleX, appleY int16
72+
apple Point
6373
status uint8
6474
}
6575

@@ -114,12 +124,12 @@ func (g *Game) Start() {
114124
case PLAY:
115125
display.FillScreen(g.colors[BCK])
116126

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
123133

124134
g.snake.length = 3
125135
g.snake.direction = 3
@@ -168,26 +178,26 @@ func (g *Game) Start() {
168178

169179
func (g *Game) collisionWithSnake(x, y int16) bool {
170180
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 {
172182
return true
173183
}
174184
}
175185
return false
176186
}
177187

178188
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))
184194
}
185-
g.drawApple(g.appleX, g.appleY)
195+
g.drawApple(g.apple)
186196
}
187197

188198
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
191201

192202
switch g.snake.direction {
193203
case 0:
@@ -222,28 +232,28 @@ func (g *Game) moveSnake() {
222232

223233
// draw head
224234
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 {
226236
g.snake.length++
227237
g.createApple()
228238
} else {
229239
// 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])
231241
}
232242
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
235245
}
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
238248
}
239249

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)
242252
}
243253

244254
func (g *Game) drawSnake() {
245255
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])
247257
}
248258
}
249259

0 commit comments

Comments
 (0)