Page 1 of 1

[Solved]Snake Game: unable to draw tail correctly

Posted: Sat Oct 26, 2019 9:19 pm
by apolius
I've been creating the snake game in love and i wasn't able to draw the tail correctly due to not being able to give each tail block the correct x and y.
Basically there is this array of tail blocks that starts at 0 and gains one unit each time the snake eats food, this unit has x and y coordinates.
each update cycle, the snake moves, the tail moves(new x,y for each unit) and the tail gets longer if food is eaten.

I tried several times but my spaghetti code didn't cut it.
I know I should have used a timer variable and a proper grid but i wanted this one to be different.

TLDR: in main.lua exists an updateTail function, where unit.x and unit.y reside.

Thanks for taking the time to read this and a huge hug is waiting for any wonderful person who gives me advice regarding this subject.

Re: Snake Game: unable to draw tail correctly

Posted: Sat Oct 26, 2019 10:24 pm
by Xugro
Reverse the order of the tail-update in line 31 from

Code: Select all

for i = #tail, 1, -1 do
to

Code: Select all

for i = 1, #tail do
and it will work.

In your version the position of an element will be set on the position of the previous element and all tail-elements will be on the same position after the update.

And if you know that you are writing spaghetti code: Try to write better code / refactor your code. This will help you tremendously :)

Re: Snake Game: unable to draw tail correctly

Posted: Sun Oct 27, 2019 7:16 pm
by apolius
Sometimes, you just need to stop, call your grandma, walk your dog, then come back to have a fresh look, pretty sure this was one of these moments, thanks for the help <3