General discussion about LÖVE, Lua, game development, puns, and unicorns.
danukas155
Prole
Posts: 1 Joined: Tue Feb 01, 2022 8:12 pm
Post
by danukas155 » 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.
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
this is the only part where I used snake.body
BrotSagtMist
Party member
Posts: 657 Joined: Fri Aug 06, 2021 10:30 pm
Post
by BrotSagtMist » Tue Feb 01, 2022 9:59 pm
Just give us the full .love file.
Following an out of context logic is kinda hard.
obey
dusoft
Party member
Posts: 635 Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:
Post
by dusoft » Tue Feb 01, 2022 10:00 pm
Also, this kind of question goes into support, not in general.
pgimeno
Party member
Posts: 3656 Joined: Sun Oct 18, 2015 2:58 pm
Post
by pgimeno » Tue Feb 01, 2022 11:53 pm
Not to mention that the problem might be somewhere else (happens quite often).
darkfrei
Party member
Posts: 1197 Joined: Sat Feb 08, 2020 11:09 pm
Post
by darkfrei » Wed Feb 02, 2022 8:08 am
Code: Select all
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]
Why are you storing x and y in two separated tables?
Users browsing this forum: Google [Bot] and 2 guests