如何在人工智能反应中集成计算时间?

我的程序是 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 计算所需的时间,但我应该使用它进行什么操作以规范化挡板的移动。

点赞
用户127839
用户127839

从你的代码看,挡板速度是恒定的; 可以考虑将其替换为一个函数,从而初始时挡板移动得更慢,然后加速,最后在到达所需位置时再次减速。

在运动行为上,这将使其更加逼真,并且它也可能解决你的卡顿问题。

你的恒定PADDLE_SPEED需要被一些函数替换,该函数在适当的时间步骤中返回一个值(可能介于0.0和PADDLE_SPEED之间)。

2020-04-26 14:48:03