Pygame Tutorial
Pygame Game Development Fundamentals
This tutorial provides a foundational understanding of Pygame, a set of Python modules designed for writing video games. It covers essential concepts for aspiring game developers, drawing inspiration from resources like the Clear Code tutorial.
Creating a Basic Pygame Window
The first step in game development with Pygame is to set up a display window. This involves initializing Pygame, creating a screen surface, setting a window title, and establishing a game loop to keep the window open and responsive.
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800,400))
pygame.display.set_caption('My Game')
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# draw all our elements
# update everything
pygame.display.update()
clock.tick(60)
Handling Mouse Input in the Event Loop
Interacting with the game world often requires mouse input. Pygame's event loop allows you to detect mouse movements and clicks. Understanding `event.pos` for coordinates and different mouse button events is crucial for creating interactive elements.
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800,400))
pygame.display.set_caption('My Game')
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEMOTION:
print(event.pos)
if event.type == pygame.MOUSEBUTTONDOWN:
print('mouse was clicked')
if event.type == pygame.MOUSEBUTTONUP:
print('mouse button was released')
pygame.display.update()
clock.tick(60)
Handling Keyboard Input in the Event Loop
Keyboard input is fundamental for controlling game characters and triggering actions. Pygame's event system captures key presses (`KEYDOWN`) and releases (`KEYUP`), enabling you to implement player controls and game commands.
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800,400))
pygame.display.set_caption('My Game')
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
print('key pressed')
if event.type == pygame.KEYUP:
print('key was released')
pygame.display.update()
clock.tick(60)
Working with Surfaces in Pygame
Surfaces are the building blocks for rendering graphics in Pygame. You can load images, create text, and draw shapes onto surfaces, which are then "blitted" (drawn) onto the main screen surface. This section demonstrates loading background images, rendering text, and drawing basic shapes.
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800,400))
pygame.display.set_caption('My Game')
clock = pygame.time.Clock()
# Assuming these image files exist in the specified path
sky_surface = pygame.image.load('src/graphics/sky.png').convert()
ground_surface = pygame.image.load('src/graphics/ground.png').convert()
# Example for text rendering (ensure 'test_font' is defined or loaded)
# For demonstration, let's assume a font is initialized elsewhere or use a default
try:
test_font = pygame.font.Font(None, 30) # Default font, size 30
except pygame.error:
print("Warning: Could not load default font. Text rendering might fail.")
test_font = None
if test_font:
text_surface = test_font.render('Welcome', False, (64,64,64))
score_rectangle = text_surface.get_rect(center = (400, 50))
else:
score_rectangle = pygame.Rect(350, 25, 100, 50) # Placeholder rect if font fails
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEMOTION:
print(event.pos)
screen.blit(sky_surface,(0,0))
screen.blit(ground_surface,(0,300))
pygame.draw.rect(screen,'#c0e8ec',score_rectangle)
pygame.draw.rect(screen,'#c0e8ec',score_rectangle,10)
if test_font:
screen.blit(text_surface,score_rectangle)
pygame.display.update()
clock.tick(60)
This tutorial provides a starting point for your Pygame journey. By mastering these fundamental concepts, you can begin building more complex game mechanics, characters, and environments. Explore the official Pygame documentation for further details on advanced features and libraries.
For more in-depth learning, consider these external resources: