Dayanne
Dayanne

Reputation: 3

Love2d Gridlocked Player Issues

I am new to Löve so sorry for anything wrong I may say already. I've seen the Gridlocked Player tutorial and I wanted to do something like that based on another's person. It is a invisible maze that draws the tiles as you touch them.

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

function love.load()
    picture = love.graphics.newImage( "candy.png")
    back = love.graphics.newImage( "sky.jpg")
    cloud = love.graphics.newImage("cloud.png")
    love.mouse.setVisible(false)
    song = love.audio.newSource('elevator.mp3')
    song:setLooping(true)
    song:play()
    song:setVolume(0.1)
    anime = love.graphics.newImage("anime.png")
    music = love.audio.newSource("tick.mp3")
    music:setVolume(0.3)
    font = love.graphics.newFont("Kendal.ttf", 18)
    font1 = love.graphics.newFont("Kendal.ttf", 40)
    font2 = love.graphics.newFont("Kendal.ttf", 60)

    player = {
      grid_x = 256,
      grid_y = 256,
      act_x = 300,
      act_y = 300,
      speed = 20
    }

  map = {
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
    { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
    { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
    { 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
    { 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 },
    { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
  }

  -- Cria um array do mesmo tamanho do array de map
  seen_tiles = {}
  for yindex,column in ipairs(map) do
    seen_tiles[yindex] = {}
    for xindex,tile in ipairs(column) do
      seen_tiles[yindex][xindex] = 0
    end
  end

end

function love.update(dt)
  function testMap (x, y)
  if map[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 1 then
    -- Marca um bloco como visto assim que setá-lo para 1
    seen_tiles[(player.grid_y / 32) + y][(player.grid_x / 32) + x] = 1
    return false
  end
  return true
  end
  player.act_y = player.act_y - ((player.act_y - player.grid_y) * player.speed * dt)
  player.act_x = player.act_x - ((player.act_x - player.grid_x) * player.speed * dt)
end


function love.draw()    
  love.graphics.draw(back, 0, 0)
  love.graphics.setColor(3, 3, 3)
  love.graphics.rectangle("fill", 580, 0, width, height)
  love.graphics.setColor(255, 255, 255)
  love.graphics.rectangle("fill", player.act_x, player.act_y, 32, 32)
  love.graphics.draw(cloud, 591, 150, 0, 0.2111111)
  love.graphics.setFont(font1)
  love.graphics.print("Ghost Maze", 600, 200)
  love.graphics.setFont(font)
  love.graphics.print("Minimum movements: 12", 600, 280)
  love.graphics.print("Your movements:" .. points, 600, 300)
  love.graphics.draw( picture, love.mouse.getX() , love.mouse.getY() )
  love.graphics.draw(anime, 530, 360)

  -- Desenha os blocos se eles forem marcados como vistos
  for y=1, #seen_tiles do
        for x=1, #seen_tiles[y] do
            if seen_tiles[y][x] == 1 then
                love.graphics.setColor(120, 0, 200)
                love.graphics.rectangle("fill", x * 32, y * 32, 32, 32)
                love.graphics.setColor(255,255,255)
            end

        end
    end
end

function love.keypressed (key)
  if key == "escape" then
    love.event.quit()
  end
  if key == "up" then
    if testMap (0, -1) then
      player.grid_y = player.grid_y - 32
      points = points + 1
      love.audio.play(music)
    end
  elseif key == "down" then
    if testMap (0, 1) then
      player.grid_y = player.grid_y + 32
      points = points + 1
      love.audio.play(music)

    end
  elseif key == "left" then
    if testMap (-1, 0) then
      player.grid_x = player.grid_x - 32
      points = points + 1
      love.audio.play(music)

    end
  elseif key == "right" then
    if testMap (1, 0) then
      player.grid_x = player.grid_x + 32
      points = points + 1
      love.audio.play(music)

    end
  end
end

My struggle is that as soon as the character (the little box) leaves the "map", this error happens: Error.

I want to show some screen that means the level is complete when it gets out of the maze but I have little to no idea how to solve this problem.

Upvotes: 0

Views: 71

Answers (2)

user7003504
user7003504

Reputation:

When manipulating or reading from array, you should check each lookup step if it exists, else you get error when trying to access something that doesnt. so for map[a][b] you check if map[a] ~= nil and map[a][b] ~= nil then map tile is declared. If you try to access map[a][b] while map or map[a] is not declared, then you get an error trying to access something that doesnt exist. This applies on creating, reading and changing arrays and their contents.

example:

add new arrays to map: if map[a] ~= nil then map[a][b] = {}; else map[a] = {}; map[a][b] = {}; end --here you check if map[a] exists first before declaring map[a][b] and create in case it doesnt, as Love2d will try to access map then map[a] first and then create [b] inside. If it fails to find map or map[a], instead of creating new arrays to insert to, it will result in error.

look for values on map: if map[a] ~= nil then result = map[a][b]; else result = "map[a] doesnt exist so doesnt map[a][b]"; end --if you try to look for map[a][b], Love2d will try to access map, then [a] inside map, then it will look what is inside [b]. if it fails to find map or map[a] before reaching map[a][b], instead of retrieving nil it will result in error.

Upvotes: 0

Gage Hendy Ya Boy
Gage Hendy Ya Boy

Reputation: 1564

The issue is on the line seen_tiles[(player.grid_y / 32) + y][(player.grid_x / 32) + x] = 1

You are trying to change an array item that doesn't exist because you are off of the grid.

Try preventing the player from moving off the grid in the first place, or making the code I referenced above into a function, and checking if the item in the array exists before doing anything.

Upvotes: 1

Related Questions