Pygame Basics
Pygame is a cross-platform library used for making 2D games in Python.
Pygame Introduction
Pygame allows you to create games with graphics, sound, and user interaction.
import pygame
pygame.init()
print("Pygame initialized!")
Basic Pygame Structure
Every Pygame program has a main loop handling events.
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
Pygame - Display Modes
Set up the game window using display modes.
Pygame - Colors and Events
Define colors and handle user inputs with keyboard and mouse events.
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
RED = (255, 0, 0)
pygame.draw.rect(screen, RED, (50, 50, 100, 100))
pygame.display.update()
Pygame - Drawing Shapes
Use Pygame's built-in functions to draw shapes.
pygame.draw.circle(screen, (0, 255, 0), (200, 150), 50)
Pygame - Using Images
Load and display images in Pygame.
image = pygame.image.load('player.png')
screen.blit(image, (100, 100))
Pygame - Playing Sound
Play sound and background music in your game.
pygame.mixer.init()
pygame.mixer.Sound('sound.wav').play()