Andrew Smith
Andrew Smith

Reputation: 621

Python Crash Course Alien Invasion issue

This is in regards to the book "Python Crash Course 2nd Edition".

After going through the "Drawing the Ship to the Screen" section in Chapter 12, I get a black screen, instead of a grey screen, and I'm not seeing the ship show up when I run my alien_invasion.py. I have tried running Matthes' downloadable resource file for that step, and I still get a black screen. I'm running these .py files from Sublime text, but have tried using terminal to run them (I get indent errors) and python IDLE to run them (gives me a pygame module not found error, though I know pygame is installed and found by Sublime).

Here is the code for the game that should no display a grey background and a ship at the bottom of the screen, if you have the ship image:

import sys

import pygame

from settings import Settings
from ship import Ship

class AlienInvasion:
    """Overall class to manage game assets and behavior."""

    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)

    def run_game(self):
        """Start the main loop for the game."""
        while True:
            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()

Im using Mac OS version 10.14.6.

Any solutions for this issue, or better practices or programs that I should be writing/running these game modules in?

Anyone with a Mac that has successfully installed pygame and/or has gotten alien_invasion to work?

One reply to my reddit post about this said they had a similar issue on Mac OS but when they tried on linux it worked fine...

Is there another alternative for Mac people to use short of installing linux on a separate partition or something?

Thanks for your help!

Upvotes: 1

Views: 2716

Answers (3)

Gary Wilson
Gary Wilson

Reputation: 1

I had this issue with the black screen closing immediately after. The code has the ship in folder 'images/ship.bmp'. Make sure the destination is accurate. Mine was in a slightly different location as I have a project folder for the book.

Mine was 'Python Crash Course/Alien Invasion/images/ship.bmp'

Upvotes: 0

Stephen Smith
Stephen Smith

Reputation: 11

I had the same issues, black screen and no ship. What solved the issue for me was, under settings, playing with the height and width. I'm a novice at this but I found that when I centered the ship instead of bottom center, the ship barely showed up at the bottom. Eventually I was able to center it at the bottom of the screen.

Upvotes: 1

Andrew Smith
Andrew Smith

Reputation: 621

Found a solution on Eric Matthes's github. Uff da, pygame is not stable on python 3.8.2 yet...I needed to install the dev version of pygame to run with python 3.8.2.

From Eric's github:

The stable version of Pygame has not been updated to work with Python 3.8 yet. However, there is a recent development version that works with Python 3.8. To install it, run the following command:

$ python -m pip install pygame==2.0.0.dev6

You should use the same command you use to run a Python terminal session on your system, which might be python, python3, py, python3.8, or something else.

If you’ve had any issues running Pygame on macOS, this version of Pygame should address those issues as well.

Upvotes: 3

Related Questions