Page 1 of 1

Something is wrong with my code

Posted: Tue Feb 01, 2022 8:21 pm
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

Re: Something is wrong with my code

Posted: Tue Feb 01, 2022 9:59 pm
by BrotSagtMist
Just give us the full .love file.
Following an out of context logic is kinda hard.

Re: Something is wrong with my code

Posted: Tue Feb 01, 2022 10:00 pm
by dusoft
Also, this kind of question goes into support, not in general.

Re: Something is wrong with my code

Posted: Tue Feb 01, 2022 11:53 pm
by pgimeno
Not to mention that the problem might be somewhere else (happens quite often).

Re: Something is wrong with my code

Posted: Wed Feb 02, 2022 8:08 am
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?