Snake's body not appearing and game crashing for some reason

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
mynameisnotpaul
Prole
Posts: 10
Joined: Sun Jan 01, 2017 4:25 am

Snake's body not appearing and game crashing for some reason

Post by mynameisnotpaul »

I'm having (another) problem with my snake game. The code I have for the creation of the snake's body doesn't seem to work, and crashes the game too. I know that the problem that leads to the game crashing is located somewhere in the lastCalc() function, but I have no idea how it causes the game to crash as it seems like it would execute without any problems.

Here's the code:

Code: Select all

local height = love.graphics.getHeight()
local width = love.graphics.getWidth()

local gameover = false
local lastX = {

}
local lastY = {

}
local nummiesAte = 1
local GMmessage = ""
local Xloss = false
local Yloss = false
local i = 0
local t = 0
local q  = 0
local lastsDone = 1
didThing = false
function love.load()
    direction = 0
    time = 0
    snakeHead = {
    x = 500,
    y = 200,
    speed = 0.1
    }

    nummyBool = false

end


function love.update (dt)
  time = time + dt
  eatNummy()
  checkBorder()
  if gameover ~= true then
    if time > snakeHead.speed then
      if direction == 1 then
        snakeHead.y = snakeHead.y -20
      elseif direction == 2 then
        snakeHead.x = snakeHead.x +20
      elseif direction == 3 then
        snakeHead.y = snakeHead.y +20
      elseif direction == 4 then
        snakeHead.x = snakeHead.x -20
      end
      preLastCalc()
      lastCalc()
      time = 0
      lastsDone = lastsDone + 1
    end
  elseif gameover == true then
      GMmessage = "you lost"

  end

end


function love.draw()
  spawnNummy()
  if nummyBool == true then
    love.graphics.setColor(0,255,0)
    love.graphics.rectangle('fill', nummyX, nummyY, 20, 20)
  end
  love.graphics.setColor(255, 255, 255)
  love.graphics.rectangle('fill', snakeHead.x, snakeHead.y, 20, 20)
  love.graphics.setColor(255,0,0)
  love.graphics.print(GMmessage, width/2, height/2)
  if lastsDone > 1 then
    for t=1, lastsDone do
      love.graphics.setColor(255,255,255)
      love.graphics.rectangle('fill', lastX[t], lastY[t], 20, 20)
    end
  end
end


function love.keypressed(key)
  if key == ("up") then
    direction = 1
  elseif key == ("right") then
    direction = 2
  elseif key == ("down") then
    direction = 3
  elseif key == ("left") then
    direction = 4
  end
end

function checkBorder()
  if snakeHead.x > width or snakeHead.x < 0 then
    gameover = true
    Xloss = true
  end
  if snakeHead.y > height or snakeHead.y < 0 then
    gameover = true
    Yloss = true
  end
end

function spawnNummy()
  if nummyBool == false then
    nummyX = math.random(love.graphics.getWidth()/20)*20
    nummyY = math.random(love.graphics.getHeight()/20)*20
    if nummyX ~= snakeHead.x and nummyY ~= snakeHead.y then
      nummyBool = true
    end
  end
  end

function eatNummy()
  if snakeHead.x == nummyX and snakeHead.y == nummyY then
    nummiesAte = nummiesAte + 1
    nummyBool = false
  end
end

function lastCalc()
  lastX[lastsDone] = snakeHead.x
  lastY[lastsDone] = snakeHead.y
end

function preLastCalc()
  local k= 0

  if lastsDone >= 2 and lastsDone >= nummiesAte then
    for k= 1, lastsDone do
      lastX[k] = lastX[k+1]
    end
  end
end
Also I've attached the .love file:
snakegame.love
(1.91 KiB) Downloaded 86 times
Thanks for reading and any help would be appreciated :) !
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Snake's body not appearing and game crashing for some reason

Post by zorg »

The error i got clearly said that the error was on line 75: love.graphics.rectangle('fill', lastX[t], lastY[t], 20, 20)
and it was that lastX[t] was empty, basically.

