Sitemap
Krapendra Kumar

Krapendra Kumar

1 min readMar 7, 2023

--

import pygame
import random

# Initialize Pygame
pygame.init()

# Set up the game window
width = 640
height = 480
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Bubble Shooter')

# Set up game variables
clock = pygame.time.Clock()
font = pygame.font.SysFont('Arial', 30)
score = 0
game_over = False

# Set up bubble variables
bubble_radius = 20
bubble_speed = 5
bubble_color = (0, 255, 255)
bubbles = []
for i in range(10):
x = random.randint(bubble_radius, width - bubble_radius)
y = random.randint(bubble_radius, height - bubble_radius)
bubbles.append(pygame.Rect(x, y, bubble_radius * 2, bubble_radius * 2))

Get Krapendra Kumar’s stories in your inbox

Join Medium for free to get updates from this writer.

# Game loop
while not game_over:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.MOUSEBUTTONDOWN:
# Check if player clicked on a bubble
pos = pygame.mouse.get_pos()
for bubble in bubbles:
if bubble.collidepoint(pos):
bubbles.remove(bubble)
score += 10

# Move bubbles
for bubble in bubbles:
bubble.move_ip(0, bubble_speed)

# Check for collisions
for bubble in bubbles:
if bubble.bottom >= height:
game_over = True

# Draw the game
screen.fill((0, 0, 0))
for bubble in bubbles:
pygame.draw.circle(screen, bubble_color, bubble.center, bubble_radius)
score_text = font.render('Score: ' + str(score), True, (255, 255, 255))
screen.blit(score_text, (10, 10))
pygame.display.flip()

# Set the frame rate
clock.tick(60)

# Clean up Pygame
pygame.quit()

--

--