【问题标题】:Pygame error: video system not initialized stuck in game loop [duplicate]Pygame错误:视频系统未初始化卡在游戏循环中[重复]
【发布时间】:2016-06-17 00:30:30
【问题描述】:

好的,所以我有这个我使用 pygame 制作的蛇游戏,它非常基本并且可以运行,但是我创建了一个登录程序,我需要从中运行游戏,每当我尝试关闭游戏时,我都会收到一个看起来像的错误这个问题是,当我尝试在蛇代码中的 pygame.exit() 之后放置 sys.exit() 时,我的登录程序关闭并只运行一个空白的 python 窗口:

Traceback (most recent call last):
  File "C:\Users\test\Desktop\Summative\LukaLogin.py", line 119, in Snake
    snake = snakeSummative.main()        
  File "C:\Users\test\Desktop\Summative\snakeSummative.py", line 178, in main
    playAgain(final)
  File "C:\Users\test\Desktop\Summative\snakeSummative.py", line 161, in   playAgain
    for event in pygame.event.get():
pygame.error: video system not initialized 

贪吃蛇游戏:

def playAgain(final):
    gameDisplay.fill(WHITE)
    username = 'luka'
    message_to_screen("Game Over, press c to play again press q to quit", RED)
    message_to_screen1(str(final),GREEN)
    file = open('Scores.txt','a')
    file.write( username + ' ' + 'Snake' + ' ' + str(final) + ' ' + '\n')
    pygame.display.update()
    while True:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_c:
                        score = 0
                        return True
                    if event.key == pygame.K_q:
                        pygame.quit()
                        break              
def main():
    while True:
        final = gameLoop()
        playAgain(final)
    pygame.quit()
    quit()

if __name__ == '__main__':
    main()

调用蛇游戏的登录def:

class GameHub(QtGui.QMainWindow,Ui_GameHub):
    def __init__ (self):
        QtGui.QMainWindow.__init__(self)
        self.setupUi(self)
        self.play_BlackJack.clicked.connect(self.BlackJack)
        self.play_Snake.clicked.connect(self.Snake)
        self.play_Scores.clicked.connect(self.HighScores)
        self.play_Logout.clicked.connect(self.LogOut)
    def Snake(self):
        import snakeSummative
        snake = snakeSummative.main()  

如果我打破循环,我真的很难理解为什么我会得到错误的任何帮助

【问题讨论】:

    标签: python windows python-2.7 pyqt pygame


    【解决方案1】:

    您正在退出 pygame,然后尝试再次使用它:

    if event.key == pygame.K_q:
        pygame.quit()
        break
    

    这会退出 pygame,然后打破围绕它的 while 循环,然后在 main() 中再次调用函数,但这次 pygame 已退出。您需要打破外循环才能退出永远不会完成的游戏。

    def playAgain(final):
        gameDisplay.fill(WHITE)
        username = 'luka'
        message_to_screen("Game Over, press c to play again press q to quit", RED)
        message_to_screen1(str(final),GREEN)
        file = open('Scores.txt','a')
        file.write( username + ' ' + 'Snake' + ' ' + str(final) + ' ' + '\n')
        pygame.display.update()
        while True:
            for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        return False
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_c:
                            score = 0
                            return True
                        if event.key == pygame.K_q:
                            return False         
    def main():
        while True:
            final = gameLoop()
            if not playAgain(final): break
        pygame.quit()
    
    if __name__ == '__main__':
        main()
    

    我认为这是你想要的行为,希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      我猜This 可能会回答你的问题。

      在启动游戏程序之前,请确保您已致电pygame.init()

      【讨论】:

      • 你为什么要发布这个作为答案?我已经将其标记为重复。
      • 没注意到@LPK
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多