Advertisement
JkSoftware

Day 19 - Turtle Race

Dec 2nd, 2021
941
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. from turtle import Turtle, Screen
  2. import random
  3.  
  4. is_race_on = False
  5. screen = Screen()
  6. screen.setup(500, 400)
  7. user_bet = screen.textinput("Make your bet", "Which turtle will win the race? Enter a color: ").lower()
  8. print(user_bet)
  9. colors = ["red", "orange", "yellow", "green", "blue", "purple"]
  10. y_pos = [-70, -40, -10, 20, 50, 80]
  11. all_turtles = []
  12.  
  13. for turtle_index in range(0, 6):
  14.     new_turtle = Turtle(shape="turtle")
  15.     new_turtle.color(colors[turtle_index])
  16.     new_turtle.penup()
  17.     new_turtle.goto(-230, y_pos[turtle_index])
  18.     all_turtles.append(new_turtle)
  19.  
  20. if user_bet:
  21.     is_race_on = True
  22.  
  23. while is_race_on:
  24.  
  25.     for turtle in all_turtles:
  26.         if turtle.xcor() > 230:
  27.             is_race_on = False
  28.             winning_color = turtle.pencolor()
  29.             if winning_color == user_bet:
  30.                 print(f"{winning_color} has won! You WIN! :)")
  31.             else:
  32.                 print(f"{winning_color} Has won! You LOST! :(")
  33.         rand_distance = random.randint(0, 10)
  34.         turtle.forward(rand_distance)
  35.  
  36. screen.exitonclick()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement