From 357024811b65f5427ae9a667cf624ec2eba47623 Mon Sep 17 00:00:00 2001 From: Pradeep Date: Wed, 6 Oct 2021 11:01:39 +0530 Subject: [PATCH 1/2] Added Turtle Racing using python --- Turtle Racing/Turtle_race.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Turtle Racing/Turtle_race.py diff --git a/Turtle Racing/Turtle_race.py b/Turtle Racing/Turtle_race.py new file mode 100644 index 0000000..e69de29 From a6ead8892f7bcafa7f590afa6a374e3fe9b62165 Mon Sep 17 00:00:00 2001 From: Pradeep Date: Wed, 6 Oct 2021 18:45:27 +0530 Subject: [PATCH 2/2] Added Turtle Racing in python --- Turtle Racing/Turtle_race.py | 68 ++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/Turtle Racing/Turtle_race.py b/Turtle Racing/Turtle_race.py index e69de29..c268b43 100644 --- a/Turtle Racing/Turtle_race.py +++ b/Turtle Racing/Turtle_race.py @@ -0,0 +1,68 @@ +import random +import time +import turtle + +WIDTH, HEIGHT = 500,500 +COLORS = ["red","green","blue","orange","yellow","black","brown","cyan","purple","pink"] + +def get_number_of_racers(): + racers =0 + while True: + racers = input("Enter the number of racers(2 - 10): ") + if racers.isdigit(): + racers = int(racers) + + else: + print("Please input valid number") + continue + + if 2<=racers <=10: + return racers + else: + print("Number is not in range, please enter valid number(between 2 and 10") + + +def race(colors): + turtles = create_turtle(colors) + + while True: + for racer in turtles: + distance = random.randrange(1,20) + racer.forward(distance) + x, y =racer.pos() + + if y>= HEIGHT // 2 -10: + return colors[turtles.index(racer)] + + +def create_turtle(colors): + turtles =[] + spacingX = WIDTH // (len(colors)+1) + for i,color in enumerate(colors): + racer =turtle.Turtle() + racer.color(color) + racer.shape("turtle") + racer.left(90) + racer.penup() + racer.setpos(-WIDTH//2 + (i + 1)*spacingX, -HEIGHT//2 +20)#set position + racer.pendown() + turtles.append(racer) + return turtles + + +# Initializes turtle window +def init_turtle(): + screen = turtle.Screen() + screen.setup(WIDTH,HEIGHT) + screen.title("Turtle Race") + + +racers = get_number_of_racers() +init_turtle() + +random.shuffle(COLORS) +colors = COLORS[:racers] + +winner = race(colors) +print("The winner is the turtle with color:", winner) +time.sleep(5)