Something is wrong with my code

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
danukas155
Prole
Posts: 1
Joined: Tue Feb 01, 2022 8:12 pm

Something is wrong with my code

Post by danukas155 »

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
User avatar
BrotSagtMist
Party member
Posts: 655
Joined: Fri Aug 06, 2021 10:30 pm

Re: Something is wrong with my code

Post by BrotSagtMist »

Just give us the full .love file.
Following an out of context logic is kinda hard.
obey
User avatar
dusoft
Party member
Posts: 611
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Something is wrong with my code

Post by dusoft »

Also, this kind of question goes into support, not in general.
User avatar
pgimeno
Party member
Posts: 3640
Joined: Sun Oct 18, 2015 2:58 pm

Re: Something is wrong with my code

Post by pgimeno »

Not to mention that the problem might be somewhere else (happens quite often).
User avatar
darkfrei
Party member
Posts: 1195
Joined: Sat Feb 08, 2020 11:09 pm

Re: Something is wrong with my code

Post by darkfrei »

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?
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 1 guest