Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use point struct as position #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@ var snakeGame = Game{
color.RGBA{160, 160, 160, 255},
},
snake: Snake{
body: [208][2]int16{
{0, 3},
{0, 2},
{0, 1},
body: [208]Point{
{x: 0, y: 3},
{x: 0, y: 2},
{x: 0, y: 1},
},
length: 3,
direction: 3,
},
appleX: -1,
appleY: -1,
apple: Point{-1, -1},
status: START,
}

Expand Down
62 changes: 36 additions & 26 deletions snake.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ const (
HEIGHTBLOCKS = 13
)

type Point struct {
Copy link
Member

@doniacld doniacld Oct 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For history of this structure, it could be nice to have a comment explaining why we are not using image.Point using the comment from @conejoninja.

Suggested change
type Point struct {
// Point is a custom type defining a point with x, y coordinates as int16 like the display coordinates.
type Point struct {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add the comment.

x int16
y int16
}

var (
// Those variable are there for a more easy reading of the apple shape.
re = colors[APPLE] // red
Expand All @@ -51,15 +56,20 @@ var (
)

type Snake struct {
body [208][2]int16
body [208]Point
length int16
// direction should take one onf the folloing positions:
// 0 for left
// 1 for up
// 2 for down
// 3 for right
direction int16
}

type Game struct {
colors []color.RGBA
snake Snake
appleX, appleY int16
apple Point
status uint8
}

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

g.snake.body[0][0] = 0
g.snake.body[0][1] = 3
g.snake.body[1][0] = 0
g.snake.body[1][1] = 2
g.snake.body[2][0] = 0
g.snake.body[2][1] = 1
g.snake.body[0].x = 0
g.snake.body[0].y = 3
g.snake.body[1].x = 0
g.snake.body[1].y = 2
g.snake.body[2].x = 0
g.snake.body[2].y = 1

g.snake.length = 3
g.snake.direction = 3
Expand Down Expand Up @@ -168,26 +178,26 @@ func (g *Game) Start() {

func (g *Game) collisionWithSnake(x, y int16) bool {
for i := int16(0); i < g.snake.length; i++ {
if x == g.snake.body[i][0] && y == g.snake.body[i][1] {
if x == g.snake.body[i].x && y == g.snake.body[i].y {
return true
}
}
return false
}

func (g *Game) createApple() {
g.appleX = int16(rand.Int31n(16))
g.appleY = int16(rand.Int31n(13))
for g.collisionWithSnake(g.appleX, g.appleY) {
g.appleX = int16(rand.Int31n(16))
g.appleY = int16(rand.Int31n(13))
g.apple.x = int16(rand.Int31n(16))
g.apple.y = int16(rand.Int31n(13))
for g.collisionWithSnake(g.apple.x, g.apple.y) {
g.apple.x = int16(rand.Int31n(16))
g.apple.y = int16(rand.Int31n(13))
}
g.drawApple(g.appleX, g.appleY)
g.drawApple(g.apple)
}

func (g *Game) moveSnake() {
x := g.snake.body[0][0]
y := g.snake.body[0][1]
x := g.snake.body[0].x
y := g.snake.body[0].y

switch g.snake.direction {
case 0:
Expand Down Expand Up @@ -222,28 +232,28 @@ func (g *Game) moveSnake() {

// draw head
g.drawSnakePartial(x, y, g.colors[SNAKE])
if x == g.appleX && y == g.appleY {
if x == g.apple.x && y == g.apple.y {
g.snake.length++
g.createApple()
} else {
// remove tail
g.drawSnakePartial(g.snake.body[g.snake.length-1][0], g.snake.body[g.snake.length-1][1], g.colors[BCK])
g.drawSnakePartial(g.snake.body[g.snake.length-1].x, g.snake.body[g.snake.length-1].y, g.colors[BCK])
}
for i := g.snake.length - 1; i > 0; i-- {
g.snake.body[i][0] = g.snake.body[i-1][0]
g.snake.body[i][1] = g.snake.body[i-1][1]
g.snake.body[i].x = g.snake.body[i-1].x
g.snake.body[i].y = g.snake.body[i-1].y
}
g.snake.body[0][0] = x
g.snake.body[0][1] = y
g.snake.body[0].x = x
g.snake.body[0].y = y
}

func (g *Game) drawApple(x, y int16) {
display.FillRectangleWithBuffer(10*x, 10*y, 10, 10, appleBuf)
func (g *Game) drawApple(p Point) {
display.FillRectangleWithBuffer(10*p.x, 10*p.y, 10, 10, appleBuf)
}

func (g *Game) drawSnake() {
for i := int16(0); i < g.snake.length; i++ {
g.drawSnakePartial(g.snake.body[i][0], g.snake.body[i][1], g.colors[SNAKE])
g.drawSnakePartial(g.snake.body[i].x, g.snake.body[i].y, g.colors[SNAKE])
}
}

Expand Down