Tetris Timing Problem(俄罗斯方块计时问题)
本文介绍了俄罗斯方块计时问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用PyGame编写俄罗斯方块程序,遇到了一个有趣的问题。
在我提出问题之前,以下是伪代码:
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秒移动一次。我希望它能让挡路以人能按下的速度移动,同时挡路每0.4秒就掉一次。我如何安排代码才能做到这一点呢?谢谢!
推荐答案
我在这里看到的主要问题是您使用0.4秒的等待来限制帧速率。
您不应该限制帧率,而应该限制您的挡路的下落速度。
如果我没记错的话,有一个公式可以用来做到这一点。它是基于自上一帧以来经过的时间长度。它看起来像:
fraction of a second elapsed since last frame * distance you want your block to move in a second
这样,您可以保持您的主循环完好无损,并且移动处理将在每一帧进行。
这篇关于俄罗斯方块计时问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:俄罗斯方块计时问题
基础教程推荐
猜你喜欢
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
