Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pygame Passing from main to game file causing errors

I receive a "pygame.error: display Surface quit" when trying to run the main.py file.

from Asteroid import Asteroid
import neat
import pygame

pygame.init()
neat.config.Config

Screenw, Screenh = 500, 500
windo = pygame.display.set_mode((Screenw, Screenh))

game = Asteroidick.Game()
game.__init__(windo)

running = True

while running:
    game.loop()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            key = pygame.key.name(event.key)
            game.input(key)
        
    pygame.quit()

Here’s the Asteroid file

import pygame
import random

pygame.init()

class Game:

    red = random.randrange(256)
    green = random.randrange(256)
    blue = random.randrange(256)
    userred = 0
    usergreen = 0
    userblue = 0

    userlist = [userred, usergreen, userblue]
    points = 0 #this number increases every time you input the right color value.
    teext = f"Points: {points}" # + points
    i = 0


    def __init__(self, screen):
        pygame.display.set_caption("Asteroiiid")
        self.screen = screen
        self.screen.fill((255, 255, 255))

    def checker(self):
        righty = True
        if(self.userlist[0] != self.red):
            righty = False
        else: 
            self.points += 1
        if(self.userlist[1] != self.green):
            righty = False
        else: 
            self.points += 1
        if(self.userlist[2] != self.blue):
            righty = False
        else: 
            self.points += 1
        if righty:
            self.points += 1
        self.red = random.randrange(256)
        self.green = random.randrange(256)
        self.blue = random.randrange(256)
        self.userlist[0] = 0
        self.userlist[1] = 0
        self.userlist[2] = 0 
        self.i = 0

    def loop(self):
        self.teext = f"Points: {self.points}"
        if self.i == 3:
            self.checker()      
#<----------ERROR OCCURS HERE----------------->  
        self.screen.fill((self.red, self.green, self.blue))
        font = pygame.font.SysFont("Arial", 24)
        score = font.render(self.teext, True, (0,0,0))
        redval = font.render(str(self.userlist[0]), True, (255,0,0))
        greenval = font.render(str(self.userlist[1]), True, (0, 255, 0))
        blueval = font.render(str(self.userlist[2]), True, (0,0,255))

        self.screen.blit(score,(0,0))
        self.screen.blit(redval,(400,100))
        self.screen.blit(greenval,(400,200))
        self.screen.blit(blueval,(400,300))
        pygame.display.flip()

    def input(self, key):
        if (key == 'up'):
            self.userlist[self.i] += 1
        if (key == 'return'):
            self.i = self.i + 1
        else:
            self.loop()
        #I think I'll just have the user enter values

pygame.quit()

If I comment out self.screen.fill((self.red,self.green,self.blue)) line, I receive a different error on the fonts.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have been slowly building this file up and I’ve just moved the window to main. It worked before when I did not pass window to Asteroid. The init method also doesn’t get errors, either.

>Solution :

When pygame.quit() is executed, all PyGame modules a uninstalled. Every following call to a PyGame API function will cause an error. You must not call pygame.quit() in the application loop. Call it after the application loop.

while running:
    game.loop()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            key = pygame.key.name(event.key)
            game.input(key)

#<--|        
pygame.quit()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading