我正在用 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/