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

Pygame、pygame_menu - 启动后关闭且视频系统未初始化错误

如何解决Pygame、pygame_menu - 启动后关闭且视频系统未初始化错误

我正在尝试制作 Simon 游戏。我是编程初学者。 我在菜单启动时第一次启动程序时遇到问题。当我点击子菜单中的播放和另一个播放时,它会启动主循环并且游戏正常运行。 当我关闭窗口时,它给了我一个错误

pygame.display.flip() 错误:视频系统未初始化

当我第二次启动程序时,窗口将启动,但它立即关闭,没有错误消息。

所以它仍然存在:
第一次启动 -> 启动并关闭错误
第二次启动 -> 无错误关闭窗口。
我检查了一些帖子 我检查了一些帖子,但没有用。


import pygame as game
import pygame_menu as game_menu

print(game.font.get_fonts())


game.init()

game.font.init()

game.mixer.init()


WIDTH,HEIGHT = 900,500


FPS = 60


window = game.display.set_mode((WIDTH,HEIGHT))

game.display.set_caption("Simon v1.0")

#FONTS

Font = game.font.SysFont("Arial",85)
mainFont = game_menu.font.FONT_MUNRO
titleFont = game_menu.font.FONT_8BIT
titleBar = game_menu.widgets.MENUBAR_STYLE_UNDERLINE
fontscore = game.font.SysFont("Arial",35)
FontLower = game.font.SysFont("Arial",20)

#Width for center
# nameWidth,_ = Font.size("SIMON")
pressWidth,_ = Font.size("Press space to start")
overWidth,_ = Font.size("Gameover")
#MENU THEME
menuTheme = mytheme = game_menu.Theme(background_color=(0,0),title_background_color=(255,255,255),widget_padding = 15,widget_font = mainFont,widget_font_size = 20,widget_font_color = (255,title_font = titleFont,title_bar_style = titleBar,title_offset = (0,-10),widget_Box_border_width = 100,title_font_color = (209,212,161))

#Player name
playerName = ""

#colors
radius = 100
redCircle = (140,0)
blueCircle = (0,140)
yellowCircle = (140,140,0)
greenCircle = (0,0)

run = True
gameOver = False
gameState = 0
score = 0

def main():

    global run,gameState,colorR,colorG,colorB


    clock = game.time.Clock()  
    draw()
    while run == True:
        clock.tick(FPS)
        for event in game.event.get():

            if event.type == game.QUIT:

               run = False
    game.quit() 

def draw():
    global gameOver,overWidth
    
    window.fill((0,0))
    
    if gameState == 0:
        textPress = fontscore.render("Press space to start ",1,(255,255))
        window.blit(textPress,(WIDTH/2 - pressWidth/2,50))
    
    if gameOver:
        textGameOver = mainFont.render("Gameover",255))
        centerTitle = textGameOver.get_rect(center=(WIDTH/2,120))
        window.blit(textGameOver,centerTitle)
        
        textFinscore = fontscore.render("Your score is: " + str(score),255))
        centerTitle = textFinscore.get_rect(center=(WIDTH/2,200))
        window.blit(textFinscore,centerTitle)
        
        textRestart = FontLower.render("Press space for restart",255))
        centerTitle = textRestart.get_rect(center=(WIDTH/2,300))
        window.blit(textRestart,centerTitle)
        
        textExit = FontLower.render("Press esc for exit from game",255))
        centerTitle = textExit.get_rect(center=(WIDTH/2,335))
        window.blit(textExit,centerTitle)
        
    else:
        game.draw.circle(window,redCircle,[(WIDTH/2),240],radius,40,draw_top_right = True)
        game.draw.circle(window,blueCircle,250],draw_bottom_right = True)
        game.draw.circle(window,greenCircle,[(WIDTH/2)-10,draw_top_left = True)
        game.draw.circle(window,yellowCircle,draw_bottom_left = True)  
        draw_score()
        
    game.display.update() 
    game.display.flip()
    
def draw_score():
    textscore = fontscore.render("score: " + str(score),255))
    window.blit(textscore,(20,0))    

def save_name(name):
    global playerName
    playerName = name
    print(playerName)

def mainMenu():
    global top,menu,credi,play,topMenu
    
    Cred = "Created by: "
    
    menu = game_menu.Menu(300,400,'Simon',theme = menuTheme)
    credi = game_menu.Menu(300,onclose = None,theme = menuTheme)
    play = game_menu.Menu(300,theme = menuTheme)
    topMenu = game_menu.Menu(300,theme = menuTheme)
    #Main menu
    menu.add.button("Play",play)
    menu.add.button("Top score")
    menu.add.button("Settings")
    menu.add.button("Credits",credi)
    menu.add.button("Quit",game_menu.events.EXIT)
    #Credit submenu
    credi.add_label(Cred,max_char = 0,font_size = 20)
    credi.add.button("Back",game_menu.events.BACK)
    
    #Play submenu
    play.add_label("Type your name",font_size = 20)
    play.add.text_input("Name : ",default = '',input_underline = "_",maxchar = 14,onchange = save_name,onreturn = save_name)
    play.add.button("Play",main)
    play.add.button("Back",game_menu.events.BACK)
    menu.mainloop(window)

mainMenu()

抱歉我的英语不好。 并感谢您的提示

解决方法

当您关闭窗口时,main() 运行 game.quit() 关闭对视频的访问但接下来它返回到 mainMenu 仍然运行 mainloop() 以绘制菜单并且它仍然尝试访问视频显示菜单。这会导致问题

您可以在`game.quit() 之后使用exit() 退出程序。

        if event.type == game.QUIT:
           game.quit() 
           exit()

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