In this game, you are a small square and you jump on platforms and avoid obsticles to collect as many coins as you can!

Click here to view an example!

To play the game, copy and paste the game below into your python file. Run the code and enjoy!

Copy the code for FREE!:

import pygame
import sys
import random

# Initialize Pygame
pygame.init()

# Set up the screen
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Platformer Game")

# Set up colors
background_color = (135, 206, 235)
player_color = (0, 128, 255)
platform_color = (139, 69, 19)
coin_color = (255, 215, 0)
enemy_color = (255, 0, 0)

# Set up platforms
platforms = [
    pygame.Rect(100, 500, 200, 20),
    pygame.Rect(400, 400, 200, 20),
    pygame.Rect(150, 300, 200, 20),
    pygame.Rect(500, 200, 200, 20)
]

# Moving platforms
moving_platforms = [
    {"rect": pygame.Rect(300, 100, 200, 20), "dir": 1, "axis": "y", "speed": 2, "range": [100, 300]},
    {"rect": pygame.Rect(100, 250, 200, 20), "dir": 1, "axis": "x", "speed": 2, "range": [100, 600]}
]

# Set up coins
coins = [
    pygame.Rect(150, 460, 20, 20),
    pygame.Rect(450, 360, 20, 20),
    pygame.Rect(200, 260, 20, 20),
    pygame.Rect(550, 160, 20, 20)
]

# Set up enemies
enemies = [
    pygame.Rect(300, 480, 20, 20),
    pygame.Rect(500, 380, 20, 20),
    pygame.Rect(100, 280, 20, 20),
    pygame.Rect(400, 180, 20, 20)
]

# Set up the player
player_size = 50
initial_platform = platforms[0]
player_x = initial_platform.x + (initial_platform.width - player_size) // 2
player_y = initial_platform.y - player_size
player_speed = 7
player_jump = 15
player_velocity = 0
gravity = 1
is_jumping = False

# Set up the score and level
score = 0
level = 1
max_coins = len(coins)
font = pygame.font.SysFont(None, 35)

def draw_text(text, font, color, surface, x, y):
    textobj = font.render(text, True, color)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    surface.blit(textobj, textrect)

def game_over():
    my_font = pygame.font.SysFont(None, 50)
    game_over_surface = my_font.render('Game Over! Your Score is: ' + str(score), True, (255, 255, 255))
    game_over_rect = game_over_surface.get_rect()
    game_over_rect.midtop = (screen_width / 2, screen_height / 4)
    screen.blit(game_over_surface, game_over_rect)
    pygame.display.flip()
    pygame.time.wait(2000)
    pygame.quit()
    sys.exit()

def move_platforms():
    for platform in moving_platforms:
        if platform["axis"] == "y":
            platform["rect"].y += platform["speed"] * platform["dir"]
            if platform["rect"].y <= platform["range"][0] or platform["rect"].y >= platform["range"][1]:
                platform["dir"] *= -1
        elif platform["axis"] == "x":
            platform["rect"].x += platform["speed"] * platform["dir"]
            if platform["rect"].x <= platform["range"][0] or platform["rect"].x >= platform["range"][1]:
                platform["dir"] *= -1

# Main game loop
running = True
clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Get keys pressed
    keys = pygame.key.get_pressed()

    # Update player position
    if keys[pygame.K_LEFT]:
        player_x -= player_speed
    if keys[pygame.K_RIGHT]:
        player_x += player_speed

    # Jumping
    if keys[pygame.K_SPACE] and not is_jumping:
        is_jumping = True
        player_velocity = -player_jump

    # Apply gravity
    player_velocity += gravity
    player_y += player_velocity

    # Prevent the player from moving off screen
    player_x = max(0, min(player_x, screen_width - player_size))

    # Check if player is on a platform
    on_platform = False
    for platform in platforms + [p["rect"] for p in moving_platforms]:
        if pygame.Rect(player_x, player_y + player_size, player_size, player_velocity).colliderect(platform):
            player_y = platform.y - player_size
            player_velocity = 0
            on_platform = True
            is_jumping = False

    # If the player is on the ground, they are not jumping
    if player_y + player_size >= screen_height:
        player_y = screen_height - player_size
        player_velocity = 0
        is_jumping = False
        on_platform = True

    # Check for game over condition (falling off the screen)
    if player_y > screen_height:
        game_over()

    # Collect coins
    for coin in coins[:]:
        if pygame.Rect(player_x, player_y, player_size, player_size).colliderect(coin):
            coins.remove(coin)
            score += 1

    # Check for enemy collision
    for enemy in enemies:
        if pygame.Rect(player_x, player_y, player_size, player_size).colliderect(enemy):
            game_over()

    # Move platforms
    move_platforms()

    # Check if all coins are collected
    if len(coins) == 0:
        level += 1
        max_coins += 2  # Increase the number of coins in the next level
        coins = [
            pygame.Rect(random.randint(50, screen_width - 50), random.randint(50, screen_height - 200), 20, 20)
            for _ in range(max_coins)
        ]
        enemies.append(pygame.Rect(random.randint(50, screen_width - 50), random.randint(50, screen_height - 200), 20, 20))

    # Fill the screen with background color
    screen.fill(background_color)

    # Draw the player
    pygame.draw.rect(screen, player_color, pygame.Rect(player_x, player_y, player_size, player_size))

    # Draw the platforms
    for platform in platforms:
        pygame.draw.rect(screen, platform_color, platform)

    # Draw the moving platforms
    for platform in moving_platforms:
        pygame.draw.rect(screen, platform_color, platform["rect"])

    # Draw the coins
    for coin in coins:
        pygame.draw.rect(screen, coin_color, coin)

    # Draw the enemies
    for enemy in enemies:
        pygame.draw.rect(screen, enemy_color, enemy)

    # Draw the score and level
    draw_text(f'Score: {score}', font, (255, 255, 255), screen, 10, 10)
    draw_text(f'Level: {level}', font, (255, 255, 255), screen, 10, 45)

    # Update the display
    pygame.display.flip()

    # Cap the frame rate
    clock.tick(60)

# Quit Pygame
pygame.quit()
sys.exit()

Leave a Reply

Your email address will not be published. Required fields are marked *