0% found this document useful (0 votes)
17 views1 page

Python Crash Course - 066

Uploaded by

darkflux514
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

Python Crash Course - 066

Uploaded by

darkflux514
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Starting the Game Project

We’ll begin building the game by creating an empty Pygame window. Later,
we’ll draw the game elements, such as the ship and the aliens, on this win-
dow. We’ll also make our game respond to user input, set the background
color, and load a ship image.

Creating a Pygame Window and Responding to User Input


We’ll make an empty Pygame window by creating a class to represent the
game. In your text editor, create a new file and save it as alien_invasion.py;
then enter the following:

alien import sys


_invasion.py
import pygame

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

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

2 self.screen = pygame.display.set_mode((1200, 800))


pygame.display.set_caption("Alien Invasion")

def run_game(self):
"""Start the main loop for the game."""
3 while True:
# Watch for keyboard and mouse events.
4 for event in pygame.event.get():
5 if event.type == pygame.QUIT:
sys.exit()

# Make the most recently drawn screen visible.


6 pygame.display.flip()

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

First, we import the sys and pygame modules. The pygame module con-
tains the functionality we need to make a game. We’ll use tools in the sys
module to exit the game when the player quits.
Alien Invasion starts as a class called AlienInvasion. In the __init__()
method, the pygame.init() function initializes the background settings that
Pygame needs to work properly 1. Then we call pygame.display.set_mode()
to create a display window 2, on which we’ll draw all the game’s graphical
elements. The argument (1200, 800) is a tuple that defines the dimensions
of the game window, which will be 1,200 pixels wide by 800 pixels high.
(You can adjust these values depending on your display size.) We assign this

A Ship That Fires Bullets 229

You might also like