Lesson 13: Installing Pygame and Drawing Windows
Topics Covered:
- What is Pygame?
- Installing Pygame
- Creating a game window
- Drawing basic shapes: rectangles and circles
What is Pygame?
Pygame is a Python library designed for making video games. It helps you create graphics, play sounds, and handle user input.
Installing Pygame
To install Pygame, open your command prompt or terminal and type:
pip install pygame
If you are using an online editor, check if Pygame is pre-installed or supported.
Creating a Basic Game Window
import pygame
import sys
pygame.init()
# Set up the window
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("My First Pygame Window")
# Main loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 0)) # Fill screen with black
pygame.display.flip() # Update the display
pygame.quit()
sys.exit()
Drawing Shapes
You can draw shapes using Pygame’s drawing functions:
# Draw a red rectangle
pygame.draw.rect(screen, (255, 0, 0), (50, 50, 100, 50))
# Draw a blue circle
pygame.draw.circle(screen, (0, 0, 255), (320, 240), 40)
Activity: Draw Your Shapes
Modify the example to draw different colored shapes in different positions and sizes.