import random from turtle import * def drawTrack(): track = Turtle() track.hideturtle() track.speed(0) track.penup() # Draw START line at x = -300 track.goto(-300, 150) track.pendown() track.right(90) track.forward(300) track.penup() track.goto(-300, 160) track.write("START", align="center", font=("Arial", 14, "bold")) # Draw FINISH line at x = 300 track.goto(300, 150) track.pendown() track.forward(300) track.penup() track.goto(300, 160) track.write("FINISH", align="center", font=("Arial", 14, "bold")) # Draw lane lines for 6 racers y_start = 120 for i in range(6): y = y_start - (i * 40) track.goto(-320, y) track.pendown() track.goto(320, y) track.penup() def setup(): global racers, writer s = getscreen() drawTrack() writer = Turtle() writer.hideturtle() writer.penup() racers = {} names = ["Fred", "Sheldon", "Tomoki", "Kai", "Jonatah", "Qasim"] colors = ["blue", "red", "green", "purple", "orange", "dark gray"] start_y = 120 for i in range(6): t = Turtle() t.color(colors[i]) t.penup() t.goto(-300, start_y - (i * 40)) t.pendown() racers[names[i]] = t def displayWinner(): winner_name = None winner_x = -9999 for name, t in racers.items(): x = t.xcor() if x > winner_x: winner_x = x winner_name = name writer.goto(0, 200) writer.color("orange") writer.write( f"🏁 Winner: {winner_name} (x = {winner_x:.0f}) 🏁", align="center", font=("Arial", 26, "bold") ) game_started = False commentator = Turtle() def Instructions(): instructor = Turtle() instructor.hideturtle() instructor.penup() instructor.goto(0, 240) instructor.write( "🏇 HORSE RACING CHAMPIONSHIP 🏁\n" "Click START to begin the race!\n" "Horses move at random speeds\n" "Live commentary during the race\n" "First horse to FINISH wins!", align="center", font=("Arial", 14, "bold") ) def drawStartButton(): button = Turtle() button.hideturtle() button.penup() button.goto(-60, -200) button.pendown() button.fillcolor("lightgreen") button.begin_fill() for _ in range(2): button.forward(120) button.left(90) button.forward(40) button.left(90) button.end_fill() button.penup() button.goto(0, -190) button.write("START", align="center", font=("Arial", 16, "bold")) getscreen().onclick(startGame) def Commentary(): commentator.hideturtle() commentator.penup() commentator.goto(0, -240) commentator.color("darkblue") def randomCommentary(): messages = [ "A sudden burst of speed!", "One horse is pulling ahead!", "What a thrilling race!", "The crowd is on their feet!", "This could go either way!" ] commentator.clear() commentator.write(random.choice(messages), align="center", font=("Arial", 14, "italic")) def startGame(x, y): global game_started if -60 < x < 60 and -200 < y < -160 and not game_started: game_started = True commentator.clear() commentator.write( "🏁 And they're off!", align="center", font=("Arial", 16, "bold") ) race() displayWinner() def race(): finished = False while not finished: for name, turtle_obj in racers.items(): turtle_obj.forward(random.randint(1, 20)) if random.randint(1, 12) == 1: randomCommentary() if turtle_obj.xcor() >= 300: finished = True break def main(): setup() Commentary() Instructions() drawStartButton() main()