【问题标题】:Tetris Timing Problem俄罗斯方块时间问题
【发布时间】:2011-07-30 22:22:23
【问题描述】:

我正在用 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 秒掉落一次。我如何安排代码以便它可以做到这一点?谢谢!

【问题讨论】:

  • 您可能需要使用多线程。欢迎来到一个痛苦的世界:(但也许 PyGame 有一些内置的功能或其他东西。那会更容易。
  • 啊哈!我有一种预感,它与类似的事情有关。不过我从来没有读过它,带来痛苦!另一个问题:并发、多线程和并行编程。它们是一样的吗?
  • @John:有点。请记住,它们的含义略有不同,并且偶尔会有所不同。但大多数情况下,这些术语是可以互换的。例如,多线程有点过于具体:它意味着线程并且意味着多个。并行编程也不是必需的。
  • 但是不,你不需要多线程,正如给出的答案所示。

标签: python pygame timing tetris


【解决方案1】:

在玩游戏时,你应该总是尝试做这样的事情:

while not finished:
    events = get_events() # get the user input
    # update the world based on the time that elapsed and the events
    world.update(events, dt) 
    word.draw() # render the world
    sleep(1/30s) # go to next frame

睡眠时间应该是可变的,因此它考虑了绘制和计算世界更新所花费的时间。

世界更新方法看起来像这样:

def update(self, events, dt):
    self.move(events) # interpret user action
    self.elapsed += dt
    if self.elapsed > ADVANCE_TIME:
        self.piece.advance()
        self.elapsed = 0

实现这一点的另一种方法(因此您不会重绘太多)是在用户订购要移动的一块或 ADVANCE_TIME 时间过去时触发事件。然后在每个事件处理程序中更新世界并重绘。

这是假设您希望碎片一次移动一个步骤而不是连续移动。无论如何,连续运动的变化是微不足道的。

【讨论】:

    【解决方案2】:

    您可以尝试询问gamedev.stackexchange.com。查看网站上的 Game Loops,并查看其他示例 pygame 项目,看看他们是如何做到的。拥有良好的游戏循环至关重要,它会为您处理好诸如用户输入和一致的帧速率之类的事情。

    编辑:https://gamedev.stackexchange.com/questions/651/tips-for-writing-the-main-game-loop

    【讨论】:

      【解决方案3】:

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

      您不应该限制帧速率,而是应该限制方块下降的速度。

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

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

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

      【讨论】:

        【解决方案4】:

        你也可以...

            ...
            # This part of the code makes the piece fall by itself
            if the block isn't touching the floor and 
               the block hasn't automatically moved in the last 0.4 seconds:
                move block down one step
            ...
        

        请注意,如果用户没有敲击任何键,您将进行大量轮询。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-17
          • 1970-01-01
          相关资源
          最近更新 更多