Skip to content

Instantly share code, notes, and snippets.

@erap129
Created September 27, 2022 13:08
Show Gist options
  • Save erap129/34e1d6d2461d261dd4ef1f0011a2bdd7 to your computer and use it in GitHub Desktop.
Save erap129/34e1d6d2461d261dd4ef1f0011a2bdd7 to your computer and use it in GitHub Desktop.
Snake Part 3 - snake.go
package main
type Part struct {
X int
Y int
}
type SnakeBody struct {
Parts []Part
Xspeed int
Yspeed int
}
func (sb *SnakeBody) ChangeDir(vertical int, horizontal int) {
sb.Yspeed = vertical
sb.Xspeed = horizontal
}
func (sb *SnakeBody) Update(width int, height int, longerSnake bool) {
sb.Parts = append(sb.Parts, sb.Parts[len(sb.Parts)-1].GetUpdatedPart(sb, width, height))
if !longerSnake {
sb.Parts = sb.Parts[1:]
}
}
func (sb *SnakeBody) ResetPos(width int, height int) {
snakeParts := []Part{
{
X: int(width / 2),
Y: int(height / 2),
},
{
X: int(width/2) + 1,
Y: int(height / 2),
},
{
X: int(width/2) + 2,
Y: int(height / 2),
},
}
sb.Parts = snakeParts
sb.Xspeed = 1
sb.Yspeed = 0
}
func (sp *Part) GetUpdatedPart(sb *SnakeBody, width int, height int) Part {
newPart := *sp
newPart.X = (newPart.X + sb.Xspeed) % width
if newPart.X < 0 {
newPart.X += width
}
newPart.Y = (newPart.Y + sb.Yspeed) % height
if newPart.Y < 0 {
newPart.Y += height
}
return newPart
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment