Here's an updated version of the code that includes the changes you requested:
import pygame
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
WIDTH = 800
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Hot Air Balloon Launcher")
# Colors
BLUE = (135, 206, 235) # Sky Blue
GREEN = (0, 128, 0) # Green for the ground
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
# Balloon class
class Balloon(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
self.radius = 65
self.image = pygame.Surface([self.radius*2, self.radius*3])
self.image.fill(WHITE) #makes the area around the balloon transparant
self.image.set_colorkey(WHITE)
pygame.draw.ellipse(self.image, self.color, [0,0,self.radius*2,self.radius*2]) #draws the balloon
pygame.draw.polygon(self.image, BLACK, [[(self.radius*.8),self.radius*2], [(self.radius*1.2),self.radius*2], [self.radius*1.2, self.radius*2.5], [(self.radius*.8),self.radius*2.5]]) #draws the basket
self.rect = self.image.get_rect(center=(x, y)) #sets the center of the balloon to the launch position
self.y_speed = -2 # Initial upward speed
self.x_speed = random.choice([-1, 1]) * random.uniform(0.1, 0.5) # Slight horizontal drift
self.scale_factor = 1.0 #start scaling factor at 1
def update(self):
self.rect.y += self.y_speed
self.rect.x += self.x_speed
# Simulate floating away and getting smaller
self.y_speed *= 1.025 # Gradual slowdown
self.x_speed *= 0.99
self.scale_factor *= 0.999 #makes the balloons smaller
self.radius *= self.scale_factor #the radius needs to be scaled to
if self.radius < 1 or self.scale_factor < 0.1: # Remove when too small
self.kill()
else: #makes sure the balloon updates after scaling
self.image = pygame.Surface([int(self.radius*2), int(self.radius*3)])
self.image.fill(WHITE) #makes the area around the balloon transparant
self.image.set_colorkey(WHITE)
pygame.draw.ellipse(self.image, self.color, [0,0,int(self.radius*2),int(self.radius*2)]) #draws the balloon
pygame.draw.polygon(self.image, BLACK, [[(self.radius*.8),self.radius*2], [(self.radius*1.2),self.radius*2], [self.radius*1.2, self.radius*2.5], [(self.radius*.8),self.radius*2.5]]) #draws the basket
new_rect = self.image.get_rect(center=self.rect.center)
self.rect = new_rect
# Target class
class Target(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.radius = random.randint(20, 50)
self.x = random.randint(self.radius, WIDTH - self.radius)
self.y = random.randint(100, HEIGHT // 2)
self.image = pygame.Surface([self.radius*2, self.radius*2])
self.image.set_colorkey(WHITE)
self.image.fill(WHITE)
pygame.draw.ellipse(self.image, RED, [0,0,self.radius*2,self.radius*2])
self.rect = self.image.get_rect(center=(self.x, self.y))
def update(self):
pass
# Sprite groups
all_sprites = pygame.sprite.Group()
balloons = pygame.sprite.Group()
targets = pygame.sprite.Group()
# Create a target
target = Target()
targets.add(target)
all_sprites.add(target)
# Score
score = 0
font = pygame.font.Font(None, 36)
# Game loop
running = True
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # Left mouse button
# Launch a balloon at the mouse click position
x, y = event.pos
balloon = Balloon(x, HEIGHT - 50) #launch from the ground level
all_sprites.add(balloon)
balloons.add(balloon)
# Update
all_sprites.update()
# Check for collisions with the target
for balloon in balloons:
if balloon.rect.colliderect(target.rect):
score += 1
balloon.kill()
# Draw
screen.fill(BLUE) # Sky
pygame.draw.rect(screen, GREEN, (0, HEIGHT - 50, WIDTH, 50)) # Ground
all_sprites.draw(screen) # Draw all sprites
# Draw the score
score_text = font.render(f'Score: {score}', True, BLACK)
screen.blit(score_text, (10, 10))
# Update the display
pygame.display.flip()
# Limit frame rate
clock.tick(60)
pygame.quit()