Pygame初始化后仍随机报错及关闭游戏时视频系统错误求助
Pygame点击圆形小游戏初始化错误修复
你遇到的两个错误核心原因都是调用pygame.quit()后没有立即终止游戏循环,导致后续代码仍在调用已卸载的Pygame库功能,具体问题和修复方案如下:
错误原因分析
- 随机出现
pygame.error: Library not initialized:当gameOver触发时,你设置了running = False并调用pygame.quit(),但循环并没有立即退出,后续的文本渲染、画面绘制等代码依然在执行,此时Pygame库已经被卸载,自然报错。 - 关闭游戏触发
pygame.error: video system not initialized:点击关闭按钮时,只调用了pygame.quit()但未设置running = False,游戏循环继续运行,后续尝试操作视频相关功能(如screen.fill、pygame.display.flip)时,视频系统已经被关闭,导致报错。
修复后的完整代码
import pygame import random import time # 初始化Pygame pygame.init() pygame.font.init() class Target(): def __init__(self, p, color): self.visible = True self.pos = p self.color = color self.startT = time.time() def isAlive(self): if time.time()-self.startT >= aliveTime: global gameOver gameOver = True return 0 else: if self.visible: return 1 return 0 def colisionCheck(self, color): if color == pygame.Color(self.color): self.visible = False global alive global score alive -= 1 score += 1 def update(self): if self.isAlive(): global targetSize pygame.draw.circle(screen, self.color, self.pos, targetSize) class Circle(pygame.sprite.Sprite): def __init__(self, pos, color, *grps): super().__init__(*grps) self.image = pygame.Surface((32, 32)) self.image.set_colorkey((1, 2, 3)) self.image.fill((1, 2, 3)) pygame.draw.circle(self.image, pygame.Color(color), (15, 15), 15) self.rect = self.image.get_rect(center=pos) running = True gameOver = False score = 0 frames = 0 targetSize = 20 aliveTime = 10 startAliveNum = 5 screenX = 1000 screenY = screenX*0.6 alive = startAliveNum targets = [] screen = pygame.display.set_mode((screenX, screenY)) font = pygame.font.Font('freesansbold.ttf', 12) player = Circle(pygame.mouse.get_pos(), 'dodgerblue', targets) pygame.display.set_caption('My Pygame game') pygame.display.flip() def newTargets(ammount): for i in range(ammount): pos = random.randint(50, screenX-50), random.randint(50, screenY-50) targets.append(Target(pos, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))) newTargets(5) startT = time.time() + 0.000001 targetStartT = startT while running: if gameOver: running = False # 先终止循环,再在循环外调用pygame.quit() continue frames += 1 text = font.render("{:.2f}fps".format(frames/(time.time()-startT)), True, (0, 0, 0), (255, 255, 255)) text2 = font.render("{:.2f} seconds left".format(targetStartT+10-time.time()), True, (0, 0, 0), (255, 255, 255)) text3 = font.render("score: " + str(score), True, (0, 0, 0), (255, 255, 255)) rect = text.get_rect() rect2 = text2.get_rect() rect2.center = (rect2.width/2, rect.height*1.5) rect3 = text3.get_rect() rect3.center = (rect3.width/2, rect.height*1.5*1.5) screen.fill((30, 30, 30)) screen.blit(text, rect) screen.blit(text2, rect2) screen.blit(text3, rect3) player.rect.center = pygame.mouse.get_pos() player.update() if alive == 0: startAliveNum += 1 newTargets(startAliveNum) alive += startAliveNum aliveTime += 0.5 targetStartT = time.time() + 0.000001 for i in range(len(targets)): targets[i].update() for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 先终止循环 if event.type == pygame.MOUSEBUTTONDOWN: color = screen.get_at(pygame.mouse.get_pos()) for i in range(len(targets)): targets[i].colisionCheck(color) pygame.display.flip() # 循环结束后统一调用pygame.quit() pygame.quit()
关键修改点
- gameOver处理:将
pygame.quit()移到循环外部,当gameOver时只设置running = False并continue跳过后续代码,确保循环正常退出后再卸载库 - 关闭事件处理:点击关闭按钮时,先设置
running = False终止循环,不再直接调用pygame.quit(),避免循环继续执行后续Pygame操作 - 统一清理:所有游戏逻辑结束后,在循环外部统一调用
pygame.quit(),保证所有Pygame操作都已完成后再卸载库
内容的提问来源于stack exchange,提问作者lukz
相关产品推荐
相关产品推荐

