【问题标题】:Love2d countdown and stopwatchLove2d 倒计时和秒表
【发布时间】:2022-01-08 01:54:53
【问题描述】:

正如标题所示,我想创建一个 love2d 倒计时和秒表。我之前尝试过这样做:

  1. 倒计时:
function love.load()
   timer = 3
end

function love.update(dt)
   if timer >= 0 then
      timer = timer - dt
   else
      printMsg = true
   end
end

function love.draw()
   if printMsg == true then
      love.graphics.print('time over')
   end
end
  1. 秒表:
function love.load()
   timer = 0
   lives = 3
end

function love.update(dt)
   if lives <= 0 then
      timer = timer + dt
   else
      printMsg = true
   end
end

function love.draw()
   if printMsg == true then
      love.graphics.print('you have survived for '..timer..' seconds')
   end
end

function love.mousepressed(x, y, button)
   if button == 1 then  --if left mouse button is pressed then 1 is subtracted from lives
      lives = lives - 1
   end
end

但问题是dt 不是一个常数,因此定时器的增加或减少甚至不会导致倒计时或秒表值更快或更慢(主要是更快)。

我也尝试使用love.timer.getTime(),但它仍然与dt 相同。非常感谢任何帮助。提前感谢任何答案。

【问题讨论】:

  • 注意:它的速度非常快。只有在后台运行密集型软件时才会变慢。
  • love.graphics 是一个库,而不是回调。你的意思是love.draw
  • 我的错,现在改正了
  • 我不明白你的问题。请详细说明为什么您认为不能使用 dt 进行计时。这是它的唯一目的。你在那里打印的time 值是什么?它来自哪里?

标签: lua love2d


【解决方案1】:

使用os.date() 生成的格式化时间字符串作为您的生命时间或倒计时显示...

function love.draw()
   if printMsg == true then
      love.graphics.print('you have survived for ' .. os.date('%H:%M:%S', time - 3600) .. ' seconds')
   end
end

...或...

function love.draw()
   if printMsg == true then
      love.graphics.print(os.date('you have survived for %H:%M:%S seconds', time - 3600):upper())
   end
end

计算开始时使用的生命周期(产卵/重生)...

time = os.time() -- get the seconds of epoch (linux time)

...并计算死亡时间( printMsg = true )...

time = os.time() - time -- Calc the life time

【讨论】:

    猜你喜欢
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多