0

Ok so i have this snake game that i made using pygame its very basic and it works but i created a login program that i need to rund the game from and whenever i try to close the game i get a error that looks like this The thing is that when i tried putting sys.exit() after the pygame.exit() in the snake code my login program closes and just runs a blank python window:

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 

Snake Game:

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()

Login def that calls the snake game:

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()  

Any help would be apreciated im really stumped to as why i get the error even though i break the loop

2 Answers2

0

You are quitting pygame and then trying to use it again:

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

this quits pygame then breaks the while loop surrounding it, then function is called again in main() but this time pygame has been quit. You need to break the outer loop in order to quit the game which is never being done.

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()

I think this is the behaviour you were going for, hope this helps.

DCA-
  • 1,262
  • 2
  • 18
  • 33
0

I guess This might answer your question.

Make sure you have called pygame.init() before starting the game program.

Community
  • 1
  • 1
White Shadow
  • 444
  • 2
  • 10
  • 26