图像在 pygame 中移动(使 pygame 崩溃)

问题描述 投票:0回答:2

我正在尝试制作一个游戏,当这个循环运行时,它会导致 pygame 崩溃

while poterbar_hunt_pone == True and ptwo_shot == True:

            if poterbarx < xone+carone_width:
                poterbar_dir = "right"
                poterbarx_c = 20
            else:
                poterbar_dir = "left"
                poterbarx_c = -20

            if poterbary+poterbarh < yone:
                poterbary_c = 20
            else:
                poterbary_c = -20

            if poterbarx <= xone+carone_width and poterbarx+poterbarw >= xone:

                if poterbary+poterbarh >= yone and poterbary <= yone+carone_height:
                    xone_change = random.randrange (-20, 20)
                    yone_change = random.randrange (-20, 20)
                    poterbar_hunt_pone = False
                    ptwo_shot = False
                    poterbarx = -100
                    poterbary = -100

当两个值都是 True 时,pygame 崩溃,我认为是在 poterbar 的运动中。 如果你们中有人知道导致 pygame 崩溃的原因,请告诉我

python while-loop pygame
2个回答
2
投票

你失踪了

pygame.display.update()

将其添加到代码的最底部,所以现在看起来像:

while poterbar_hunt_pone == True and ptwo_shot == True:

            if poterbarx < xone+carone_width:
                poterbar_dir = "right"
                poterbarx_c = 20
            else:
                poterbar_dir = "left"
                poterbarx_c = -20

            if poterbary+poterbarh < yone:
                poterbary_c = 20
            else:
                poterbary_c = -20

            if poterbarx <= xone+carone_width and poterbarx+poterbarw >= xone:

                if poterbary+poterbarh >= yone and poterbary <= yone+carone_height:
                    xone_change = random.randrange (-20, 20)
                    yone_change = random.randrange (-20, 20)
                    poterbar_hunt_pone = False
                    ptwo_shot = False
                    poterbarx = -100
                    poterbary = -100
    pygame.display.update() # You have to update display

0
投票
while poterbar_hunt_pone and ptwo_shot:
    if poterbarx < xone+carone_width:
        poterbar_dir = "right"
        poterbarx_c = 20

    else:

        poterbar_dir = "left"
        poterbarx_c = -20

    if poterbary+poterbarh < yone:
        poterbary_c = 20

    else:
        poterbary_c = -20

    if poterbarx <= xone+carone_width and poterbarx+poterbarw >= xone:
        if poterbary+poterbarh >= yone and poterbary <= yone+carone_height:
            xone_change = random.randrange (-20, 20)
            yone_change = random.randrange (-20, 20)
            poterbar_hunt_pone = False
            ptwo_shot = False
            poterbarx = -100
            poterbary = -100

“崩溃”是什么意思? Windows“程序没有应答”?所以你不要使用“pygame.event.get()”;当 Windows 没有太多时间询问事件时,程序会“崩溃”。

或者你的意思是“崩溃”是“不写任何东西”?所以你不要使用“pygame.display.update()”,它用于更新屏幕。

你应该写这样的东西:

while poterbar_hunt_pone and ptwo_shot:
    ##################
    pygame.event.get()
    ##################

    if poterbarx < xone+carone_width:
        poterbar_dir = "right"
        poterbarx_c = 20

    else:

        poterbar_dir = "left"
        poterbarx_c = -20

    if poterbary+poterbarh < yone:
        poterbary_c = 20

    else:
        poterbary_c = -20

    if poterbarx <= xone+carone_width and poterbarx+poterbarw >= xone:
        if poterbary+poterbarh >= yone and poterbary <= yone+carone_height:
            xone_change = random.randrange (-20, 20)
            yone_change = random.randrange (-20, 20)
            poterbar_hunt_pone = False
            ptwo_shot = False
            poterbarx = -100
            poterbary = -100

    #######################
    pygame.display.update()
    #######################
© www.soinside.com 2019 - 2024. All rights reserved.