To create a simple platformer game in Python using Pygame, follow these steps:
-
Install Pygame: Make sure you have Python and Pygame installed. You can install Pygame using pip install pygame.
-
Set Up the Game Window: Initialize Pygame and create a game window with a specified width and height.
-
Create the Player Character: Represent the player as a rectangle with specific dimensions and initial position.
-
Handle Player Movement: Use keyboard inputs to move the player left and right, and implement jumping mechanics.
-
Add Platforms: Create platforms as rectangles that the player can stand on.
-
Implement Gravity and Collision Detection: Apply gravity to make the player fall and implement collision detection to make the player stand on platforms.
-
Game Loop: Continuously update the game state and render the game elements.
Here is the complete code for a simple platformer game:
import pygame
# Initialize Pygame
pygame.init()
# Set up the display
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Simple Platformer")
# Player settings
player_width = 50
player_height = 50
player_x = 50
player_y = screen_height - player_height - 50
player_speed = 5
player_jump = False
jump_count = 10
player_y_velocity = 0
gravity = 1
# Player rectangle
player = pygame.Rect(player_x, player_y, player_width, player_height)
# Platform settings
platforms = [
pygame.Rect(0, screen_height - 50, screen_width, 50), # Ground
pygame.Rect(100, 400, 200, 20), # Platform 1
pygame.Rect(400, 300, 200, 20) # Platform 2
]
# Game loop
clock = pygame.time.Clock()
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Get the state of all keyboard keys
keys = pygame.key.get_pressed()
# Move left and right
if keys[pygame.K_LEFT]:
player.x -= player_speed
if keys[pygame.K_RIGHT]:
player.x += player_speed
# Apply gravity
player_y_velocity += gravity
player.y += player_y_velocity
# Check for collisions with platforms
for platform in platforms:
if player.colliderect(platform) and player_y_velocity > 0:
player.bottom = platform.top
player_y_velocity = 0
player_jump = False
jump_count = 10
# Jumping logic
if not player_jump:
if keys[pygame.K_SPACE]:
player_jump = True
player_y_velocity = -15 # Jump force
else:
if jump_count >= -10:
neg = 1
if jump_count < 0:
neg = -1
player.y -= (jump_count ** 2) * 0.5 * neg
jump_count -= 1
else:
player_jump = False
jump_count = 10
# Fill the screen with a color (e.g., white)
screen.fill((255, 255, 255))
# Draw the platforms
for platform in platforms:
pygame.draw.rect(screen, (0, 0, 0), platform)
# Draw the player
pygame.draw.rect(screen, (0, 0, 255), player)
# Update the display
pygame.display.flip()
# Cap the frame rate
clock.tick(60)
# Quit Pygame
pygame.quit()
This code creates a simple platformer game where you can move a blue rectangle (the player) around and jump on platforms. You can expand on this by adding more features like collectibles, enemies, and better graphics.