Tuesday, September 15, 2026

PyGame Programming Setup

In 2020, we checked out Python Setup Cheat Sheet to create development environment. However, working through code exercises to improve code skills is tedious; we would like to build video games using PyGame!

Let’s check it out!

PyGame
PyGame is a free and open-source cross-platform library for the development of video games using Python. Using Simple DirectMedia Layer to abstract most common functions makes it easier to write these programs.

Tutorials
In order to build video games in Python we first need to learn PyGame APIs. Here are some great resources:
 PyGame Docs  Comprehensive documentation reference for using Pygame to create games
 PyGame Examples  GitHub repository with sample programs that demonstrate how to use Pygame
 PyGame Coders  Beginner-friendly Pygame tutorial that teaches the game development basics
 PyGame Tutorial  Extremely thorough Pygame tutorial used to drive examples in this blog post

Example I - Hello PyGame
All examples listed are Python 3.11.11 compatible. Launch PyCharm | New Project. Enter the following info:

 Location: ~/HelloPyGame
 Interpreter type:  uv
 Python version: 3.11
 Path to uv:  ~/.local/bin/uv

PyCharm should setup UV virtual environment and configure Python interpreter if not then enter commands:
  uv venv --python 3.11
  source .venv/bin/activate           # OR .\.venv\Scripts\activate
  which python
  `which python` --version            # Python 3.11.11

In the PyCharm Terminal | Enter the following commands for UV to install and sync package dependencies:
  uv lock
  uv sync

Create the following file: main.py. Enter the command uv run main.py. Verify Hello PyGame is working OK!
  Example I - Hello PyGame
  import pygame
  pygame.init()
  
  screen = pygame.display.set_mode((640, 480))
  pygame.display.set_caption("Hello PyGame!")
  
  font = pygame.font.Font(None, 48)  # Built-in default font
  text = font.render("Hello PyGame!", True, (255, 255, 255))
  
  running = True
  while running:
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              running = False
  
      screen.fill((0, 0, 0))
      screen.blit(text, (220, 220))
      pygame.display.flip()
  
  pygame.quit()


Example II - Hello Splash
Repeat previous exercise but render splash screen. Launch PyCharm | New Project. Enter the following info:
 Location: ~/HelloSplash
 Interpreter type:  uv
 Python version: 3.11
 Path to uv:  ~/.local/bin/uv

In the PyCharm Terminal | Enter the following commands for UV to install and sync package dependencies:
  uv venv --python 3.11
  source .venv/bin/activate           # OR .\.venv\Scripts\activate
  which python
  `which python` --version            # Python 3.11.11
  uv sync

Create the following file: main.py. Enter the command uv run main.py. Verify splash screen renders OK!
  Example II - Hello Splash
  import pygame
  pygame.init()
  
  screen = pygame.display.set_mode((640, 480))
  pygame.display.set_caption("Hello Splash")
  sms_surface = pygame.Surface((640, 480))
  
  splash = pygame.image.load("StevePro.bmp").convert()
  
  running = True
  while running:
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              running = False
  
      wide: int = (640 - splash.get_width()) / 2
      high: int = (480 - splash.get_height()) / 2
      sms_surface.blit(splash, dest=(wide, high))
  
      screen.fill((0, 0, 0))
      scaled = pygame.transform.scale(sms_surface, (640, 480))
      screen.blit(scaled, (0, 0))
  
      pygame.display.flip()
  
  pygame.quit()


Example III - Hello Text
Repeat previous exercise but render font text. Launch PyCharm | New Project. Setup project as previously:
  Example III - Hello Text
  import pygame
  pygame.init()
  
  screen = pygame.display.set_mode((640, 480))
  pygame.display.set_caption("Hello Text")
  sms_surface = pygame.Surface((640, 480))
  
  font = pygame.font.Font("emulogic.ttf", 20)
  running = True
  while running:
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              running = False
  
      sms_surface.fill((0, 0, 0))
      text_surface = font.render("Hello Text!", False, (255, 255, 255))
      sms_surface.blit(text_surface, (0, 0))
      scaled = pygame.transform.scale(sms_surface, (640, 480))
      screen.blit(scaled, (220, 220))
  
      pygame.display.flip()
  
  pygame.quit()

