【问题标题】:Python Pygame Loop Unusual delayPython Pygame 循环异常延迟
【发布时间】:2014-05-16 05:10:08
【问题描述】:

我似乎真的卡在我的程序中的某个点上,希望你能提供帮助。

我基本上是在尝试创建一个简单的热门运动图像游戏。图像被随机放置在一系列“向上点”中的一个,然后玩家需要在图像被放置到新位置之前击中图像。

一切似乎都很好,但我似乎遇到了 pygame.tick 或 python time.sleep() 的奇怪延迟。

我只想说我是一个 python 和 pygame 新手,但我一生都无法理解出了什么问题。到目前为止,根据我的理解,图像应该以 1 秒的间隔移动到新位置。

但有时程序似乎“挂起”或“延迟”,图像似乎卡在原地几秒钟,然后来回奔波,好像试图赶上,然后程序似乎可以工作几秒钟内似乎没问题(通常延迟 1 秒),然后回到努力移动或跟上。

希望这是有道理的 :) 她的代码是带有循环的,所以你可以看到我认为我有问题的地方:

# The values are tuples which hold the top-left x, y co-ordinates at which the  
# rabbit image is to be blitted...

uppoints = {1: (70, 70), 2: (250, 70), 3: (430, 70), 4: (600, 70),
        5: (70, 250), 6: (250, 250), 7: (430, 250), 8: (600, 250),
        9: (70, 430), 10: (250, 430), 11: (430, 430), 12: (600, 430)}


# Get a random location to pop up...
def getpos(ups):
    x = random.randint(1, 2)
    if x == 1:
        up = ups[1]
        return up
    elif x == 2:
        up = ups[2]
        return up

uppos = ()

while gamerunning:
    uppos = getpos(uppoints)

        for event in pygame.event.get():
            if event.type == QUIT:
            gamerunning = False

            if event.type == pygame.MOUSEMOTION:
                mpos = pygame.mouse.get_pos()

            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    if rabbits.rect.collidepoint(event.pos):
                        score += 10

    mainwin.fill((0, 0, 0))
    scoretxt2 = basefont.render(str(score), True, (0, 0, 0))
    mainwin.blit(mainback, (0, 0))
    mainwin.blit(rabbits.image, uppos)
    mainwin.blit(scoretxt, (70, 30))
    mainwin.blit(scoretxt2, (160, 32))
    mainwin.blit(livestxt, (600, 30))
    mainwin.blit(livestxt2, (690, 32))

    pygame.display.flip()

    time.sleep(1)

    # Check which level the user is by score, and increase the speed of the rabbits  as needed...
    fpsclock.tick(60)

如果我使用 time.sleep,游戏会以 1 秒间隔的正确速度运行。如果我省略 time.sleep() 并使用 .tick(60),图像会疯狂闪烁,但我确信我仍然可以看到某种延迟。

我试着用谷歌搜索了一下,发现有几页说 pygame 的 .tick() 方法和 python sleep() 方法都有问题,但无法确定这是不是真的。

我真的不知道我在这里做错了什么,所以希望你能帮忙:)

非常感谢!

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    您的time.sleepfpsclock.tick 调用有些相互矛盾。对sleep 的调用告诉Python 停止整整一秒钟,什么也不做。另一方面,tick 调用表示您希望以每秒 60 帧的速度运行。

    我怀疑你不想使用sleep。当程序处于休眠状态时,不会处理 IO 事件并且程序会出现挂起。相反,您需要更改代码的其余部分,使其在每秒更新多次时能够正常运行。

    我认为您可能希望将调用getpos 的位置更改为每秒只调用一次。我建议类似:

    ms = 0
    
    while gamerunning:
        ms += fpsclock.get_time()
        if ms > 1000: # 1000 ms is one second
            ms -= 1000
            uppos = getpos(uppoints)
    
        # ...
    

    【讨论】:

    • 您好,谢谢您的回复,我明白您所说的 2 会在哪里发生冲突,感谢您的帮助。在尝试 if ms.. 语句时,我认为它的评估为 False,因为我得到无法 blit 到该位置(upos)的错误,所以我认为它是一个空元组。如果我添加一个 else 子句,问题仍然存在......
    • @user3069267:啊,它可能在第一遍就被窃听了。尝试在开始循环之前初始化uppos,或者在1000 开始ms,以便它在第一帧上运行更新。
    • 太棒了!非常感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多