0

im very new to python, and im currently at chapter 12 on Python Crash Course.

it asks me to build an alien invasion game, and im stuck at this point where I wrote exactly what it said on the book, and tried exactly what YouTube Travis Pugh said in his tutorial on this chapter (his code works fine in the video) (https://youtu.be/WB8i9cseVT8), my pygame window jumps out and disappear immediately.

could anybody please help me ?

here's Travis Pugh's code(there's no changes to the class Settings):

(first file is ship.py)

import pygame

class Ship:
    def __init__(self, ai_game):
        """initialize the ship, set its starting location"""
        self.screen = ai_game.screen 
        self.screen_rect = ai_game.screen.get_rect()

        #load the ship image, get its rectangle
        self.image = pygame.image.load('images/ship.bmp')
        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 blitme(self):
        """draw the ship at its current location"""
        self.screen.blit(self.image, self.rect)

(second file is alien_invasion.py)

import sys
import pygame

from settings import Settings
from ship import Ship

class AlienInvasion:
    def __init__(self):
        """initialize 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 background color
        self.bg_color = (230,230,230)
    
    def run_game(self):
        """start the main loop for the game"""
        while True:
            # watch for keyboard and 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.bg_color)
            self.ship.blitme()

            #make the most recently drawn screen visible
            pygame.display.flip()

if __name__ == '__main__':
    #make a game instance, and run the game
    ai = AlienInvasion()  
    ai.run_game()
import random
  • 3,054
  • 1
  • 17
  • 22
Leah
  • 29
  • 2
  • How are you launching your python script? If you launch it from a command window, you'll see an error trace. This might be something you can fix, e.g. `ModuleNotFoundError: No module named 'pygame'`; Fix by `python -m pip install pygame`. Or it might be complex, in which case you can edit your question to include the traceback so it's possible to help you. – import random Apr 26 '22 at 12:51
  • Your code is looking to import a `Settings` class from `settings.py`, so if you've not created this yet, this could be the cause of your unhandled exception. – import random Apr 26 '22 at 12:55
  • 1
    thanks for the comments!! fixed the problem finally – Leah May 10 '22 at 03:06
  • Glad to hear you've fixed your problem, you can [answer your own question](https://stackoverflow.com/help/self-answer) in case others experience similar issues and wind up at your question in the future. – import random May 10 '22 at 04:44

0 Answers0