Welcome to the Turtle Racing project! This Python project simulates a turtle race using the turtle module. The project allows users to specify the number of racing turtles (between 2 and 10), and then the turtles race to the top of the screen. The winner is the first turtle to cross the finish line.
turtleRacing.py: The main Python script that runs the turtle race.final_output_race.png: Screenshot of the race's final output.outputWindow_screenrecord.webm: Screen recording of the turtle race.output_in_execution.png: Screenshot of the race's under in progress.
- Python 3.x
-
Clone the Repository:
git clone https://github.com/shawrajdeep00/turtle_racing_python.git cd turtle_racing_python -
Run the Script:
python turtleRacing.py
-
Enter the Number of Racers: When prompted, enter the number of turtles that will participate in the race (between 2 and 10).
The turtleRacing.py script contains the following sections:
- Import the
turtlemodule. - Import the
timemodule. - Import the
randommodule.
- Set
WIDTHandHEIGHTfor the screen dimensions. - Define a list
COLORScontaining the colors for the turtles.
Pseudocode:
Function get_number_of_racers()
Set racers to 0
While True
Prompt user to enter number of racers between 2 and 10
If input is numeric
Convert input to integer
Else
Print error message and continue loop
If racers is between 2 and 10
Return racers
Else
Print error message and continue loop
Pseudocode:
Function race(colors)
turtles = create_turtles(colors)
While True
For each turtle in turtles
Move turtle forward by random distance between 1 and 20
Get turtle's current position
If turtle's position >= finish line
Return turtle's color as winner
Pseudocode:
Function create_turtles(colors)
Initialize empty list turtles
Calculate horizontal spacing between turtles
For each color in colors
Create new turtle
Set turtle color
Set turtle shape to "turtle"
Turn turtle to face upward
Lift turtle pen
Set turtle starting position
Put turtle pen down
Add turtle to turtles list
Return turtles
Pseudocode:
Function init_turtle()
Create screen object
Set up screen with specified width and height
Set screen title to "Turtle Racing!"
Pseudocode:
Call get_number_of_racers()
Call init_turtle()
Shuffle COLORS list
Select subset of colors based on number of racers
Call race(colors)
Print winner's color
Wait for 5 seconds before closing window
The script imports three modules:
turtle: Provides the Turtle graphics library for creating the race.time: Allows for pauses in the script to control the flow.random: Generates random numbers to simulate the turtles' movements.
WIDTHandHEIGHT: These constants set the dimensions of the window where the race will take place.COLORS: A list of colors to distinguish each racing turtle.
This function handles user input to determine the number of turtles participating in the race. It ensures the input is valid and within the specified range (2 to 10).
This function conducts the race. It moves each turtle forward by a random distance in each iteration until one turtle reaches or crosses the finish line. The function returns the color of the winning turtle.
This function initializes the turtles. It creates a list of turtle objects, sets their colors, shapes, and initial positions on the screen.
This function sets up the turtle graphics screen, configuring its dimensions and title.
The main part of the script orchestrates the race. It:
- Gets the number of racers from the user.
- Initializes the turtle screen.
- Shuffles the colors and selects a subset based on the number of racers.
- Starts the race and determines the winner.
- Prints the winner's color and waits for 5 seconds before closing the window.
The turtle module in Python provides a simple way to draw shapes and create graphics. For more detailed information, you can refer to the official Python Turtle Documentation.
Key functions used in this project include:
turtle.Turtle(): Creates a new turtle.turtle.Screen(): Creates a screen object.Turtle.forward(distance): Moves the turtle forward by the specified distance.Turtle.left(angle): Turns the turtle left by the specified angle.Turtle.penup(): Lifts the pen, so the turtle does not draw when moving.Turtle.pendown(): Puts the pen down, so the turtle draws when moving.Turtle.setpos(x, y): Sets the turtle's position to the specified coordinates.Screen.setup(width, height): Sets the width and height of the window.Screen.title(title): Sets the title of the window.
Implementing the Turtle Racing project is a fun and engaging way to practice programming fundamentals. It helps you get comfortable with key concepts like loops, conditionals, and functions, while also introducing you to the turtle graphics module in Python. By working on this project, you’ll also learn how to handle user input and use random number generation to create dynamic outcomes. In real life, these skills can open doors to more complex projects, like building graphical user interfaces, animations, or even simple games. This project not only strengthens your coding abilities but also sparks creativity, making it a great stepping stone for future development endeavors.
Developers are asked to use this block of code and this project according to their own project needs. Changes are always welcomed. For this project, by following the steps and explanations provided, you can easily understand and recreate the turtle racing project from scratch. Enjoy the race! Thank you!