import pygame
import random
# Initialize Pygame
pygame.init()
# Set the dimensions of the window
WIDTH, HEIGHT = 800, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Splitting Ball Game")
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255)] # List of different colors
# Ball properties
BALL_RADIUS = 10
BALL_SPEED = 5
# Define ball class
class Ball:
def __init__(self, x, y, vel_x, vel_y, color):
self.x = x
self.y = y
self.vel_x = vel_x
self.vel_y = vel_y
self.color = color
def draw(self):
pygame.draw.circle(WIN, self.color, (self.x, self.y), BALL_RADIUS)
# List to hold balls
balls = []
# Flag to track if a ball has been split
ball_split = False
# Function to draw text on the screen
def draw_text(text, font, color, x, y):
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.center = (x, y)
WIN.blit(text_surface, text_rect)
# Function to draw restart button
def draw_restart_button():
restart_font = pygame.font.SysFont(None, 30)
restart_text = restart_font.render("RESTART", True, WHITE)
restart_rect = restart_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 50))
pygame.draw.rect(WIN, BLACK, restart_rect)
WIN.blit(restart_text, restart_rect)
# Main menu screen
def main_menu():
menu_font = pygame.font.SysFont(None, 50)
instructions = True
while instructions:
WIN.fill(BLACK)
draw_text("Welcome to Splitting Ball Game till it reaches 50", menu_font, WHITE, WIDTH // 2, HEIGHT // 3)
draw_text("Click START to begin", menu_font, WHITE, WIDTH // 2, HEIGHT // 2)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
instructions = False
main_game() # Start the game when the player clicks the start button
# Main game loop
def main_game():
global ball_split, balls
balls = [Ball(WIDTH // 2, HEIGHT // 2, random.randint(-BALL_SPEED, BALL_SPEED),
random.randint(-BALL_SPEED, BALL_SPEED), random.choice(COLORS))] # Start with one ball at the center
# Flag to track if the player has won
game_won = False
while not game_won:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
# Fill the window with a background color
WIN.fill(BLACK)
# Display ball count
draw_text(f"Balls: {len(balls)}", pygame.font.SysFont(None, 30), WHITE, WIDTH // 2, HEIGHT - 30)
# Move and draw the balls
for ball in balls:
ball.x += ball.vel_x
ball.y += ball.vel_y
# Bounce off the walls
if ball.x - BALL_RADIUS <= 0 or ball.x + BALL_RADIUS >= WIDTH:
ball.vel_x = -ball.vel_x
if ball.y - BALL_RADIUS <= 0 or ball.y + BALL_RADIUS >= HEIGHT:
ball.vel_y = -ball.vel_y
ball.draw()
# Get mouse position
mouse_x, mouse_y = pygame.mouse.get_pos()
# Check if the mouse clicks a ball
click = pygame.mouse.get_pressed()[0] # Get the left mouse button state
if click == 1 and not ball_split:
for ball in balls.copy(): # Using copy to iterate over a mutable list
distance = ((ball.x - mouse_x) ** 2 + (ball.y - mouse_y) ** 2) ** 0.5
if distance < BALL_RADIUS:
if len(balls) < 50:
# Split the ball into two smaller balls
new_color = random.choice(COLORS) # Randomly choose a color for the new balls
balls.append(Ball(ball.x, ball.y, random.randint(-BALL_SPEED, BALL_SPEED),
random.randint(-BALL_SPEED, BALL_SPEED), new_color))
balls.append(Ball(ball.x, ball.y, random.randint(-BALL_SPEED, BALL_SPEED),
random.randint(-BALL_SPEED, BALL_SPEED), new_color))
balls.remove(ball)
ball_split = True
break
# Check if the player has won
if len(balls) >= 50:
game_won = True
draw_text("Congratulations! You won!", pygame.font.SysFont(None, 50), WHITE, WIDTH // 2, HEIGHT // 2)
draw_restart_button() # Draw restart button
# Reset the ball split flag after a click
if click == 0:
ball_split = False
# Update the display
pygame.display.update()
# Limit frames per second
pygame.time.Clock().tick(60)
# Main function
def main():
main_menu()
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1 and len(balls) >= 50:
main_game() # Restart the game if restart button is clicked
if event.type == pygame.QUIT:
pygame.quit()
if __name__ == "__main__":
main()