1.Planning Your Project
2.Installing Pygame
python3 -m pip install --user pygame
3.Starting the Game Project
3.1 Creating a Pygame Window and Responding to User Input.
# import modules
import sys
import pygame
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game, and create game resources."""
# the pygame.init() function initializes the background settings that Pygame need to work properly.
pygame.init()
# create a display window. the argument (1200, 800) is a tuple that defines the dimensions of the game window,which will be 1200 pixels wide by 800 pixels high.
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."""
while True:
# Watch for keyboard and mouse events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# 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()
3.2 Controlling the Frame Rate
# import modules
import sys
import pygame
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game, and create game resources."""
# the pygame.init() function initializes the background settings that Pygame need to work properly.
pygame.init()
# define the clock in the __init() method.ensure the clock ticks once on each pass through the main loop.
self.clock = pygame.time.Clock()
# create a display window. the argument (1200, 800) is a tuple that defines the dimensions of the game window,which will be 1200 pixels wide by 800 pixels high.
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."""
while True:
# Watch for keyboard and mouse events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Make the most recently drawn screen visible
pygame.display.flip()
# the tick() method takes one argument: the frame rate for the game.
self.clock.tick(60)
if __name__ == '__main__':
# Make a game instance, and run the game.
ai = AlienInvasion()
ai.run_game()
3.3 Setting the Background Color
# import modules
import sys
import pygame
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game, and create game resources."""
# the pygame.init() function initializes the background settings that Pygame need to work properly.
pygame.init()
# define the clock in the __init() method.ensure the clock ticks once on each pass through the main loop.
self.clock = pygame.time.Clock()
# create a display window. the argument (1200, 800) is a tuple that defines the dimensions of the game window,which will be 1200 pixels wide by 800 pixels high.
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 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.bg_color)
# Make the most recently drawn screen visible
pygame.display.flip()
# the tick() method takes one argument: the frame rate for the game.
self.clock.tick(60)
if __name__ == '__main__':
# Make a game instance, and run the game.
ai = AlienInvasion()
ai.run_game()
3.4 Creating a Setting Class
class Settings:
"""A class to store all settings for Alien Invasion"""
def __init__(self):
"""Initialize the game's settings."""
# Screen settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
To make an instance of Settings in the project and use it to access our settings, we need to modify alien_invasion.py as follows:
# import modules
import sys
import pygame
from settings import Settings
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game, and create game resources."""
# the pygame.init() function initializes the background settings that Pygame need to work properly.
pygame.init()
# define the clock in the __init() method.ensure the clock ticks once on each pass through the main loop.
self.clock = pygame.time.Clock()
# create a display window. the argument (1200, 800) is a tuple that defines