首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >pygame主游戏函数在if语句后冻结

pygame主游戏函数在if语句后冻结
EN

Stack Overflow用户
提问于 2020-10-17 02:31:52
回答 1查看 34关注 0票数 0

正如标题中所说,在我选择一个按钮后,将代码移动到第二个if语句上,整个屏幕就会冻结并停止响应。我在努力寻找问题是什么,有什么洞察力可以导致冻结吗?我几乎可以肯定,问题不在于我没有包含的函数,因为它们都非常简单,不应该改变任何可能冻结代码的东西,尽管如果你认为它们可能是问题所在,我会在评论中添加它们。

代码语言:javascript
运行
复制
def game():
        player_health = 400
        running = True
        turn = 1
        stopmusic()
        pmusic("assets\\music\\battlemusic.mp3")
        monster_health()
        move_complete = False
        monster_move = False
        while player_health > 0:
                if turn == 1:
                        while move_complete == False:
                                try:
                                        screen.fill((0, 0, 0))
                                        sewer = pygame.image.load('assets\\backgrounds\\sewer.png')
                                        screen.blit(sewer,(0,0))
                                        mx, my = pygame.mouse.get_pos()
                                        mob()

                                        button_attack = pygame.Rect(150, 500, 200, 50)
                                        button_potion = pygame.Rect(150, 600, 200, 50)
                                        button_spell = pygame.Rect(450, 500, 200, 50)
                                        button_suicide = pygame.Rect(450, 600, 200, 50)
                                        healthbar_outline = pygame.Rect(45,45, 1190, 60)

                                        healthbar = pygame.Rect(50,50, (how_much_health_bar), 50)


                                        combat_log = pygame.Rect(680, 150, 500, 450)

                                        click = False

                                        for event in pygame.event.get():
                                                # this will make it so that you can press escape to exit this funciton
                                                if event.type == QUIT:
                                                        pygame.quit()
                                                        sys.exit()
                                                if event.type == KEYDOWN:
                                                        stopmusic()
                                                        pmusic("assets\\music\\backgroundmusic.mp3")
                                                        if event.key == K_ESCAPE:
                                                               running = False
                                                if event.type == MOUSEBUTTONDOWN:
                                                        if event.button == 1:
                                                                click = True
                                                if event.type == pygame.MOUSEBUTTONDOWN:
                                                        if event.button == 1:
                                                                if button_attack.collidepoint((mx, my)):
                                                                        pygame.mixer.Sound.play(click_sound)
                                                                        update_healthbar()
                                                                        print(damage_done)
                                                                        healthbar = pygame.Rect(50,50, (how_much_health_bar), 50)
                                                                        move_complete = True



                                        if button_potion.collidepoint((mx, my)):
                                                if click:
                                                        pygame.mixer.Sound.play(click_sound)

                                        if button_spell.collidepoint((mx, my)):
                                                if click:
                                                        pygame.mixer.Sound.play(click_sound)

                                        if button_suicide.collidepoint((mx, my)):
                                                if click:
                                                        pygame.mixer.Sound.play(click_sound)



                                        #this draws the boxs on the game window
                                        pygame.draw.rect(screen, (0, 128, 128), healthbar_outline)
                                        pygame.draw.rect(screen, (180, 0, 0), healthbar)
                                        pygame.draw.rect(screen, (145, 39, 143), combat_log)


                                        #this will show the monsters health total
                                        pygame.draw.rect(screen, (255, 0, 0), healthbar)
                                        if healthbar.collidepoint((mx, my)):
                                                draw_text((display_health), font, (255, 255, 255), screen, 550, 60)
                                        else:
                                                pygame.draw.rect(screen, (255, 0, 0), healthbar)

                                        if healthbar_outline.collidepoint((mx, my)):
                                                pygame.draw.rect(screen, (255, 0, 0), healthbar)
                                                draw_text((display_health), font, (255, 255, 255), screen, 550, 60)
                                        else:
                                                pygame.draw.rect(screen, (255, 0, 0), healthbar)


                                        #this makes the buttons glow as you hover over them
                                        if button_attack.collidepoint((mx, my)):
                                                pygame.draw.rect(screen, (255, 0, 0), button_attack)
                                        else:
                                                pygame.draw.rect(screen, (180, 0, 0), button_attack)
                                        draw_text('attack', font, (255, 255, 255), screen, 150, 500)

                                        if button_potion.collidepoint((mx, my)):
                                                pygame.draw.rect(screen, (255, 0, 0), button_potion)
                                        else:
                                                pygame.draw.rect(screen, (180, 0, 0), button_potion)
                                        draw_text('potion', font, (255, 255, 255), screen, 150, 600)

                                        if button_spell.collidepoint((mx, my)):
                                                pygame.draw.rect(screen, (255, 0, 0), button_spell)
                                        else:
                                                pygame.draw.rect(screen, (180, 0, 0), button_spell)
                                        draw_text('spell', font, (255, 255, 255), screen, 450, 500)
                                        if button_suicide.collidepoint((mx, my)):
                                                pygame.draw.rect(screen, (255, 0, 0), button_suicide)
                                        else:
                                                pygame.draw.rect(screen, (180, 0, 0), button_suicide)
                                        draw_text('suicide', font, (255, 255, 255), screen, 450, 600)

                                        turn = 2
                                        print(turn)
                                        pygame.display.update()
                                        mainClock.tick(60)
                                except:
                                        print("please just work")


                if turn == 2:
                    try:
                        turn = 1
                        print(turn)
                        pygame.display.update()
                        mainClock.tick(60)
                    except:
                        print("please just work")
EN

回答 1

Stack Overflow用户

发布于 2020-10-17 02:40:07

窗口冻结是因为在第二种情况下没有事件循环。请分别查看pygame.event.get() pygame.event.pump()

对于游戏的每一帧,您将需要对事件队列进行某种调用。这确保了您的程序可以在内部与操作系统的其余部分进行交互。

在第二种情况下,实现与第一种情况类似的事件循环。例如:

代码语言:javascript
运行
复制
for event in pygame.event.get():
    # this will make it so that you can press escape to exit this funciton
    if event.type == QUIT:
        pygame.quit()
        sys.exit()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64394605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档