1
votes

I am following Python Crash Course 2nd edition a the moment. I'm stuck on chapter 12 where you start with pygame. This is the code (from the book, so should work). I'm on mac using VSC.

import sys

import pygame

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

    def __init__(self):
        """Initialize the game, and create game resources"""
        pygame.init()

        self.screen = pygame.display.set_mode((1200, 800))
        pygame.display.set_caption("Alien Invasion")

        # set the background color.
        self.bg_color = (230, 230, 230)

    def run_game(self):
        """Start th emain loop for the game"""
        while True:
            # Watch for keyboard and mouse events.
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    print('Quitting...')
                    sys.exit()

            # Redraw the screen during each pass through the loop.
            self.screen.fill(self.bg_color)

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

if __name__ == '__main__':
    # Make a game instance and run the game.
    ai = AlienInvasion()
    print("running pygame...")
    ai.run_game()

the print("running pygame...") statement runs, but no window opens.

Any ideas as to whats going wrong here?

EDIT: pygame is installed through pip3. I've tried running this code in Idle and VSC. The output to my terminal is as follows

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
running pygame...

no new window opens after that

2
I can't see any obvious issue. the code works fine for me. - Rabbid76
In the last few weeks (early 2020) there's been lots of questions here about problems with Python 3.8 and PyGame. Maybe try dropping back to Python 3.7 until minor issues are sorted out. - Kingsley
I've tried running pygame in python 3.6 but that still gives me the same problem. I get a python icon pop up in the dock of my mac, but nothing opens - coding_catastrophe

2 Answers

0
votes

You forgot the () after the AlienInvasion class. It should look like this: class AlienInvasion(): """Overall class to manage game assets and behavior."""

0
votes

I ran into this issue, and had to downgrade to python 3.6 from 3.8. That fixed it for me. There seem to be a lot of issues in running pygame in 3.8.