python - Pygame,pygame_menu - 启动后关闭和视频系统未初始化错误
问题描述
我正在尝试制作西蒙游戏。我是编程初学者。我在菜单第一次启动程序时遇到问题。当我点击播放和子菜单中的另一个播放时,它会启动主循环并且游戏正常运行。当我关闭窗口时,它给了我一个错误:
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, 0, 0),
title_background_color=(255, 255, 255, 255),
widget_padding = 15,
widget_font = mainFont,
widget_font_size = 20,
widget_font_color = (255, 255, 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,0)
blueCircle = (0,0,140)
yellowCircle = (140,140,0)
greenCircle = (0,140,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,0))
if gameState == 0:
textPress = fontScore.render("Press space to start ", 1, (255,255,255))
window.blit(textPress, (WIDTH/2 - pressWidth/2,50))
if gameOver:
textGameOver = mainFont.render("Gameover", 1, (255,255,255))
centerTitle = textGameOver.get_rect(center=(WIDTH/2,120))
window.blit(textGameOver, centerTitle)
textFinScore = fontScore.render("Your score is: " + str(score), 1, (255,255,255))
centerTitle = textFinScore.get_rect(center=(WIDTH/2,200))
window.blit(textFinScore, centerTitle)
textRestart = FontLower.render("Press space for restart", 1, (255,255,255))
centerTitle = textRestart.get_rect(center=(WIDTH/2,300))
window.blit(textRestart, centerTitle)
textExit = FontLower.render("Press esc for exit from game", 1, (255,255,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, [(WIDTH/2), 250], radius, 40, draw_bottom_right = True)
game.draw.circle(window, greenCircle, [(WIDTH/2)-10, 240], radius, 40, draw_top_left = True)
game.draw.circle(window, yellowCircle, [(WIDTH/2)-10, 250], radius, 40, draw_bottom_left = True)
draw_score()
game.display.update()
game.display.flip()
def draw_score():
textScore = fontScore.render("Score: " + str(score), 1, (255,255,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, 400, 'Simon', onclose = None, theme = menuTheme)
play = game_menu.Menu(300, 400, 'Simon', onclose = None, theme = menuTheme)
topMenu = game_menu.Menu(300, 400, 'Simon', onclose = None, 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()
which close access to video 但接下来它会返回到mainMenu
仍然运行mainloop()
以绘制菜单并且它仍然尝试访问视频显示菜单。这使得问题
您可以exit()
在 `game.quit() 之后使用退出程序。
if event.type == game.QUIT:
game.quit()
exit()
推荐阅读
- java - 带有 AWS EC2 IMDSv2 的 Spring Cloud Netflix Eureka 客户端?
- java - 使用 Java AWT 在 2D Canvas 上渲染 3D 对象
- python - 如何在 discord.py 中获取 pin 消息
- wordpress - 多站点内容同步 Wordpress 问题
- android - 如何修复此错误?错误:找不到符号 Vholder.setData(mValues.get(position));
- filter - 没有为 scipy.signal.butter 定义名称为“b”的错误。怎么了?
- javascript - n秒后datatable.js ajax livereload
- javascript - 在 arr 中生成 4 个唯一的随机数?
- dialogflow-es - 无法创建新代理
- c# - jQuery:当我有多个具有相同名称但唯一 ID 的元素时,我可以按名称选择一个元素并读取它的 ID 吗?