I'm trying to use a dictionary to keep track of the movements of the snake in a snake game. So far, I've managed to keep track of the head of the snake, but I fail to see how I should keep track of the body movements.
What I'm doing is this: When the head of the snake moves one cell I fill the gap with the last segment of the body (when the snake doesn't grow) or with a new body segment (when the snake grows). How should I keep track of that movement? What should I erase?
My code for the head movement (GridData.Content is the dictionary):
func _on_Timer_timeout():
GridData.previousHeadPosition = GridData.GetCellPosition(self.position)
GridData.Content[GridData.previousHeadPosition] = GridData.Item.SNAKE
match direction:
Here I check which direction the head should go (this works fine)
GridData.currentHeadPosition = GridData.GetCellPosition(self.position)
GridData.Content[GridData.currentHeadPosition] = GridData.Item.SNAKE
My code for the body movement so far (There are a total of 90 body segments, but I'm only using 10 of them):
var noOfBodySegments := 90
var bodySegmentArray := []
var startCell := Vector2(7, 7)
var counterStart := 0
var counterEnd := 9
var counter := counterStart
onready var timer = get_node("Timer")
func _ready():
var bodySegment
for _i in range(noOfBodySegments):
bodySegment = Sprite.new()
bodySegment.texture = load("res://Sprites/Body.png")
add_child(bodySegment)
bodySegment.position = GridData.GetPixelPosition(Vector2(-1, -1))
bodySegmentArray.append(bodySegment)
func _on_Timer_timeout():
bodySegmentArray[counter].position = GridData.GetPixelPosition(GridData.currentHeadPosition)
counter += 1
if counter > counterEnd:
counter = counterStart
Thanks for having a look at this.