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

Document 12a

Uploaded by

mohanraje2024
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 views3 pages

Document 12a

Uploaded by

mohanraje2024
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/ 3

Import pygame

Import sys

# Initialize Pygame

Pygame.init()

# Set up display

Screen_width, screen_height = 600, 600

Screen = pygame.display.set_mode((screen_width, screen_height))

Pygame.display.set_caption(“Bouncing Ball Simulation”)

# Define colors

BLACK = (0, 0, 0)

WHITE = (255, 255, 255)

RED = (255, 0, 0)

# Ball properties

Ball_radius = 20

Ball_x, ball_y = screen_width // 2, screen_height // 2 # Start in the center

Ball_speed_x, ball_speed_y = 5, 5 # Speed in x and y directions

# Main game loop

Running = True

While running:

# Handle events

For event in pygame.event.get():

If event.type == pygame.QUIT:
Running = False

# Move the ball

Ball_x += ball_speed_x

Ball_y += ball_speed_y

# Bounce the ball off the walls

If ball_x – ball_radius <= 0 or ball_x + ball_radius >= screen_width:

Ball_speed_x = -ball_speed_x # Reverse x direction

If ball_y – ball_radius <= 0 or ball_y + ball_radius >= screen_height:

Ball_speed_y = -ball_speed_y # Reverse y direction

# Fill the screen with black

Screen.fill(BLACK)

# Draw the ball

Pygame.draw.circle(screen, RED, (ball_x, ball_y), ball_radius)

# Update the display

Pygame.display.flip()

# Frame rate (60 FPS)

Pygame.time.Clock().tick(60)

# Quit Pygame

Pygame.quit()
Sys.exit()

You might also like