python - 俄罗斯方 block 计时问题

标签 python pygame timing tetris

我正在用 PyGame 编写俄罗斯方 block 程序,遇到了一个有趣的问题。

在我提问之前,这是伪代码:

while True:
    # In this part, the human controls the block to go left, right, or speed down
    if a key is pressed and the block isnt touching the floor:
        if the key is K-left:
            move piece left one step
        if the key is K-right:
            move piece right one step
        if the key is K-down:
            move piece down one step


    # This part of the code makes the piece fall by itself
    if the block isnt touching the floor:
        move block down one step

    # This part makes the while loop wait 0.4 seconds so that the block does not move
    # down so quickly
    wait 0.4 seconds

问题在于,由于代码的“等待 0.4 秒”部分,人为控制的部分只能每 0.4 秒移动一次。我希望方 block 移动的速度与人类按下键的速度一样快,同时,方 block 每 0.4 秒掉落一次。我如何安排代码才能做到这一点?谢谢!

最佳答案

我在这里看到的主要问题是您使用 0.4 秒的等待时间限制了帧率。

您不应该限制帧率,而应该限制方 block 下落的速度。

如果我没记错的话,您可以使用一个公式来做到这一点。它基于自上一帧以来耗时量。它看起来像:

fraction of a second elapsed since last frame * distance you want your block to move in a second

这样,您可以保持主循环完好无损,移动处理将在每一帧发生。

关于python - 俄罗斯方 block 计时问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5568654/

相关文章:

python - 如何使用 %%timeit 单元格魔法并排除设置代码?

python - 使用边界框列表从图像中裁剪多个边界框

python - 如何在 matplotlib 中的每个子图上嵌入图像?

python - 玩家被困在墙壁pygame中

python - 带有鼠标事件的 Pygame blit 图像

sql - 针对哈希 token 的 SQL SELECT 定时攻击的对策

python - 导入错误 : No module named 'cv2' Python3

python - 文件名末尾的 '~' 在 Python MVC 中是什么意思

python - 我如何实现图片而不是我的红色矩形?

python - 我的功能需要负时间才能完成。到底发生了什么事?