Pygame Example
This is a pygame example.
Supplies
Any computer that has pygame and python
text editer
and some way to run that code
The Instructions
- Import necessary modules: The pygame module is imported for game development, and sys is imported for system-specific parameters and functions.
import pygame
import sys
- Initialize Pygame: This is necessary for every Pygame application.
pygame.init()
- Set up constants: These constants define the window size, player speed, gravity, jump force, and frames per second.
WIDTH, HEIGHT = 640, 480
SPEED = 6
GRAVITY = 0.5
JUMP_FORCE = 10
FPS = 30
- Set up the display: This creates a window for the game. The size of the window is defined by the WIDTH and HEIGHT constants.
screen = pygame.display.set_mode((WIDTH, HEIGHT))
- Set up the player: This creates a rectangle for the player. The rectangle is positioned in the middle of the screen and has a size of 50x50 pixels. A vector is also created for the player’s speed.
player = pygame.Rect(WIDTH / 2, HEIGHT / 2, 50, 50)
player_speed = pygame.Vector2(0, 0)
- Set up the clock: This creates a clock to control the frame rate of the game.
clock = pygame.time.Clock()
- Set up the game loop: This is the main loop of the game. It continues as long as the running variable is True.
running = True
jumping = False
while running:
...
- Fill the screen: This fills the screen with black at the start of each frame.
screen.fill((0, 0, 0))
- Draw the player: This draws the player on the screen. The player is represented as a white rectangle.
pygame.draw.rect(screen, (255, 255, 255), player)
- Event handling: This checks for two types of events - QUIT and KEYDOWN. If the QUIT event is detected, the game ends. If the KEYDOWN event is detected and the key is SPACE, and the player is on the ground (player’s bottom is at HEIGHT), the player starts to jump.
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and player.bottom >= HEIGHT:
jumping = True
- Player movement: This checks if the left or right arrow key is pressed. If the left key is pressed, the player moves to the left. If the right key is pressed, the player moves to the right. If neither key is pressed, the player doesn’t move horizontally.
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_speed.x = -SPEED
elif keys[pygame.K_RIGHT]:
player_speed.x = SPEED
else:
player_speed.x = 0
- Jumping: If the player is jumping, the vertical speed is set to the negative of the jump force, and the jumping variable is set to False.
if jumping:
player_speed.y = -JUMP_FORCE
jumping = False
- Apply gravity: Gravity is added to the vertical speed.
player_speed.y += GRAVITY
- Move the player: The player’s position is updated based on the player’s speed.
player.move_ip(player_speed)
- Ensure the player stays on the screen: If the player is below the bottom of the screen, the player is moved back to the bottom of the screen and the vertical speed is set to 0.
if player.bottom > HEIGHT:
player.bottom = HEIGHT
player_speed.y = 0
- Flip the display: This updates the display with everything that has been drawn.
pygame.display.flip()
- Cap the frame rate: This ensures the game runs at a maximum of 30 frames per second.
clock.tick(FPS)
- Quit Pygame: After the game loop ends, Pygame is quit and the program is exited.
pygame.quit()
sys.exit()
This is a basic Pygame program that creates a simple game where the player can move left and right and jump. The player is affected by gravity and cannot jump while in the air. The game ends when the user closes the window.
The Code Together
import pygame
import sys
# Initialize Pygame
pygame.init()
# Set up some constants
WIDTH, HEIGHT = 640, 480 # Window size
SPEED = 6 # Player speed
GRAVITY = 0.5 # Gravity force
JUMP_FORCE = 10 # Jump force
FPS = 30 # Frames per second
# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# Set up the player
player = pygame.Rect(WIDTH / 2, HEIGHT / 2, 50, 50) # Player size and position
player_speed = pygame.Vector2(0, 0) # Player speed
# Set up the clock
clock = pygame.time.Clock()
# Set up the game loop
running = True
jumping = False
while running:
# Fill the screen with black
screen.fill((0, 0, 0))
# Draw the player
pygame.draw.rect(screen, (255, 255, 255), player)
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT: # Quit event
running = False
elif event.type == pygame.KEYDOWN: # Key press event
if event.key == pygame.K_SPACE and player.bottom >= HEIGHT: # Jump event
jumping = True
# Player movement
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]: # Move left
player_speed.x = -SPEED
elif keys[pygame.K_RIGHT]: # Move right
player_speed.x = SPEED
else: # Stay still
player_speed.x = 0
# Jumping
if jumping:
player_speed.y = -JUMP_FORCE # Apply jump force
jumping = False
# Apply gravity
player_speed.y += GRAVITY
# Move the player
player.move_ip(player_speed)
# Ensure the player stays on the screen
if player.bottom > HEIGHT:
player.bottom = HEIGHT
player_speed.y = 0
# Flip the display
pygame.display.flip()
# Cap the frame rate
clock.tick(FPS)
# Quit Pygame
pygame.quit()
sys.exit()