Created
September 27, 2022 04:23
-
-
Save erap129/3281fd4e437f00697832602fb2c8ef08 to your computer and use it in GitHub Desktop.
Snake Part 2 - snake.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
type SnakePart struct { | |
X int | |
Y int | |
} | |
type SnakeBody struct { | |
Parts []SnakePart | |
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) { | |
sb.Parts = append(sb.Parts, sb.Parts[len(sb.Parts)-1].GetUpdatedPart(sb, width, height)) | |
sb.Parts = sb.Parts[1:] | |
} | |
func (sp *SnakePart) GetUpdatedPart(sb *SnakeBody, width int, height int) SnakePart { | |
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