【发布时间】: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