import random from turtle import * setup(900, 500) bgcolor("#7ec8ff") title("Turtle Racing Game") TRACK_OFFSET_Y = -100 def drawTrack(): track = Turtle() track.hideturtle() track.speed(0) track.penup() track.pensize(3) track.color("black") start_x = -300 finish_x = 300 racers_count = 6 lane_height = 40 # start line track.goto(start_x, TRACK_OFFSET_Y) track.setheading(90) track.pendown() track.forward((racers_count - 1) * lane_height) track.penup() # finish line track.goto(finish_x, TRACK_OFFSET_Y) track.setheading(90) track.pendown() track.forward((racers_count - 1) * lane_height) track.penup() # lane lines for i in range(racers_count - 1): track.goto(start_x, TRACK_OFFSET_Y + i * lane_height) track.setheading(0) track.pendown() track.forward(finish_x - start_x) track.penup() # top line for Brad track.goto(start_x, TRACK_OFFSET_Y + (racers_count - 1) * lane_height) track.setheading(0) track.pendown() track.forward(finish_x - start_x) track.penup() def setupRace(): global racers drawTrack() names = ["Jeff", "Fred", "Alice", "Xin", "Max", "Brad"] colors = ["red", "blue", "green", "orange", "purple", "yellow"] racers = [] for i in range(6): t = Turtle() t.nickname = names[i] t.shape("turtle") t.color(colors[i]) t.penup() t.goto(-300, TRACK_OFFSET_Y + i * 40) t.setx(-330) t.write(t.nickname, align="right", font=("Arial", 10, "bold")) t.setx(-300) t.pendown() racers.append(t) def race(): finished = False while not finished: for t in racers: t.forward(random.randint(5, 20)) finished = checkWin() def checkWin(): for t in racers: if t.xcor() >= 300: return True return False def displayResult(prediction): writer = Turtle() writer.hideturtle() writer.penup() winner = max(racers, key=lambda t: t.xcor()) writer.goto(0, 190) writer.color("#FFEA00") writer.write( f"🏆 Winner: {winner.nickname}", align="center", font=("Arial", 22, "bold") ) writer.goto(0, 170) writer.color("black") writer.write( f"Your prediction: {prediction}", align="center", font=("Arial", 14, "normal") ) writer.goto(0, 140) if prediction == winner.nickname.lower(): writer.color("#2ecc71") writer.write("🎉 YOU WIN!", align="center", font=("Arial", 18, "bold")) else: writer.color("#e74c3c") writer.write("❌ YOU LOSE", align="center", font=("Arial", 18, "bold")) def getPrediction(): valid_names = ["jeff", "fred", "alice", "xin", "max", "brad"] while True: prediction = textinput( "Prediction", "Which turtle will win?\n(Jeff, Fred, Alice, Xin, Max, Brad)" ) if prediction is None: continue prediction = prediction.strip().lower() if prediction in valid_names: return prediction def askRestart(): while True: answer = textinput("Play Again?", "Do you want to play again? (yes / no)") if answer is None: continue answer = answer.strip().lower() if answer in ["yes", "y"]: return True if answer in ["no", "n"]: return False def main(): while True: Screen().clear() bgcolor("#7ec8ff") setupRace() prediction = getPrediction() race() displayResult(prediction) if not askRestart(): break def startScreen(): screen = Screen() screen.tracer(0) writer = Turtle() writer.hideturtle() writer.penup() writer.goto(0, 80) writer.write( "🐢 TURTLE RACING GAME 🐢", align="center", font=("Arial", 32, "bold") ) button = Turtle() button.hideturtle() button.penup() button.goto(0, -20) button.shape("square") button.shapesize(stretch_wid=2, stretch_len=8) button.fillcolor("#2ecc71") button.color("green") button.showturtle() writer.goto(0, -35) writer.write( "START GAME", align="center", font=("Arial", 16, "bold") ) screen.update() def startGame(x, y): if -80 < x < 80 and -40 < y < 0: screen.clear() bgcolor("#7ec8ff") main() button.onclick(startGame) startScreen()