lastY[t] might have a similar issue as well, maybe the t variable you give it is wrong, as in, it's larger that the number of values you have in lastX; i don't know.

But, if i had to guess, you add 1 to lastsDone at the end of love.update (so it becomes equal to 2) but you only defined lastX[1] and lastY[1]...
One solution would be to do for t=1, lastsDone-1 do but understanding why this happens can help more.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
mynameisnotpaul
Prole
Posts: 10
Joined: Sun Jan 01, 2017 4:25 am

Re: Snake's body not appearing and game crashing for some reason

Post by mynameisnotpaul »

What I meant about the problem being with lastCalc() is that that's what causes the problem at line 75. I'll try implementing what you said when I'm working on it again and I'll see if it gets fixed.
mynameisnotpaul
Prole
Posts: 10
Joined: Sun Jan 01, 2017 4:25 am

Re: Snake's body not appearing and game crashing for some reason

Post by mynameisnotpaul »

I completely rewrote the code to try doing things a little bit differently to see if it would turn out well, but I'm now struggling to make a trail for my snake. In my for statement it says that the variable that goes up in my for statement is nil, when it isn't. This code is a lot smaller so the error should be easy to find, but it seems I'm blind to it.

Code: Select all

function love.load()
  width = love.graphics.getWidth()/20
  height = love.graphics.getHeight()/20
  snake = {
    length = 5,
    x = {math.random(width)*20},
    y = {math.random(height)*20}
  }
  counter = 1
  time = 0
end

function love.update(dt)
  local speed = 0.1
  time = time + dt
  if time >= speed then
    snake.x[counter] = snake.x[1]
    snake.y[counter] = snake.y[1]

    snake.x[1] = snake.x[1] - 20
    time = time - speed
    counter = counter + 1
  end

end

function love.draw()
  love.graphics.setColor(255,255,255)

  if counter > 1 then
    for i = 1, counter do
      love.graphics.rectangle('fill', snake.x[i], snake.y[i], 20, 20)
    end
  end
end
Thanks for reading (:!
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Snake's body not appearing and game crashing for some reason

Post by zorg »

Yes, the code became much more neat! :3

The issue is actually the same though, like it was before, here are the steps up to the error:
You define one element for both snake.x and snake.y in love.load.
You define counter to be 1 in love.load.
In love update, when enough time elapsed, you set snake.x[counter] to snake.x[1], and similarly with y... counter is still 1 there.
Then you increase counter at the end.
Finally, in love.draw, you again try to draw out a rectangle at snake.x[2] which doesn't exist, because of the above two reasons.

This time, this should be an easier fix:

Code: Select all

function love.update(dt)
  local speed = 0.1
  time = time + dt
  if time >= speed then
    counter = counter + 1 -- Moved this from the end to here, so that the assignment works.
    snake.x[counter] = snake.x[1]
    snake.y[counter] = snake.y[1]

    snake.x[1] = snake.x[1] - 20
    time = time - speed
  end
end
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
mynameisnotpaul
Prole
Posts: 10
Joined: Sun Jan 01, 2017 4:25 am

Re: Snake's body not appearing and game crashing for some reason

Post by mynameisnotpaul »

zorg wrote:Yes, the code became much more neat! :3

The issue is actually the same though, like it was before, here are the steps up to the error:
You define one element for both snake.x and snake.y in love.load.
You define counter to be 1 in love.load.
In love update, when enough time elapsed, you set snake.x[counter] to snake.x[1], and similarly with y... counter is still 1 there.
Then you increase counter at the end.
Finally, in love.draw, you again try to draw out a rectangle at snake.x[2] which doesn't exist, because of the above two reasons.

This time, this should be an easier fix:

Code: Select all

function love.update(dt)
  local speed = 0.1
  time = time + dt
  if time >= speed then
    counter = counter + 1 -- Moved this from the end to here, so that the assignment works.
    snake.x[counter] = snake.x[1]
    snake.y[counter] = snake.y[1]

    snake.x[1] = snake.x[1] - 20
    time = time - speed
  end
end

Thanks it worked! :)
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests