We all love to play platformer games. Have you ever wondered if creating your own pygame is a type of game where the player controls a character that jumps between platforms, avoids obstacles, and possibly collects items. Some popular examples of platformer games are Mario and Sonic.
Let’s learn how to create a basic platformer in Python using Pygame step-by-step:
Step 1: Setting Up the Game
Just like any game, the first step is to install a software or application that supports the data. Now, in the 2D game, the first step is to install Pygame and set up the game window.
import pygame pygame.init() # Create the game window screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption(“Platformer Game”) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False screen.fill((135, 206, 235)) # Sky blue background pygame.display.update() pygame.quit() |
This code creates a window with a blue background, like a sky, for our platformer.
Step 2: Creating the Player
Next, we’ll add a player character that can jump between platforms.
player_x = 100 player_y = 500 player_speed = 5 player_jump = –15 gravity = 1 velocity_y = 0 is_jumping = False while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Player movement keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player_x -= player_speed if keys[pygame.K_RIGHT]: player_x += player_speed # Jumping logic if not is_jumping and keys[pygame.K_SPACE]: velocity_y = player_jump is_jumping = True # Apply gravity velocity_y += gravity player_y += velocity_y # Stop falling when reaching the ground if player_y >= 500: player_y = 500 velocity_y = 0 is_jumping = False # Draw the player (a simple rectangle) screen.fill((135, 206, 235)) pygame.draw.rect(screen, (255, 0, 0), (player_x, player_y, 50, 50)) pygame.display.update() |
Step 3: Adding Platforms
To make it more interactive and engaging we add more platforms. Now, let’s add platforms for a more engaging experience, this is how you do it:
platforms = [(0, 550, 800, 50), (150, 400, 100, 20), (400, 300, 150, 20)] while running: # Draw platforms for platform in platforms: pygame.draw.rect(screen, (0, 255, 0), platform) # Check for collision with platforms (simple vertical collision) for platform in platforms: if player_y + 50 > platform[1] and player_y + 50 < platform[1] + 10 and player_x + 50 > platform[0] and player_x < platform[0] + platform[2]: player_y = platform[1] – 50 velocity_y = 0 is_jumping = False pygame.display.update() |
Step 4: Adding Enemy Obstacles
Add an enemy or an obstacles to make the game more challenging. Here’s a simple block that moves horizontally.
enemy_x = 300 enemy_speed = 2 while running: # Move the enemy enemy_x += enemy_speed if enemy_x > 750 or enemy_x < 0: enemy_speed *= -1 # Check for player-enemy collision if player_x + 50 > enemy_x and player_x < enemy_x + 50 and player_y + 50 > 500: print(“Game Over”) running = False # Draw enemy pygame.draw.rect(screen, (255, 0, 0), (enemy_x, 500, 50, 50)) pygame.display.update() |
Conclusion
You’ve now created a basic platformer game in Python! Your player can move, jump, and avoid enemies. With more practice, you can add collectibles, power-ups, or more complex levels to your game. Creating platformers in Python is a fun way to explore programming and game design, and the skills you learn here can help you create even more amazing games!