Lua 获取游戏中玩家的 (x,y)

Lua getting (x,y) of player in game

我想实时找到我的玩家的 x 和 y 坐标,这样我就知道在哪里制作我的游戏的下一关。我目前正在使用 LÖVE 2D 运行 我的代码。当我尝试打印 player.xplayer.y 时,游戏 运行 没问题,但没有坐标的文本输出。我试图更改文本所在的位置,但这不起作用。任何帮助表示赞赏。注意:我今天刚开始 Lua 所以请直言不讳。 :)

love.graphics.setDefaultFilter('nearest','nearest')
function love.load()
  room1Image = love.graphics.newImage('room1.png')
  room2Image = love.graphics.newImage('room2.png')
  room3Image = love.graphics.newImage('room3.png')
  room1 = true
  room2 = false
  room3 = false
  player = {}
  player.x = 0
  player.y = 255
  player.speed = 5
  player.image = love.graphics.newImage('player.png')
end

function love.update(dt)
  if love.keyboard.isDown("left") then
    player.x = player.x - 5
  end
  if love.keyboard.isDown("right") then
    player.x = player.x + 5
  end
  if love.keyboard.isDown("up") then
    player.y = player.y - 5
  end
  if love.keyboard.isDown("down") then
    player.y = player.y + 5
  end
  if player.y >= 600 and room1 then
    room1 = false
    room2 = true
    player.y = 5
  end
  if player.y <= 0 and room2 then
    room1 = true
    room2 = false
    player.y = 600
  end
  if player.y >= 600 and room2 then
    room2 = false
    room3 = true
    player.y = 5
  end
  if player.y <= 0 and room3 then
    room2 = true
    room3 = false
    player.y = 600
  end


end

function love.draw()
  --draw background
  if room1 then
    love.graphics.draw(room1Image, room1Image.x, room1Image.y)
  elseif room2 then
    love.graphics.draw(room2Image, room2Image.x, room2Image.y)
  elseif room3 then
    love.graphics.draw(room3Image, room3Image.x, room3Image.y)
  end
  --draw player
  love.graphics.draw(player.image, player.x, player.y, 0, 5)
  end

如果你想输出一些东西到控制台使用print()。这在游戏中是不可见的 window.

如果你想向玩家显示一些文字(在游戏中)调用 love.graphics.print inside love.draw():

local x,y = 0, 0 --coordinates at which the text is printed
function love.load()
end

function love.update(dt)
end

function love.draw()
  love.graphics.print("This is something I want you to see.", x, y)
end