0% found this document useful (0 votes)
7 views4 pages

Document Game

This document is a Python script using Pygame to create a simple game where a player can hit a ball towards a person on a platform. The game includes mechanics for ball movement, collision detection, and the person falling into water when hit. The main game loop handles events, updates positions, and renders the game elements on the screen.

Uploaded by

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

Document Game

This document is a Python script using Pygame to create a simple game where a player can hit a ball towards a person on a platform. The game includes mechanics for ball movement, collision detection, and the person falling into water when hit. The main game loop handles events, updates positions, and renders the game elements on the screen.

Uploaded by

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

import pygame

import sys

# Initialize Pygame

pygame.init()

# Screen dimensions

SCREEN_WIDTH = 800

SCREEN_HEIGHT = 600

# Colors

WHITE = (255, 255, 255)

BLUE = (0, 0, 255)

RED = (255, 0, 0)

GREEN = (0, 255, 0)

# Initialize screen

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

pygame.display.set_caption("Hit the Ball and Drop the Person!")

# Clock for controlling frame rate

clock = pygame.time.Clock()

# Ball properties

ball = pygame.Rect(100, 500, 20, 20)

ball_speed_x = 7

# Target properties (person on a platform)

person = pygame.Rect(700, 300, 50, 50)


platform = pygame.Rect(680, 350, 100, 20)

# Water properties

water = pygame.Rect(0, SCREEN_HEIGHT - 50, SCREEN_WIDTH, 50)

# Game state

ball_moving = False

person_falling = False

# Font for text

font = pygame.font.Font(None, 36)

def reset_game():

global ball, ball_moving, person, person_falling

ball = pygame.Rect(100, 500, 20, 20)

ball_moving = False

person = pygame.Rect(700, 300, 50, 50)

person_falling = False

# Main game loop

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit()

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_SPACE and not ball_moving:

ball_moving = True
# Update ball movement

if ball_moving:

ball.x += ball_speed_x

if ball.x > SCREEN_WIDTH: # Reset if it goes off-screen

reset_game()

# Check collision between ball and person

if ball.colliderect(person):

person_falling = True

ball_moving = False

# Update person falling

if person_falling:

person.y += 5

if person.y > SCREEN_HEIGHT - 50: # Stop at the water

person_falling = False

# Draw everything

screen.fill(WHITE) # Clear screen

pygame.draw.rect(screen, BLUE, water) # Draw water

pygame.draw.rect(screen, RED, ball) # Draw ball

pygame.draw.rect(screen, GREEN, person) # Draw person

pygame.draw.rect(screen, (0, 0, 0), platform) # Draw platform

# Display instructions

text = font.render("Press SPACE to hit the ball!", True, (0, 0, 0))

screen.blit(text, (10, 10))

# Update display
pygame.display.flip()

clock.tick(60)

You might also like