启动game_loop()后Pygame窗口冻结

时间:2018-08-27 17:17:33

标签: python pygame

嘿,它最大了,我遇到问题了,我的game_loop()运行了大约半秒钟,然后停滞了,这并不是说没有响应,或者只是完全不响应所有按钮,任何人都知道我做了什么错误?我正在定义game_intro(),并说如果我单击Enter [RETURN],则开始game_loop()intro=False

任何帮助:)

def game_loop():
global bot_x1
global bot_x2
global bot_y1
global bot_y2
global x
global y  
global running
pygame.display.update()
clock.tick(60)
pygame.display.update()

if running:
    pygame.display.update()
    pygame.display.update()

    for event in pygame.event.get():
            #CLOSE
            if event.type==pygame.QUIT:
                pygame.quit()
                pygame.quit()
                quit()

                  #MOVEMENT

##                if pygame.key.get_pressed()[pygame.K_LEFT] and x > 0:
##                    x-= selfspeed
##                     
##                if keys [pygame.K_RIGHT] and x < 710 :
##                    x+= selfspeed
##
##                if keys [pygame.K_UP] and y > 0:
##                    y-= selfspeed
##
##                if keys [pygame.K_DOWN] and y < 400:
##                    y+= selfspeed

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT and x > 0:
                    x-=selfspeed
                elif event.key == pygame.K_RIGHT and x < 710 :
                    x+=selfspeed
                elif event.key == pygame.K_UP and y > 0:
                    y-=selfspeed
                elif event.key == pygame.K_DOWN and y < 400 :
                    y+=selfspeed

#BOT MOVEMENT
            if bot_y2 > window_height:
                bot_y2 = 0
                bot_x2 = random.uniform(50,750) 

            if bot_y1 > window_height:
                bot_y1 = 0
                bot_x1 = random.uniform(50,750)                    

#SHOW PICTURES
    bot_y1+=botspeed
    bot_y2+=botspeed             
    window.fill(grey)
    bot_1(bot_x1,bot_y1)
    bot_2(bot_x2,bot_y2)
    plane(x,y)
    print("working")
    running=True

#UPDATE GAME DISPLAY+FPS
    keys=pygame.key.get_pressed()
    pygame.display.flip()
    clock.tick(30)

#GAME LOOP BROKEN CLOSE
game_intro()
game_loop()

1 个答案:

答案 0 :(得分:1)

我的代表还不够高,无法发表评论,但是有很多细节您还不够清楚,例如game_intro()函数的作用或冻结后窗口是否关闭。您还会在解释中引用一个从未在代码(intro = False)中显示的变量。但是根据您提供的代码,您似乎从未将game_loop()置于实际循环中。代替...

...
if running:
     pygame.display.update()
...

在您的game_loop()内尝试

...
while running:
     pygame.display.update()
....

如果仍然无法解决问题,请尝试提供带有更新问题或粘贴链接的更多代码。