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
这样,您可以保持您的主循环完好无损,并且移动处理将在每一帧进行。
这篇关于俄罗斯方块计时问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:俄罗斯方块计时问题
基础教程推荐
猜你喜欢
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 包装空间模型 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
