lua - 增量时间和低估(我不了解增量时间)
问题描述
嘿,你们在用 delta 时间理解这些问题时遇到了问题。它在 Pong-3 当然是 CS50 - 游戏开发简介。我不明白为什么 dt 在那里。dt 是所有代码中都没有提到的变量。BUt 在语言 LUA 的渲染过程中被调用。它就像一个局部变量,但不是预定义的或引入该变量的另一种方式。我会给出代码并等待......一些答案。
--[[
GD50 2018
Pong Remake
pong-3
"The Paddle Update"
-- Main Program --
Author: Colton Ogden
cogden@cs50.harvard.edu
Originally programmed by Atari in 1972. Features two
paddles, controlled by players, with the goal of getting
the ball past your opponent's edge. First to 10 points wins.
This version is built to more closely resemble the NES than
the original Pong machines or the Atari 2600 in terms of
resolution, though in widescreen (16:9) so it looks nicer on
modern systems.
]]
-- push is a library that will allow us to draw our game at a virtual
-- resolution, instead of however large our window is; used to provide
-- a more retro aesthetic
--
-- https://github.com/Ulydev/push
push = require 'push'
WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720
VIRTUAL_WIDTH = 432
VIRTUAL_HEIGHT = 243
-- speed at which we will move our paddle; multiplied by dt in update
PADDLE_SPEED = 200
--[[
Runs when the game first starts up, only once; used to initialize the game.
]]
function love.load()
love.graphics.setDefaultFilter('nearest', 'nearest')
-- more "retro-looking" font object we can use for any text
smallFont = love.graphics.newFont('font.ttf', 8)
-- larger font for drawing the score on the screen
scoreFont = love.graphics.newFont('font.ttf', 32)
-- set LÖVE2D's active font to the smallFont obect
love.graphics.setFont(smallFont)
-- initialize window with virtual resolution
push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
fullscreen = false,
resizable = false,
vsync = true
})
-- initialize score variables, used for rendering on the screen and keeping
-- track of the winner
player1Score = 0
player2Score = 0
-- paddle positions on the Y axis (they can only move up or down)
player1Y = 30
player2Y = VIRTUAL_HEIGHT - 50
end
--[[
Runs every frame, with "dt" passed in, our delta in seconds
since the last frame, which LÖVE2D supplies us.
]]
function love.update(dt)
-- player 1 movement
if love.keyboard.isDown('w') then
-- add negative paddle speed to current Y scaled by deltaTime
player1Y = player1Y + -PADDLE_SPEED * dt
elseif love.keyboard.isDown('s') then
-- add positive paddle speed to current Y scaled by deltaTime
player1Y = player1Y + PADDLE_SPEED * dt
end
-- player 2 movement
if love.keyboard.isDown('up') then
-- add negative paddle speed to current Y scaled by deltaTime
player2Y = player2Y + -PADDLE_SPEED * dt
elseif love.keyboard.isDown('down') then
-- add positive paddle speed to current Y scaled by deltaTime
player2Y = player2Y + PADDLE_SPEED * dt
end
end
--[[
Keyboard handling, called by LÖVE2D each frame;
passes in the key we pressed so we can access.
]]
function love.keypressed(key)
-- keys can be accessed by string name
if key == 'escape' then
-- function LÖVE gives us to terminate application
love.event.quit()
end
end
--[[
Called after update by LÖVE2D, used to draw anything to the screen,
updated or otherwise.
]]
function love.draw()
-- begin rendering at virtual resolution
push:apply('start')
-- clear the screen with a specific color; in this case, a color similar
-- to some versions of the original Pong
love.graphics.clear(40/255, 45/255, 52/255, 255/255)
-- draw welcome text toward the top of the screen
love.graphics.setFont(smallFont)
love.graphics.printf('Hello Pong!', 0, 20, VIRTUAL_WIDTH, 'center')
-- draw score on the left and right center of the screen
-- need to switch font to draw before actually printing
love.graphics.setFont(scoreFont)
love.graphics.print(tostring(player1Score), VIRTUAL_WIDTH / 2 - 50,
VIRTUAL_HEIGHT / 3)
love.graphics.print(tostring(player2Score), VIRTUAL_WIDTH / 2 + 30,
VIRTUAL_HEIGHT / 3)
-- render first paddle (left side), now using the players' Y variable
love.graphics.rectangle('fill', 10, player1Y, 5, 20)
-- render second paddle (right side)
love.graphics.rectangle('fill', VIRTUAL_WIDTH - 10, player2Y, 5, 20)
-- render ball (center)
love.graphics.rectangle('fill', VIRTUAL_WIDTH / 2 - 2, VIRTUAL_HEIGHT / 2 - 2, 4, 4)
-- end rendering at virtual resolution
push:apply('end')
end
解决方案
Loveupdate
文档简要地提到了增量时间。
dt
(Delta Time) 保存自上次update
调用函数以来的时间,以秒为单位。在正常情况下,这应该是几分之一秒。作为游戏循环的一部分, Love 框架update
会反复为您调用该函数。它还在框架内的某处定义并跟踪dt
变量,这就是为什么您看不到它在任何地方定义或引入的原因。您只能从更新功能中访问它(除非您将它传递到其他地方)。
为什么需要增量时间?
因为你不能保证update
总是以一致的速率被调用。想象一下,您有一个玩家角色,每次update
调用该角色时您移动 1 个像素。如果您的游戏以 60fps 运行,那么您的角色在 1 秒内移动 60px。但是如果你的游戏速度下降到 59、50 或 12fps,那么你的角色也会变慢,这通常不是你想要的。
您可以使用增量时间来为您的角色保持一致的速度。将所需的每秒速度 (60px) 乘以dt
每帧的准确移动量,无论update
调用的频率如何。
这就是您的代码在这样的地方发生的事情:
player2Y = player2Y + PADDLE_SPEED * dt
PADDLE_SPEED
是每秒移动的量乘以自上次更新以来的秒数。结果被添加到玩家的位置。
推荐阅读
- java - 为什么在使用 forEachOrdered 的静态初始化程序块中使用 lambda 进行并行流处理会产生死锁,而使用 forEach 不会产生死锁?
- swift - CoreML 是否可以与 swift 包管理器一起使用?
- ios - 检查单元格是否完全显示在collectionview中
- php - laravel-graphql php artisan 命令'无法打开输入文件:make:graphql:type'
- java - Java登录系统不工作;读取第一个用户,但无法解析第二个或下一个用户
- ionic-framework - 将 Ionic 中的标签名称更改为居中文本后如何重新加载标签页?
- php - 如果数字已被猜到,如何将信息从两个会话变量发送到文本文件
- azure - Service Fabric 可靠的参与者和 I/O 操作
- list - 如何在 prolog 中打印出我的事实列表?
- design-patterns - 具有指定要运行的任务类型的字段的对象是否有设计模式?