【发布时间】:2021-08-11 18:07:32
【问题描述】:
嘿,你们在理解这些增量时间问题时遇到了问题。它在 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
【问题讨论】:
-
如果从那里启动 LÖVE main.lua,只需在
update(dt)中执行print(dt)即可在控制台中查看“每帧秒数”。 - 这里有点像0.017025980995918。