import random from turtle import * reddotsposition = [] racers = [] prediction = "" game_started = False weather = "sunny" ufo_count = 0 commentator = Turtle() commentator.hideturtle() commentator.penup() commentator.color("white") def say(text): """this function is to display commentary texts at the top of the screen.""" commentator.clear() commentator.goto(0, 280) commentator.write(text, align="center", font=("Arial", 14, "bold")) def setweather(): """Set random weather for the race.""" global weather weather = random.choice(["sunny", "rainy", "cloudy"]) def start_game(x, y): """This function allows mouse clicking to start the game from the instructions.""" global game_started if not game_started: game_started = True clearscreen() main() def show_instructions(): """This function displays the instruction and waits for the players to press start.""" screen = Screen() screen.bgcolor("grey") pen = Turtle() pen.hideturtle() pen.color("white") pen.penup() pen.goto(0,120) pen.write("Welcome to the turtle grandprix", align="center", font=("Arial", 20, "bold")) pen.goto(0,80) pen.write("Instructions", align="center", font=("Arial", 18, "bold")) pen.goto(0,50) pen.write("Pick a racer before the race starts", align="center", font=("Arial", 16, "bold")) pen.goto(0,30) pen.write("The red ball slows down the turtles", align="center", font=("Arial", 16, "bold")) pen.goto(0,-40) pen.write("Click anywhere to start", align="center", font=("Arial", 20, "bold")) screen.onclick(start_game) def draw_track(): """This function draws th race track, I used Ai for the track as stated in Sprint 4 to include Ai to create the track, overall the track has a gray surface, whitelanes, and red obstacles(I didn't use ai for the red obstacles as i made it myself)""" global reddotsposition screen = Screen() screen.bgcolor("darkgreen") track = Turtle() track.hideturtle() track.speed(0) track.penup() track.color("gray") track.goto(-330, -50) track.begin_fill() track.goto(330, -50) track.goto(330, 260) track.goto(-330, 260) track.goto(-330, -50) track.end_fill() track.color("white") track.goto(-300, -50) track.pendown() track.goto(-300, 260) track.penup() track.goto(300, -50) track.pendown() track.goto(300, 260) track.penup() for i in range(7): track.goto(-300, i * 40 - 50) track.pendown() track.goto(300, i * 40 - 50) track.penup() reddots = Turtle() reddots.hideturtle() reddots.penup() reddots.color("red") reddotsposition.clear() for i in range(10): x = random.randint(-280, 280) y = random.randint(-40, 240) reddots.goto(x, y) reddots.dot(25) reddotsposition.append((x, y)) def setup_racers(): """This function creates and positions the 6 turtles with their names""" global racers draw_track() setweather() names = ["Jeff", "Fred", "Alice", "Xin", "Max", "Brad"] racers = [] for i in range(6): racers.append(Turtle()) for i in range(6): racers[i].nickname = names[i] racers[i].shape("turtle") racers[i].penup() racers[i].goto(-300, i * 40) def race(): """This function runs the main race loop where turtles move and random events occur, I used AI to help me understand how distance-based collision detection works, not to complete the task for me.""" say("And they're off! Let's see who takes the lead!") while not check_win(): randomnumber = random.randint(0,10) if randomnumber >= 3: say("Look! A UFO appears over the track!") ufo() for racer in racers: x = racer.xcor() y = racer.ycor() near = False for dot_x, dot_y in reddotsposition: dx = x - dot_x dy = y - dot_y distance = (dx ** 2 + dy ** 2) ** 0.5 if distance < 30: near = True break if near: say(racer.nickname + " is slowed down by a red ball!") racer.forward(random.randint(5, 15)) else: if weather == "rainy": racer.forward(random.randint(10,15)) elif weather == "cloudy": racer.forward(random.randint(15,20)) else: racer.forward(random.randint(20,30)) def ufo(): """Creates ufo that removes two random turtles from the race""" global ufo_count if ufo_count >= 2: return if len(racers) <= 1: return ufo_sprite = Turtle() ufo_sprite.shape("triangle") ufo_sprite.color("green") ufo_sprite.penup() ufo_sprite.goto(0, 20) ufo_sprite.showturtle() c = random.choice(racers) commentator.clear() commentator.goto(0, 280) commentator.write(c.nickname + " has been taken out by the UFO!", align="center", font=("Arial", 14, "bold")) c.hideturtle() racers.remove(c) ufo_count += 1 ufo_sprite.hideturtle() def check_win(): """this function checks if any turtles cross the finish line at x=300""" for racer in racers: if racer.xcor() >= 300: return True return False def display_result(): """This function shows the race winner and compare with the players prediction""" say("The race is over! Let's see the winner!") winner = racers[0] for racer in racers: if racer.xcor() > winner.xcor(): winner = racer winner.penup() winner.goto(0, 0) winner.color("white") winner.write("Winner: " + winner.nickname, align="center", font=("Arial", 18, "normal")) winner.goto(0, -30) if prediction == winner.nickname: winner.color("lime") winner.write("You were correct!", align="center", font=("Arial", 14, "normal")) else: winner.color("red") winner.write("Your guess was " + prediction, align="center", font=("Arial", 14, "normal")) def get_prediction(): """This function get's player's prediction of which turtle will win the race""" names = ["Jeff", "Fred", "Alice", "Xin", "Max", "Brad"] display_racers() guess = textinput("Who will win?", "Type turtle name") if guess and guess.capitalize() in names: return guess.capitalize() return random.choice(names) def display_racers(): """This displays the list of the turtle's name on the left side of the screen""" pen = Turtle() pen.hideturtle() pen.penup() pen.color("white") pen.goto(-360, 0) pen.write("Turtle 1: Jeff") pen.goto(-360, 40) pen.write("Turtle 2: Fred") pen.goto(-360, 80) pen.write("Turtle 3: Alice") pen.goto(-360, 120) pen.write("Turtle 4: Xin") pen.goto(-360, 160) pen.write("Turtle 5: Max") pen.goto(-360, 200) pen.write("Turtle 6: Brad") pen.goto(-360, -30) pen.write(f"Weather: {weather}") def main(): """This function setups, runs, and ends the race.""" global prediction setup_racers() prediction = get_prediction() race() display_result() show_instructions() done()