Example IV - Hello Sprites
Repeat previous exercise but render out sprites. Launch PyCharm | New Project. Setup project previously:
  Example IV - Hello Sprites
  import pygame
  pygame.init()
  
  screen = pygame.display.set_mode((640, 480))
  pygame.display.set_caption("Hello Sprites")
  sms_surface = pygame.Surface((320, 240))
  
  spritesheet = pygame.image.load("Sprites.bmp").convert()
  
  def get_sprite(sheet, col, row, width=32, height=32):
      rect = pygame.Rect(col * width, row * height, width, height)
      return sheet.subsurface(rect)
  
  running = True
  while running:
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              running = False
  
      sms_surface.fill((0, 0, 0))
  
      sprite1 = get_sprite(spritesheet, col=0, row=0)
      sprite2 = get_sprite(spritesheet, col=0, row=1)
      sms_surface.blit(sprite1, (64, 64))
      sms_surface.blit(sprite2, (128, 64))
  
      scaled = pygame.transform.scale(sms_surface, (640, 480))
      screen.blit(scaled, (0, 0))
  
      pygame.display.flip()
  
  pygame.quit()

Example V - Hello Input
Repeat previous exercise but with input detection. Launch PyCharm | New Project. Setup project previously:
  Example V - Hello Input
  import pygame
  pygame.init()
  
  screen = pygame.display.set_mode((640, 480))
  pygame.display.set_caption("Hello Input")
  
  font = pygame.font.Font(None, 48)  # Built-in default font
  text = font.render("Hello Input - Press space...", True, (255, 255, 255))
  
  running = True
  while running:
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              running = False
  
      screen.fill((0, 0, 0))
      screen.blit(text, (100, 220))
  
      keys = pygame.key.get_pressed()
      if keys[pygame.K_SPACE]:
          space = font.render("Space pressed!!", True, (255, 255, 255))
          screen.blit(space, (200, 260))
  
      pygame.display.flip()
  
  pygame.quit()

Example VI - Hello Sound
Example VII - Hello Music
Example VIII - Hello Audio
Repeat previous exercise but integrate all audio. Launch PyCharm | New Project. Setup project previously:
  Example VIII - Hello Audio
  import pygame
  pygame.init()
  pygame.mixer.init()
  
  screen = pygame.display.set_mode((640, 480))
  pygame.display.set_caption("Hello Audio")
  
  font = pygame.font.Font(None, 48)  # Built-in default font
  sound_01 = pygame.mixer.Sound("right.wav")
  sound_02 = pygame.mixer.Sound("wrong.wav")
  pygame.mixer.music.load("music.mp3")
  
  def render_text(text, color=(255, 255, 255)):
      return font.render(text, False, color)
  
  text1 = render_text("Hello Audio!" )
  text2 = render_text("Press Left - sound #1")
  text3 = render_text("Press Right - sound #2")
  text4 = render_text("Press Up - play music")
  text5 = render_text("Press Down - stop music")
  
  running = True
  while running:
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              running = False
  
      screen.fill((0, 0, 0))
      screen.blit(text1, (220, 140))
      screen.blit(text2, (160, 180))
      screen.blit(text3, (160, 220))
      screen.blit(text4, (160, 260))
      screen.blit(text5, (160, 300))
  
      keys = pygame.key.get_pressed()
      if keys[pygame.K_LEFT]:
          sound_01.play()
      if keys[pygame.K_RIGHT]:
          sound_02.play()
      if keys[pygame.K_UP]:
          pygame.mixer.music.play()
      if keys[pygame.K_DOWN]:
          pygame.mixer.music.stop()
  
      pygame.display.flip()
  
  pygame.quit()

  Example III - Hello Text   Example IV - Hello Sprites
  Example V - Hello Input   Example VIII - Hello Audio

Summary
To summarize, we now have the building blocks to implement PyGame APIs; we are in an excellent position to reuse existing video game content and port video game from another language to Python using PyGame! This will be the topic of the next post.