[Solved]Snake Game: unable to draw tail correctly

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
apolius
Prole
Posts: 3
Joined: Fri Oct 18, 2019 4:19 pm

[Solved]Snake Game: unable to draw tail correctly

Post 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.
Attachments
the_snakegame.love
(1.25 KiB) Downloaded 122 times
Last edited by apolius on Sun Oct 27, 2019 7:16 pm, edited 1 time in total.
Xugro
Party member
Posts: 110
Joined: Wed Sep 29, 2010 8:14 pm

Re: Snake Game: unable to draw tail correctly

Post 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 :)
apolius
Prole
Posts: 3
Joined: Fri Oct 18, 2019 4:19 pm

Re: Snake Game: unable to draw tail correctly

Post 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
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 25 guests