Make a python game using turtle that display falling circles of different colors and…
- User
Make a python game using turtle that display falling circles of different colors and sizes. If you click a circle you get a score based on how small its size is (the smaller the higher score). The falling speed gets higher as your score increases.
- Assistant
Sure! Below is a Python game using the
turtlemodule where colorful circles of varying sizes fall from the top of the screen. Clicking on a circle increases your score based on the circle's size—the smaller the circle, the higher the score. As your score increases, the falling speed of the circles also increases, making the game progressively more challenging.How the Game Works:
- Circles Generation: Circles of random sizes, colors, and horizontal positions are generated at the top of the screen and start falling down.
- Scoring: When you click on a circle, it disappears, and you earn points inversely proportional to its size (smaller circles give more points).
- Increasing Difficulty: As your score increases, the falling speed of the circles becomes faster.
- Game Over Condition: The game continues indefinitely, but you can implement additional features like game over conditions based on your preference.
Requirements:
- Python installed on your computer.
- Basic understanding of Python and the
turtlemodule.
Complete Code:
import turtle import random import time # Screen setup SCREEN_WIDTH = 600 SCREEN_HEIGHT = 600 BG_COLOR = "black" # Circle parameters MIN_RADIUS = 10 MAX_RADIUS = 30 CIRCLE_COLORS = ["red", "green", "blue", "yellow", "purple", "orange", "cyan", "magenta"] # Speed parameters BASE_SPEED = 2 SPEED_INCREMENT = 0.1 # How much to increase speed per score # Circle generation parameters NEW_CIRCLE_INTERVAL = 2000 # Milliseconds # Initialize the screen screen = turtle.Screen() screen.setup(width=SCREEN_WIDTH, height=SCREEN_HEIGHT) screen.title("Falling Circles Game") screen.bgcolor(BG_COLOR) screen.tracer(0) # Turn off automatic screen updates # Score display score = 0 score_display = turtle.Turtle() score_display.hideturtle() score_display.penup() score_display.color("white") score_display.goto(-SCREEN_WIDTH//2 + 20, SCREEN_HEIGHT//2 - 40) score_display.write(f"Score: {score}", font=("Arial", 16, "normal")) # List to hold all circles circles = [] # Define a Circle class class Circle: def __init__(self): self.radius = random.randint(MIN_RADIUS, MAX_RADIUS) self.color = random.choice(CIRCLE_COLORS) self.x = random.randint(-SCREEN_WIDTH//2 + self.radius, SCREEN_WIDTH//2 - self.radius) self.y = SCREEN_HEIGHT//2 + self.radius self.speed = BASE_SPEED + (score * SPEED_INCREMENT) # Create turtle for circle self.turtle = turtle.Turtle() self.turtle.shape("circle") self.turtle.shapesize(stretch_wid=self.radius/10, stretch_len=self.radius/10) # Default circle size is 20x20 self.turtle.color(self.color) self.turtle.penup() self.turtle.goto(self.x, self.y) def move(self): self.y -= self.speed self.turtle.sety(self.y) def is_off_screen(self): return self.y < -SCREEN_HEIGHT//2 - self.radius def remove(self): self.turtle.hideturtle() circles.remove(self) # Update the score display def update_score(points): global score score += points score_display.clear() score_display.write(f"Score: {score}", font=("Arial", 16, "normal")) # Function to create a new circle def create_circle(): circle = Circle() circles.append(circle) # Schedule the next circle creation screen.ontimer(create_circle, NEW_CIRCLE_INTERVAL) # Function to update all circles def update_circles(): global circles for circle in circles[:]: circle.speed = BASE_SPEED + (score * SPEED_INCREMENT) # Update speed based on current score circle.move() if circle.is_off_screen(): # Optionally, you can end the game or remove the circle circle.remove() screen.update() # Continue updating screen.ontimer(update_circles, 20) # Update every 20 milliseconds # Function to handle clicks def handle_click(x, y): for circle in circles[:]: distance = ((x - circle.x)**2 + (y - circle.y)**2)**0.5 if distance <= circle.radius: points = MAX_RADIUS - circle.radius + 1 # Smaller radius gives higher points update_score(points) circle.remove() break # Only handle one circle per click # Start the game create_circle() update_circles() # Listen to mouse clicks screen.listen() screen.onclick(handle_click) # Keep the window open turtle.done()Explanation of the Code:
-
Imports and Constants:
turtle,random, andtimemodules are imported.- Constants are defined for screen dimensions, circle properties, speed parameters, and circle generation intervals.
-
Screen Setup:
- The screen is initialized with the specified width, height, background color, and title.
- The screen's tracer is turned off to manually control screen updates for smoother animations.
-
Score Display:
- A turtle is created to display the score at the top-left corner of the screen.
-
Circle Class:
- Each
Circleobject has properties like radius, color, position (x,y), and speed. - The
movemethod updates the circle's position based on its speed. - The
is_off_screenmethod checks if the circle has moved below the visible area. - The
removemethod hides the circle and removes it from thecircleslist.
- Each
-
Score Handling:
- The
update_scorefunction updates the globalscorevariable and refreshes the score display. - Points are calculated so that smaller circles give more points.
- The
-
Circle Creation and Updates:
- The
create_circlefunction instantiates a newCircleand schedules the next circle creation afterNEW_CIRCLE_INTERVALmilliseconds. - The
update_circlesfunction moves all circles, updates their speed based on the current score, removes circles that have moved off-screen, and schedules the next update.
- The
-
Click Handling:
- The
handle_clickfunction checks if the click is within any circle's radius. If so, it updates the score and removes the circle.
- The
-
Game Loop:
create_circleandupdate_circlesare called to start the game.- The screen listens for mouse clicks and binds them to the
handle_clickfunction. turtle.done()keeps the window open.
Customization and Enhancements:
You can enhance this basic game by adding features such as:
- Game Over Condition: End the game if a circle reaches the bottom of the screen without being clicked.
- Levels: Introduce levels that change the game's behavior after reaching certain scores.
- Sound Effects: Add sounds for clicking circles or when circles disappear.
- Multiple Clicks: Allow multiple circles to be clicked at the same time.
- High Score Tracking: Keep track of high scores across game sessions.
Running the Game:
- Save the Code: Copy the above code into a Python file, e.g.,
falling_circles_game.py. - Run the Game: Execute the script using Python:
python falling_circles_game.py - Play: Click on the falling circles to earn points. Try to click smaller circles for higher scores as the game becomes faster!
Enjoy your game!