What is Pygame?
Pygame is a Python library used to make games and multimedia applications. It handles:
• Graphics (drawing shapes, images)
• Sound (playing music and sound effects)
• Input (keyboard/mouse)
• Game timing (frame rate, timers)
It’s built on SDL (Simple DirectMedia Layer), which makes it suitable for 2D games.
SDL (Simple DirectMedia Layer) is a cross-platform, low-level development library written in C that
provides access to multimedia hardware components. It acts as a bridge between your game or app
and the system’s underlying audio, video, input, and graphics subsystems.
Space Shooter
Goal:
Control a spaceship that moves left/right and shoots bullets to destroy incoming enemy UFOs. User
scores points for each enemy destroyed. If an enemy collides with your spaceship — Game Over.
Explanation of Game Components
1. Setup and Initialization
pygame.init()
pygame.mixer.init()
• Initializes all Pygame modules.
• mixer handles sound/music.
2. Screen Setup
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
• Sets the game window size.
• caption sets window title: “Space Shooter”.
3. Assets (Images & Sounds)
We try to load spaceship (starshipdark.png) and enemy UFO (ufodark.png) images. If not found, we
use colored boxes instead.
player_img = pygame.image.load(...)
enemy_img = pygame.image.load(...)
We also load sound files for:
• Background music (background.wav)
• Laser shot (rock.wav)
• Explosion (8bit_bomb_explosion.wav)
4. Player Setup
player = player_img.get_rect(midbottom=(WIDTH // 2, HEIGHT - 20))
• Places the spaceship at the bottom center of the screen.
• speed = 7 sets movement speed.
5. Game Entities
• bullets = [] – list to track bullets.
• enemies = [] – list to track UFOs.
• enemy_timer triggers a new UFO every 1000 ms (1 sec).
6. Main Game Loop
This loop keeps the game running at 60 FPS.
a. Event Handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == enemy_timer:
...
• Handles quitting and spawns a new enemy every second.
b. Player Movement
keys = pygame.key.get_pressed()
• Move left with ← key, right with → key.
• Press SPACE to fire bullets (limit of 5 on screen).
c. Bullet Movement
bullet.y -= 10
• Bullets move upward.
• If off-screen, remove them.
d. Enemy Movement
enemy.y += 5
• Enemies fall from the top.
• If they hit the bottom or the player → Game Over.
e. Collision Detection
if bullet.colliderect(enemy):
bullets.remove(bullet)
enemies.remove(enemy)
score += 1
• If a bullet hits an enemy → play sound, remove both, increase score.
f. Game Over
if player.colliderect(enemy):
draw_text(" GAME OVER ", ...)
pygame.time.wait(2000)
running = False
• If an enemy collides with the player, show Game Over for 2 seconds, then quit.
7. Drawing Everything
Each frame:
• Background is black
• Draw:
o Player
o Bullets
o Enemies
o Score
Then:
pygame.display.flip()
• Updates the screen
And:
python
CopyEdit
clock.tick(60)
• Maintains 60 frames per second
Summary
Component Purpose
player The spaceship the player controls
bullets Fired by the player, can destroy enemies
enemies Fall from the top, must be shot or avoided
score Increases with each enemy destroyed
sound Music, shooting, and explosion effects
collision Detects bullet hits and player crash
Ideas to Expand This Game
• Add health/lives instead of instant game over
• Add power-ups (extra speed, double bullets)
• Add levels or boss enemies
• Add high score tracking