Lua Love2d玩家问题

Lua Love2d玩家问题,lua,love2d,Lua,Love2d,我是新来的,我已经为我可能说的任何错误道歉了。 我看过《僵局玩家》教程,我想根据别人的情况做一些类似的事情。这是一个看不见的迷宫,当你触摸瓷砖时,它会画出瓷砖 local points = 0 width = love.graphics.getWidth() height = love.graphics.getHeight() function love.load() picture = love.graphics.newImage( "candy.png") back = l

我是新来的,我已经为我可能说的任何错误道歉了。 我看过《僵局玩家》教程,我想根据别人的情况做一些类似的事情。这是一个看不见的迷宫,当你触摸瓷砖时,它会画出瓷砖

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
我的挣扎是,一旦角色(小盒子)离开“地图”,就会发生以下错误:


我想显示一些屏幕,这意味着水平是完整的,当它走出迷宫,但我几乎不知道如何解决这个问题

问题出现在
seed_tiles[(player.grid_y/32)+y][(player.grid_x/32)+x]=1

您正在尝试更改不存在的数组项,因为您不在网格中


首先尝试阻止播放器移出网格,或者将我上面引用的代码变成一个函数,并在执行任何操作之前检查数组中的项是否存在。

当操作或读取数组时,应检查每个查找步骤是否存在,否则,当您尝试访问一些无法访问的内容时,会出现错误。 因此,对于
map[a][b]
您检查
map[a]~=nil和map[a][b]~=nil,然后声明map tile。如果在未声明
map
map[a]
时尝试访问
map[a][b]
,则尝试访问不存在的内容时会出错。 这适用于创建、读取和更改数组及其内容

例如:

向映射添加新数组:
如果映射[a]~=nil,则映射[a][b]={};else-map[a]={};map[a][b]={};结束
——在这里,在声明
map[a][b]
之前,首先检查
map[a]
是否存在,如果不存在,则进行创建,因为Love2d将尝试访问
map
,然后先访问
map[a]
,然后在内部创建
[b]
。 如果它找不到
map
map[a]
,而不是创建要插入的新数组,则会导致错误


在map上查找值:
如果map[a]~=nil,那么result=map[a][b];else result=“映射[a]不存在,因此不映射[a][b]”;结束
——如果您尝试查找
map[a][b]
,Love2d将尝试访问
map
,然后
[a]
在map内部,然后它将查找
[b]
内部的内容。如果在到达
map[a][b]
之前未找到
map
map[a]
,如果不检索
nil
,则会导致错误。

请将代码粘贴到问题中并正确设置格式。请将代码作为文本粘贴到问题中,然后突出显示并按Ctrl+K。这允许我们将代码复制并粘贴到IDE中,并帮助识别问题。请阅读以了解您需要包含哪些代码更正感谢。可能,
map[(player.grid\u y/32)+y]
nil