From 879b10a45eb5823ea82de82e2db0a65bf0c4bb30 Mon Sep 17 00:00:00 2001 From: yashj88 Date: Sat, 27 Jun 2026 21:50:39 +0530 Subject: [PATCH 1/2] Add Turtle Racing game and README --- Turtle_Racing/README.md | 21 ++++ Turtle_Racing/TurtleRacing.py | 198 ++++++++++++++++++++++++++++++++++ 2 files changed, 219 insertions(+) create mode 100644 Turtle_Racing/README.md create mode 100644 Turtle_Racing/TurtleRacing.py diff --git a/Turtle_Racing/README.md b/Turtle_Racing/README.md new file mode 100644 index 00000000..a8050bef --- /dev/null +++ b/Turtle_Racing/README.md @@ -0,0 +1,21 @@ +# 🐢 Turtle Racing Game (Python) + +A fun racing simulation built using Python's `turtle` graphics module. +Each turtle moves randomly, and the first to cross the finish line wins! + +--- + +## 📌 Features +- Choose the number of racers (2–10 turtles). +- Turtles are assigned random colors. +- Randomized movements make every race unique. +- Winner is announced both in the console and on the screen. + +--- + +## 🛠 Requirements +- Python 3.8+ +- `turtle` module (comes pre-installed with Python). + +--- +### Dificulty Level: Intermediate diff --git a/Turtle_Racing/TurtleRacing.py b/Turtle_Racing/TurtleRacing.py new file mode 100644 index 00000000..ba94ce66 --- /dev/null +++ b/Turtle_Racing/TurtleRacing.py @@ -0,0 +1,198 @@ +import turtle +import random +import time + +# Configurable track dimensions +WIDTH, HEIGHT = 800, 600 + +# High-contrast, vibrant turtle colors suited for dark background (black removed) +COLORS = ['red', 'green', 'blue', 'orange', 'yellow', 'white', 'purple', 'pink', 'cyan', 'lime'] + +def init_turtle(): + """Initializes the turtle graphics window with a sleek dark slate background.""" + screen = turtle.Screen() + screen.setup(WIDTH, HEIGHT) + screen.title('Turtle Racing!') + screen.bgcolor('#1e1e24') + return screen + +def get_number_of_racers(screen): + """Prompts the user inside the GUI for the number of racers.""" + try: + racers = screen.numinput( + "Number of Racers", + "Enter the number of racers (2 - 10):", + default=5, + minval=2, + maxval=10 + ) + if racers is None: + return None + return int(racers) + except Exception: + return None + +def draw_track(colors, screen): + """Draws the start/finish lines and lane dividers instantly on the canvas.""" + # Temporarily turn off tracer for instantaneous drawing + screen.tracer(0) + + pen = turtle.Turtle() + pen.hideturtle() + pen.speed(0) + pen.color("white") + pen.pensize(2) + + spacingx = WIDTH // (len(colors) + 1) + start_y = -HEIGHT // 2 + 50 + finish_y = HEIGHT // 2 - 50 + + # Draw Starting Line + pen.penup() + pen.goto(-WIDTH // 2, start_y) + pen.pendown() + pen.forward(WIDTH) + + # Draw Finish Line (lime dashed line) + pen.penup() + pen.goto(-WIDTH // 2, finish_y) + dash_length = 20 + is_down = True + pen.color("lime") + pen.pensize(4) + while pen.xcor() < WIDTH // 2: + if is_down: + pen.pendown() + else: + pen.penup() + pen.forward(dash_length) + is_down = not is_down + + # Draw Lanes / Boundaries + pen.color("#3a3a44") + pen.pensize(1) + for i in range(len(colors) - 1): + x = -WIDTH // 2 + (i + 1.5) * spacingx + pen.penup() + pen.goto(x, start_y) + pen.setheading(90) + while pen.ycor() < finish_y: + pen.pendown() + pen.forward(15) + pen.penup() + pen.forward(15) + + # Update screen and restore normal tracer + screen.update() + screen.tracer(1) + +def countdown(screen): + """Displays a graphical countdown in the center of the track.""" + writer = turtle.Turtle() + writer.hideturtle() + writer.penup() + writer.color("white") + writer.goto(0, 0) + + for msg in ["3", "2", "1", "GO!"]: + writer.clear() + writer.write(msg, align="center", font=("Arial", 48, "bold")) + screen.update() + time.sleep(0.6) + writer.clear() + screen.update() + +def announce_winner(color): + """Draws a beautiful card in the center to highlight the winner.""" + announcer = turtle.Turtle() + announcer.hideturtle() + announcer.speed(0) + announcer.penup() + + # Draw dark background panel card + announcer.goto(-200, 50) + announcer.color("#0f0f12") + announcer.begin_fill() + for _ in range(2): + announcer.forward(400) + announcer.right(90) + announcer.forward(100) + announcer.right(90) + announcer.end_fill() + + # Write "Race Finished" subtitle + announcer.goto(0, -25) + announcer.color("white") + announcer.write("Race Finished!", align="center", font=("Arial", 14, "normal")) + + # Write Winner color text + announcer.goto(0, 5) + display_color = color.upper() + announcer.color(color) + announcer.write(f"{display_color} Turtle Wins!", align="center", font=("Arial", 22, "bold")) + +def create_turtles(colors): + """Creates the racers and places them at their lane starting points.""" + turtles = [] + spacingx = WIDTH // (len(colors) + 1) + start_y = -HEIGHT // 2 + 50 + for i, color in enumerate(colors): + racer = turtle.Turtle() + racer.color(color) + racer.shape('turtle') + racer.left(90) + racer.penup() + racer.setpos(-WIDTH//2 + (i + 1) * spacingx, start_y) + racer.pendown() + racer.pensize(3) + racer.speed(2) + turtles.append(racer) + + return turtles + +def race(colors, screen): + """Moves the turtles forward randomly until one crosses the finish line.""" + turtles = create_turtles(colors) + finish_y = HEIGHT // 2 - 50 + while True: + for racer in turtles: + distance = random.randrange(1, 15) + racer.forward(distance) + + x, y = racer.pos() + if y >= finish_y: + winner_color = colors[turtles.index(racer)] + announce_winner(winner_color) + return winner_color + +def main(): + try: + screen = init_turtle() + racers = get_number_of_racers(screen) + if racers is None: + print("Game closed/cancelled.") + return + + random.shuffle(COLORS) + colors = COLORS[:racers] + + draw_track(colors, screen) + countdown(screen) + + winner = race(colors, screen) + if winner: + print("The winner is the turtle with color:", winner) + + # Exit instructions + exit_msg = turtle.Turtle() + exit_msg.hideturtle() + exit_msg.penup() + exit_msg.color("gray") + exit_msg.goto(0, -HEIGHT // 2 + 15) + exit_msg.write("Click anywhere on the screen to exit.", align="center", font=("Arial", 10, "italic")) + screen.exitonclick() + except (turtle.Terminator, Exception): + print("Application closed or interrupted.") + +if __name__ == "__main__": + main() \ No newline at end of file From 089b7d1ce42e1e7f99c1c0184640f0de5679e3ad Mon Sep 17 00:00:00 2001 From: yashj88 Date: Sat, 27 Jun 2026 21:51:30 +0530 Subject: [PATCH 2/2] Improve Turtle Racing README with execution and gameplay instructions --- Turtle_Racing/README.md | 49 ++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/Turtle_Racing/README.md b/Turtle_Racing/README.md index a8050bef..d753b820 100644 --- a/Turtle_Racing/README.md +++ b/Turtle_Racing/README.md @@ -1,21 +1,50 @@ -# 🐢 Turtle Racing Game (Python) +# 🐢 Turtle Racing Game -A fun racing simulation built using Python's `turtle` graphics module. -Each turtle moves randomly, and the first to cross the finish line wins! +A fun, interactive, and customizable racing simulation built with Python's native `turtle` graphics module. Watch vibrant turtles compete on a sleek dark track! --- ## 📌 Features -- Choose the number of racers (2–10 turtles). -- Turtles are assigned random colors. -- Randomized movements make every race unique. -- Winner is announced both in the console and on the screen. + +- **Custom Racer Count:** Choose between 2 and 10 turtle racers dynamically via a GUI popup. +- **Sleek Dark Theme:** Modern `#1e1e24` dark slate background with high-contrast, vibrant turtle colors. +- **Dynamic Lanes & Track:** Lane dividers and track boundaries scale dynamically based on the number of racers. +- **Visual Countdown:** Graphical "3, 2, 1, GO!" animation before the race starts. +- **Interactive Winner Board:** A beautiful card highlights the winner on the canvas when the race is finished. +- **Console & GUI Output:** Prints the results to both the terminal and the graphic screen. --- ## 🛠 Requirements -- Python 3.8+ -- `turtle` module (comes pre-installed with Python). + +- **Python 3.8+** +- The `turtle` module (included in Python's standard library). + +--- + +## 🚀 How to Run + +1. **Navigate to the Project Directory:** + ```bash + cd Turtle_Racing + ``` + +2. **Run the Script:** + You can run the script directly using Python: + ```bash + python TurtleRacing.py + ``` --- -### Dificulty Level: Intermediate + +## 🎮 Gameplay Instructions + +1. **Set the Number of Racers:** A dialog box will prompt you to enter the number of turtles (from 2 to 10). +2. **Watch the Countdown:** Once entered, the racers will line up at the starting line and a graphical countdown will begin. +3. **The Race:** The turtles move forward by random increments. The first turtle to cross the green dashed finish line wins! +4. **Exit:** Once a winner is announced, click anywhere on the game window to exit. + +--- + +### 🏆 Difficulty Level: Intermediate +