Something is wrong with my code
Posted: Tue Feb 01, 2022 8:21 pm
So I am trying to make snake again, to try adding AI to it, but I got stuck with the tail, I checked my code about 10 time and still can't find the problem. When snake eats the fruit the tail length changes only when I pass the fruit fully with all body, not instantly. I have made a snake before and checked its code with my current snake, everything looks fine. Maybe you can spot what is wrong with it.
this is the only part where I used snake.body
Code: Select all
function movementUpdate(movements)
tails()
if movements == 1 then
snake.head.x = snake.head.x - snake.size
elseif movements == 2 then
snake.head.x = snake.head.x + snake.size
end
if movements == 3 then
snake.head.y = snake.head.y - snake.size
elseif movements == 4 then
snake.head.y = snake.head.y + snake.size
end
timer = 0
end
function scoring()
if snake.head.x == apple.x - apple.size / 2 and snake.head.y == apple.y - apple.size / 2 then
score = score + 1
snake.body.amount = snake.body.amount + 1
snake.body.x[snake.body.amount] = snake.body.x[snake.body.amount - 1]
snake.body.y[snake.body.amount] = snake.body.y[snake.body.amount - 1]
tails()
appleCreation()
end
end
function tails()
snake.body.x[0] = snake.head.x
snake.body.y[0] = snake.head.y
if snake.body.amount > 0 then
for i = snake.body.amount, 1, - 1 do
snake.body.x[i] = snake.body.x[i - 1]
snake.body.y[i] = snake.body.y[i - 1]
end
end
end