I'm making an alien invasion game by following the book Python Crash Course 2nd Edition, and I've come into an error that the book doesn't seem to have.
The exact traceback is:
Exception has occurred: error display Surface quit File "/workspaces/functions/alien_invasion/alien_invasion.py", line 34, in run_game self.screen.fill(self.settings.background_colour) File "/workspaces/functions/alien_invasion/alien_invasion.py", line 43, in <module> alien_invasion.run_game() pygame.error: display Surface quit
This is my code:
import sys
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion:
"""Overall class to manage game assets and behaviour."""
def __init__(self):
"""Initialise the game and create game resources."""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode(
(self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
# Set the backgroung colour.
self.background_colour = self.settings.background_colour # Creates a light grey colour.
def run_game(self):
"""Start the main loop for the game."""
while True:
# Watch for keyboard/mouse events.
for event in pygame.event.get():
if event.type == pygame.quit():
sys.exit()
# Redraw the screen during each pass through the loop.
self.screen.fill(self.settings.background_colour) # Line 34
self.ship.blit_me()
# Make the most recently drawn screen visible.
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance and run it.
alien_invasion = AlienInvasion()
alien_invasion.run_game() # Line 43
And I have installed Pygame via pip, so that's not the issue. I also clearly have both Pygame and sys imported.
If it's any help, here's the code for the settings:
class Settings:
"""A class to store all of the settings for alien_invasion.py."""
def __init__(self):
"""Initialise the game's settings."""
# Screen settings
self.screen_width = 1200
self.screen_height = 800
self.background_colour = (230, 230, 230)
And for the ship:
import pygame
class Ship:
"""A class to manage the ship."""
def __init__(self, alien_invasion_game):
"""Initialise the ship and set a starting position."""
self.screen = alien_invasion_game.screen
self.screen_rect = alien_invasion_game.screen.get_rect()
self.image_filepath = 'alien_invasion/assets/spaceship.jpg'
# Load the ship image and get its rect.
self.image = pygame.image.load(self.image_filepath)
self.rect = self.image.get_rect()
# Start each new ship at the bottom center of the screen.
self.rect.midbottom = self.screen_rect.midbottom
def blit_me(self):
"""Draw the ship at its current location."""
self.screen.blit(self.image, self.rect)
That error means that you called pygame.quit()
and then tried to continue with the game.
I believe this code is the problem:
if event.type == pygame.quit():
You're checking for quit events the wrong way.
The check is supposed to look like this:
if event.type == pygame.locals.QUIT:
But because you mistakenly used pygame.quit()
in the comparison, that means the pygame quit function is actually called!
Did the book tutorial actually do it that way?