首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在AI反应中整合计算时间?

如何在AI反应中整合计算时间?
EN

Stack Overflow用户
提问于 2020-04-26 16:29:00
回答 1查看 72关注 0票数 0

我的程序是Pong的一个实现,其中一个球拍由计算机移动,另一个由用户移动。

该程序可以很好地处理AI为实现现实主义而产生的错误。然而,我的球拍在屏幕上的移动是卡顿的,它似乎跳过了一到两帧。

程序在Lua(+Love2D)中。

代码语言:javascript
运行
复制
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计算所需的时间,但是我应该用它来做什么操作,以使我的球拍运动标准化。

EN

回答 1

Stack Overflow用户

发布于 2020-04-26 22:48:03

从您的代码中看,划板速度似乎是恒定的;将其替换为一个函数可能是一个好主意,这样一开始划板的移动速度会更慢,然后加速,然后在到达所需位置时再次减速。

这将使它在运动行为方面更现实,也可能解决你的口吃问题。

您的常量PADDLE_SPEED需要替换为某个函数,该函数执行一步操作并在适当的时间返回一个值(可能介于0.0和PADDLE_SPEED之间)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61437849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档