俄罗斯方块计时问题 [英] Tetris Timing Problem

查看:14
本文介绍了俄罗斯方块计时问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这样,您可以保持您的主循环完好无损,并且移动处理将在每一帧进行。

这篇关于俄罗斯方块计时问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