In this game, you are a small square jumping to avoid obstacles, as you avoid more, the levels get harder.

To view an example, click HERE.

In order to play the game, copy and paste the code provided below into your Python file, then run the code and enjoy!

Copy the code for FREE!:

import pygame
import sys
import random

# Initialize Pygame
pygame.init()

# Set up the game window
WIDTH, HEIGHT = 800, 600
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pixel Painter")

# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)

# Define player properties
player_width, player_height = 50, 50
player_x, player_y = 50, HEIGHT - player_height - 50
player_vel = 5
is_jump = False
jump_count = 10

# Define obstacle properties
obstacle_width, obstacle_height = 50, 50
obstacle_vel = 3
obstacles = []

# Define collectible properties
collectible_width, collectible_height = 30, 30
collectibles = []

# Define game variables
level = 1
score = 0

# Function to generate obstacles
def generate_obstacles():
    x = WIDTH + random.randint(100, 300)
    y = HEIGHT - obstacle_height - 50
    obstacles.append(pygame.Rect(x, y, obstacle_width, obstacle_height))

# Function to generate collectibles
def generate_collectibles():
    x = WIDTH + random.randint(100, 300)
    y = random.randint(100, HEIGHT - collectible_height - 100)
    collectibles.append(pygame.Rect(x, y, collectible_width, collectible_height))

# Main game loop
def main():
    global player_x, player_y, is_jump, jump_count, level, score

    clock = pygame.time.Clock()
    running = True
    while running:
        clock.tick(60)

        # Event handling
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        # Player movement
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and player_x > 0:
            player_x -= player_vel
        if keys[pygame.K_RIGHT] and player_x < WIDTH - player_width:
            player_x += player_vel
        if not is_jump:
            if keys[pygame.K_SPACE]:
                is_jump = True
        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:
                is_jump = False
                jump_count = 10

        # Generate obstacles and collectibles
        if len(obstacles) == 0:
            for _ in range(level * 2):
                generate_obstacles()
        if len(collectibles) == 0:
            for _ in range(level * 2):
                generate_collectibles()

        # Move obstacles
        for obstacle in obstacles:
            obstacle.x -= obstacle_vel

            # Collision detection with player
            if obstacle.colliderect((player_x, player_y, player_width, player_height)):
                pygame.quit()
                sys.exit()

            # Remove obstacle if it goes off-screen
            if obstacle.right < 0:
                obstacles.remove(obstacle)
                score += 10

        # Move collectibles
        for collectible in collectibles:
            collectible.x -= obstacle_vel

            # Collision detection with player
            if collectible.colliderect((player_x, player_y, player_width, player_height)):
                collectibles.remove(collectible)
                score += 50

        # Draw everything
        win.fill(WHITE)
        pygame.draw.rect(win, BLACK, (player_x, player_y, player_width, player_height))
        for obstacle in obstacles:
            pygame.draw.rect(win, RED, obstacle)
        for collectible in collectibles:
            pygame.draw.rect(win, YELLOW, collectible)
        pygame.display.update()

        # Increase level
        if score >= level * 200:
            level += 1

if __name__ == "__main__":
    main()

Leave a Reply

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