import random import time from turtle import * def drawTrack(): track = Turtle() track.hideturtle() track.speed(0) track.penup() track.width(3) # Start line track.goto(-300, -40) track.pendown() track.goto(-300, 240) track.penup() # Finish line track.goto(300, -40) track.pendown() track.goto(300, 240) track.penup() # Labels track.goto(-330, 250) track.write("START", font=("Arial", 12, "bold")) track.goto(260, 250) track.write("FINISH", font=("Arial", 12, "bold")) # Lanes track.color("gray") track.width(1) for y in range(-40, 241, 40): track.goto(-300, y) track.pendown() track.goto(300, y) track.penup() def drawInstructions(): ui = Turtle() ui.hideturtle() ui.penup() ui.goto(-200, 250) ui.write( "SHEEPS IN JEEPS\n\n" "Rules:\n" "• Click START to begin\n" "• Obstacles slow racers\n" "• A spaceship may remove a racer\n" "• First to finish wins", font=("Arial", 14, "bold") ) def setupCommentary(): global commentator commentator = Turtle() commentator.hideturtle() commentator.penup() commentator.goto(-250, -260) def updateCommentary(text): commentator.clear() commentator.write(text, font=("Arial", 14, "bold")) def drawStartButton(): global start_button start_button = Turtle() start_button.shape("square") start_button.shapesize(2, 6) start_button.color("green") start_button.penup() start_button.goto(0, -220) start_button.write("START", align="center", font=("Arial", 16, "bold")) start_button.onclick(startRace) def setupRacers(): global racers names = ["Jeff", "Fred", "Alice", "Xin", "Max", "Brad"] racers = [] for i in range(6): r = Turtle("turtle") r.penup() r.goto(-300, -20 + i * 40) r.nickname = names[i] racers.append(r) def createObstacles(): global obstacles obstacles = [] for _ in range(4): o = Turtle("square") o.color("brown") o.penup() o.shapesize(1, 1) o.goto( random.randint(-100, 200), random.choice([-20, 20, 60, 100, 140, 180]) ) obstacles.append(o) def spaceshipEvent(): if len(racers) <= 1: return ship = Turtle("triangle") ship.color("purple") ship.penup() ship.goto(0, 260) ship.setheading(270) victim = random.choice(racers) ship.goto(victim.position()) updateCommentary(f"{victim.nickname} was taken by a spaceship") victim.hideturtle() racers.remove(victim) time.sleep(1) ship.hideturtle() def race(): updateCommentary("The race has started") step = 0 win = False while not win: step += 1 for racer in racers: move = random.randint(5, 25) # Obstacle collision for obs in obstacles: if racer.distance(obs) < 20: move = 3 updateCommentary(f"{racer.nickname} hit an obstacle") racer.forward(move) if racer.xcor() >= 300: win = True break # Random spaceship event if step == 10 and random.random() < 0.6: spaceshipEvent() time.sleep(0.1) def displayWinner(): winner = max(racers, key=lambda r: r.xcor()) writer = Turtle() writer.hideturtle() writer.penup() writer.goto(-200, 180) writer.color("blue") writer.write( f"Winner: {winner.nickname}", font=("Arial", 28, "bold") ) def startRace(x, y): start_button.hideturtle() race() displayWinner() def main(): drawTrack() drawInstructions() setupCommentary() setupRacers() createObstacles() drawStartButton() main() mainloop()