微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PyGame 窗口崩溃而不运行代码

如何解决PyGame 窗口崩溃而不运行代码

每次我尝试在 pygame 中运行我的代码时,它都会加载窗口,然后冻结并且没有响应。有人告诉我,代码不会运行超过显示更新循环。我无法弄清楚出了什么问题,并且在无法测试的情况下尝试编写代码非常令人沮丧。这是一个简单的 PyGame 平台游戏,我正在尝试为学校的一个项目制作。这只是主菜单,确实包含了一些不在此代码片段中的功能。我使用 VS Code 编写程序,它运行在名为“Pygame Snippets”的扩展上。 代码在这里

import pygame
from pygame.locals import *

run = 1
loop = 1

pygame.init()
Screen = pygame.display.set_mode((1280,720))

White = (255,255,255)
DGray = (128,128,128)
LGray = (170,170,170)

DFont = pygame.font.SysFont("Arial",40)
BFont = pygame.font.SysFont("Arial",60)

while loop == 1:
    pygame.time.delay(100)
    pygame.display.update()

while run == 1:
    def main_menu():

            Menu1 = 1
            hover_controls = 0
            hover_levels = 0
            hover_quit = 0

            T1 = DFont.render("Level Select",True,White)
            T2 = DFont.render("Controls",White)
            T3 = DFont.render("Quit",White)
            Title = BFont.render("Maths Platformer",White)


            while Menu1 == 1:

                mouse = pygame.mouse.get_pos()
            
                if 220 <= mouse[0] <= 420 and 310<= mouse[1] <= 410:
                    hover_levels == 1

                if 860 <= mouse[0] <= 1060 and 310<= mouse[1] <= 410:
                    hover_controls == 1

                if 540 <= mouse[0] <= 740 and 550 <= mouse[1] <= 650:
                    hover_quit == 1



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

                    else:

                        if ev.type == pygame.MOUSEBUTTONDOWN:

                            if hover_levels == 1:
                                levels_menu()

                            if hover_controls == 1:
                                controls_menu()

                            if hover_quit == 1:
                                pygame.quit

            screen.fill((117,196,255))

            if hover_levels == 1:
                pygame.draw.rect(screen,LGray,[220,310,200,100])

            else:
                pygame.draw.rect(screen,DGray,100])


            if hover_controls == 1:
                pygame.draw.rect(screen,[860,100])


            if hover_quit == 1:
                pygame.draw.rect(screen,[540,550,100])


            screen.blit(T1,(270,360))

            screen.blit(T2,(910,360))

            screen.blit(T3,(590,600))

            screen.blit(Title,(570,180))


        
                
    main_menu() 

解决方法

循环 while loop == 1: 永远不会终止,因为 loop 永远不会改变。完全删除此代码。
== 是比较运算符。指定变量时必须使用赋值运算符(例如:hover_levels = 1)。 Python 区分大小写。因此,您需要确保在任何地方都使用 screen(而不是 Screen)。
您必须在应用程序循环中绘制菜单。尊重Indentation
pygame.QUIT 是一种事件类型。 “退出”函数的名称是 pygame.quit()

import pygame
from pygame.locals import *

run = 1
#loop = 1

pygame.init()
screen = pygame.display.set_mode((1280,720))

White = (255,255,255)
DGray = (128,128,128)
LGray = (170,170,170)

DFont = pygame.font.SysFont("Arial",40)
BFont = pygame.font.SysFont("Arial",60)

#while loop == 1:
#    pygame.time.delay(100)
#    pygame.display.update()

def main_menu():
    global run

    Menu1 = 1
    
    T1 = DFont.render("Level Select",True,White)
    T2 = DFont.render("Controls",White)
    T3 = DFont.render("Quit",White)
    Title = BFont.render("Maths Platformer",White)

    while Menu1 == 1:

        mouse = pygame.mouse.get_pos()
        hover_controls = 0
        hover_levels = 0
        hover_quit = 0
        if 220 <= mouse[0] <= 420 and 310<= mouse[1] <= 410:
            hover_levels = 1
        if 860 <= mouse[0] <= 1060 and 310<= mouse[1] <= 410:
            hover_controls = 1
        if 540 <= mouse[0] <= 740 and 550 <= mouse[1] <= 650:
            hover_quit = 1

        for ev in pygame.event.get():
            if ev.type == pygame.QUIT:
                run = False
                Menu1 = False
            else:

                if ev.type == pygame.MOUSEBUTTONDOWN:
                    if hover_levels == 1:
                        levels_menu()
                    if hover_controls == 1:
                        controls_menu()
                    if hover_quit == 1:
                        run = False
                        Menu1 = False

    # Indentation
    #-->|

        screen.fill((117,196,255))

        if hover_levels == 1:
            pygame.draw.rect(screen,LGray,[220,310,200,100])
        else:
            pygame.draw.rect(screen,DGray,100])

        if hover_controls == 1:
            pygame.draw.rect(screen,[860,100])

        if hover_quit == 1:
            pygame.draw.rect(screen,[540,550,100])

        screen.blit(T1,(270,360))
        screen.blit(T2,(910,360))
        screen.blit(T3,(590,600))
        screen.blit(Title,(570,180))
        pygame.display.update()

while run == 1:             
    main_menu() 

pygame.quit()
exit()
,

这个循环:

while loop == 1:
    pygame.time.delay(100)
    pygame.display.update()

始终运行,然后不断运行。删除它并将 display.update 放在菜单循环的末尾。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。