In this game, you use the arrow keys to collect red blocks and avoid the other blocks, your goal is to get the highest score you could.

Block collector demonstration

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

Copy the code for FREE!:

import pygame
import sys
import random

# Initialize Pygame
pygame.init()

# Set up the display
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Block Collector")

# Set up colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)

# Set up the player character
player_width, player_height = 50, 50
player_x = width // 2 - player_width // 2
player_y = height - 2 * player_height
player_speed = 5

# Set up the falling objects
object_width, object_height = 30, 30
object_speed = 5
objects = []

# Set up the obstacles
obstacle_width, obstacle_height = 50, 50
obstacle_speed = 5
obstacles = []

# Set up the score
score = 0
font = pygame.font.Font(None, 36)

# Set up the clock to control the frame rate
clock = pygame.time.Clock()

# Function to spawn falling objects
def spawn_objects():
    x = random.randint(0, width - object_width)
    y = 0
    objects.append([x, y])

# Function to spawn obstacles
def spawn_obstacles():
    x = random.randint(0, width - obstacle_width)
    y = 0
    obstacles.append([x, y])

# Main game loop
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and player_x > 0:
        player_x -= player_speed
    if keys[pygame.K_RIGHT] and player_x < width - player_width:
        player_x += player_speed

    # Spawn falling objects
    if random.randint(0, 100) < 5:  # Adjust the frequency of object spawns
        spawn_objects()

    # Spawn obstacles
    if random.randint(0, 100) < 2:  # Adjust the frequency of obstacle spawns
        spawn_obstacles()

    # Move and remove falling objects
    for obj in objects[:]:
        obj[1] += object_speed
        if obj[1] > height:
            objects.remove(obj)

    # Move and remove obstacles
    for obs in obstacles[:]:
        obs[1] += obstacle_speed
        if obs[1] > height:
            obstacles.remove(obs)

    # Check for collisions with falling objects
    for obj in objects[:]:
        if (
            player_x < obj[0] + object_width
            and player_x + player_width > obj[0]
            and player_y < obj[1] + object_height
            and player_y + player_height > obj[1]
        ):
            objects.remove(obj)
            score += 1

    # Check for collisions with obstacles
    for obs in obstacles[:]:
        if (
            player_x < obs[0] + obstacle_width
            and player_x + player_width > obs[0]
            and player_y < obs[1] + obstacle_height
            and player_y + player_height > obs[1]
        ):
            pygame.quit()
            sys.exit()

    # Update the display
    screen.fill(white)
    pygame.draw.rect(screen, black, (player_x, player_y, player_width, player_height))

    for obj in objects:
        pygame.draw.rect(screen, red, (obj[0], obj[1], object_width, object_height))

    for obs in obstacles:
        pygame.draw.rect(screen, black, (obs[0], obs[1], obstacle_width, obstacle_height))

    # Display score
    score_text = font.render("Score: {}".format(score), True, black)
    screen.blit(score_text, (10, 10))

    pygame.display.flip()

    # Cap the frame rate
    clock.tick(60)