Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.5 kB
4
Indexable
Never
from turtle import Turtle, Screen #imports the Turtle and Screen classes from the turtle module
                                  #these classes are used to create turtle graphics and the screen to display them.
import random #is used to generate random numbers

is_race_on = False  #This line initializes a Boolean variable called is_race_on to False. 
                    #This variable is used to control the loop that runs the race.

screen = Screen()  
screen.setup(width=500, height=400)
#This line creates a new Screen object and sets its size to 500 pixels wide and 400 pixels tall
user_bet = screen.textinput(title="Make your bet", prompt="Which turtle will win the race? Enter a color: ")
#This line displays a pop-up window with a message and a text input field. 
#The message asks the user to enter the color of the turtle they think will win the race.
colors = ["red", "orange", "yellow", "green", "blue", "purple"]#This line creates a list of turtle colors that will be used to
#create the turtles for the race
y_positions = [-70, -40, -10, 20, 50, 80] 
#This line creates a list of y-coordinates that will be used to position the turtles on the screen.


all_turtles = []  # creates an empty list called all_turtles



#This loop creates six turtle objects with a turtle shape, sets the turtle's pen to the up position, 
#sets the turtle's color to a value from the colors list, and positions the turtle at a specific location
#on the screen using x and y coordinates from the y_positions list.Each turtle object is added to the all_turtles list.
for turtle_index in range(0, 6):
    new_turtle = Turtle(shape="turtle")
    new_turtle.penup()
    new_turtle.color(colors[turtle_index])
    new_turtle.goto(x=-230, y=y_positions[turtle_index])
    all_turtles.append(new_turtle)

    
    
if user_bet:
    is_race_on = True        #This line checks if the user_bet variable has a value
                             #(i.e., the user entered a bet) and sets the is_race_on variable to True.

while is_race_on:
    for turtle in all_turtles:
        #230 is 250 - half the width of the turtle.
        if turtle.xcor() > 230:
            is_race_on = False
            winning_color = turtle.pencolor()
            if winning_color == user_bet:
                print(f"You've won! The {winning_color} turtle is the winner!")
            else:
                print(f"You've lost! The {winning_color} turtle is the winner!")

        #Make each turtle move a random amount.
        rand_distance = random.randint(0, 10)
        turtle.forward(rand_distance)

#This loop runs as long as the is_race_on variable is True. It iterates through each turtle in the all_turtles list and moves it
#forward a random distance. If a turtle's x-coordinate is greater than 230, the loop is terminated, and the winning turtle's 
#color is determined. If the winning turtle's color matches the user_bet variable, a message is printed to the console indicating 
#that the user won. If not, a message is printed to the console indicating
        
        
        
screen.exitonclick()      
#This line waits for the user to click the screen before closing it. Once the user clicks the screen, the exitonclick() method is
#called on the screen object, which terminates the program and closes the window. This is a way to keep the window open and
#give the user a chance to see the final result of the race before closing the program.