0% found this document useful (0 votes)
3 views2 pages

Write A Program About Bubbles

Uploaded by

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

Write A Program About Bubbles

Uploaded by

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

Here's a Python program that simulates bouncing bubbles on the screen:

import pygame
import random

# Define some colors


BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# Define the screen size


WIDTH = 800
HEIGHT = 600

# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Bouncing Bubbles")

# Define a class for the bubble object


class Bubble:
def __init__(self, x, y, radius, color):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.dx = random.randint(-3, 3) # Random horizontal speed
self.dy = random.randint(-3, 3) # Random vertical speed

def update(self):
self.x += self.dx
self.y += self.dy

# Check for wall collisions and reverse direction


if self.x + self.radius > WIDTH or self.x - self.radius < 0:
self.dx = -self.dx
if self.y + self.radius > HEIGHT or self.y - self.radius < 0:
self.dy = -self.dy

def draw(self, screen):


pygame.draw.circle(screen, self.color, (self.x, self.y),
self.radius)

# Create a list of bubbles


bubbles = []
for _ in range(20): # Create 20 bubbles
x = random.randint(0, WIDTH)
y = random.randint(0, HEIGHT)
radius = random.randint(10, 30)
color = (random.randint(0, 255), random.randint(0, 255),
random.randint(0, 255))
bubbles.append(Bubble(x, y, radius, color))

# Main loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# Update and draw the bubbles


screen.fill(BLACK)
for bubble in bubbles:
bubble.update()
bubble.draw(screen)

pygame.display.flip()

# Quit Pygame
pygame.quit()

This program creates a bunch of bubbles with random sizes, colors, and speeds. The bubbles
bounce around the screen, changing direction when they hit the edges.
● https://github.com/tcsfremont/curriculum
● https://github.com/Arunkumar060/Bouncing-ball-using-pygame

You might also like