我的程序是Pong的一个实现,其中一个球拍由计算机移动,另一个由用户移动。
该程序可以很好地处理AI为实现现实主义而产生的错误。然而,我的球拍在屏幕上的移动是卡顿的,它似乎跳过了一到两帧。
程序在Lua(+Love2D)中。
function Paddle:comp_move(dt)
error = math.random(3) == 2 and true or false
start_time = os.time()
diff = 0
if ball:collides(self) == false then
if ball.y > self.y + self.height then -- Ball is below paddle
-- Ball is moving up and difference is more than 20 pixels
if ball.dy < 0 and (ball.y - self.y - self.height) > 20 then
-- move down
if error == false then
diff = os.difftime(os.time() - start_time)
self.y = math.min(VIRTUAL_HEIGHT - 20, self.y + PADDLE_SPEED*(dt))
end
end
-- Ball is moving down
if ball.dy > 0 then
-- move down
if error == false then
diff = os.difftime(os.time() - start_time)
self.y = math.min(VIRTUAL_HEIGHT - 20, self.y + PADDLE_SPEED*(dt))
end
end
elseif ball.y + ball.height < self.y then -- Ball is above paddle
-- Ball is moving down
if ball.dy > 0 and (self.y - ball.y - ball.height) > 20 then
-- move up
if error == false then
diff = os.difftime(os.time() - start_time)
self.y = math.max(0,self.y - PADDLE_SPEED*(dt))
end
elseif ball.dy < 0 then -- Ball is moving up
-- move up
if error == false then
diff = os.difftime(os.time() - start_time)
self.y = math.max(0,self.y - PADDLE_SPEED*(dt))
end
end
end
end我正在计算PC计算所需的时间,但是我应该用它来做什么操作,以使我的球拍运动标准化。
发布于 2020-04-26 22:48:03
从您的代码中看,划板速度似乎是恒定的;将其替换为一个函数可能是一个好主意,这样一开始划板的移动速度会更慢,然后加速,然后在到达所需位置时再次减速。
这将使它在运动行为方面更现实,也可能解决你的口吃问题。
您的常量PADDLE_SPEED需要替换为某个函数,该函数执行一步操作并在适当的时间返回一个值(可能介于0.0和PADDLE_SPEED之间)。
https://stackoverflow.com/questions/61437849
复制相似问题