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)
# 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 # Increased radius
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()
self.rect.x = x - self.radius # Corrected: Position based on radius
self.rect.y = y - (self.radius*2) # Corrected: Position based on radius
self.y_speed = -1.025 # Increased speed
self.x_speed = random.uniform(-1, 1) # More varied horizontal drift
self.scale_factor = 1.0 #start scaling factor at 1
def update(self):
# Use int() to convert the speed to an integer before adding to the rect
self.rect.y += int(self.y_speed)
self.rect.x += int(self.x_speed)
# Simulate floating away and getting smaller
self.y_speed *= 0.99 # 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.scale_factor < 0.1: # Remove when too small
self.kill()
else: #makes sure the balloon updates after scaling
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.image = pygame.transform.scale(self.image, (self.radius*2, self.radius*3)) #actually scales the ballon
self.rect = self.image.get_rect()
self.rect.center = (self.rect.centerx + self.x_speed, self.rect.centery + self.y_speed)
# Circle class for scoring
class ScoringCircle(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.radius = random.randint(30, 70) # Random radius
self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
self.image = pygame.Surface([self.radius * 2, self.radius * 2], pygame.SRCALPHA) # Use SRCALPHA for transparency
pygame.draw.circle(self.image, self.color, (self.radius, self.radius), self.radius)
self.rect = self.image.get_rect()
self.rect.x = random.randint(100, WIDTH - 100)
self.rect.y = random.randint(100, HEIGHT - 200) # Keep it above the ground
def update(self):
# Move the circle randomly
self.rect.x += random.randint(-2, 2)
self.rect.y += random.randint(-2, 2)
# Keep the circle within the screen bounds
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > WIDTH:
self.rect.right = WIDTH
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > HEIGHT:
self.rect.bottom = HEIGHT
def reset(self):
# Change position, size, and color
self.radius = random.randint(30, 70)
self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
self.image = pygame.Surface([self.radius * 2, self.radius * 2], pygame.SRCALPHA)
pygame.draw.circle(self.image, self.color, (self.radius, self.radius), self.radius)
self.rect = self.image.get_rect()
self.rect.x = random.randint(100, WIDTH - 100)
self.rect.y = random.randint(100, HEIGHT - 200)
# Sprite groups
all_sprites = pygame.sprite.Group()
balloons = pygame.sprite.Group()
scoring_circles = pygame.sprite.Group()
# Scoring
score = 0
font = pygame.font.Font(None, 36)
# Create scoring circles
for _ in range(3): # Create 3 circles
circle = ScoringCircle()
all_sprites.add(circle)
scoring_circles.add(circle)
# 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 ground level
all_sprites.add(balloon)
balloons.add(balloon)
# Update
all_sprites.update()
# Check for collisions between balloons and scoring circles
collisions = pygame.sprite.groupcollide(balloons, scoring_circles, True, True)
for collision in collisions:
score += 1
# Reset the scoring circle
for circle in scoring_circles:
circle.reset()
# Draw
screen.fill(BLUE) # Sky
pygame.draw.rect(screen, GREEN, (0, HEIGHT - 50, WIDTH, 50)) # Ground
all_sprites.draw(screen) # Draw all sprites
# Display the score
score_text = font.render("Score: " + str(score), True, BLACK)
screen.blit(score_text, (10, 10))
# Update the display
pygame.display.flip()
# Limit frame rate
clock.tick(60)
pygame.quit()