KEMBAR78
Catch Falling Object | PDF
0% found this document useful (0 votes)
260 views2 pages

Catch Falling Object

This document is a Python script that implements a simple game using Pygame where players control a blue rectangle to catch falling red objects. The game tracks the player's score, which increases with each successful catch, while the falling object's speed increases over time. The game runs in a loop until the player closes the window.

Uploaded by

plexg30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
260 views2 pages

Catch Falling Object

This document is a Python script that implements a simple game using Pygame where players control a blue rectangle to catch falling red objects. The game tracks the player's score, which increases with each successful catch, while the falling object's speed increases over time. The game runs in a loop until the player closes the window.

Uploaded by

plexg30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import pygame

import random
import sys

# Initialize Pygame
pygame.init()

# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Catch the Falling Objects")

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

# Clock
clock = pygame.time.Clock()
FPS = 60

# Player settings
player_width, player_height = 100, 20
player_x = WIDTH // 2 - player_width // 2
player_y = HEIGHT - player_height - 10
player_speed = 10

# Falling object settings


object_width, object_height = 30, 30
object_x = random.randint(0, WIDTH - object_width)
object_y = -object_height
object_speed = 5

# Score
score = 0
font = pygame.font.Font(None, 36)

# Game loop
def main():
global player_x, object_x, object_y, object_speed, score
running = True

while running:
screen.fill(BLACK)

# Event handling
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] and player_x > 0:
player_x -= player_speed
if keys[pygame.K_RIGHT] and player_x < WIDTH - player_width:
player_x += player_speed

# Update object position


object_y += object_speed

# Check for collision


if (
object_y + object_height > player_y
and object_x + object_width > player_x
and object_x < player_x + player_width
):
score += 1
object_y = -object_height
object_x = random.randint(0, WIDTH - object_width)
object_speed += 0.5 # Increase difficulty

# Reset object if it falls off screen


if object_y > HEIGHT:
object_y = -object_height
object_x = random.randint(0, WIDTH - object_width)

# Draw player
pygame.draw.rect(screen, BLUE, (player_x, player_y, player_width,
player_height))

# Draw falling object


pygame.draw.rect(screen, RED, (object_x, object_y, object_width,
object_height))

# Draw score
score_text = font.render(f"Score: {score}", True, WHITE)
screen.blit(score_text, (10, 10))

# Update display
pygame.display.flip()

# Cap the frame rate


clock.tick(FPS)

pygame.quit()
sys.exit()

# Run the game


if __name__ == "__main__":
main()

You might also like