-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTurtle_Racing.py
More file actions
57 lines (51 loc) · 1.51 KB
/
Turtle_Racing.py
File metadata and controls
57 lines (51 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import turtle
import time
import random
WIDTH,HEIGHT = 800, 600
COLORS = ['red','green','blue','orange','yellow','black','purple', 'pink','brown','cyan']
def get_number_of_racer():
racers = 0
while True:
racers = input("Enter the number of racer (2-10): ")
if racers.isdigit():
racers = int(racers)
else:
print("Input is not numeric. Try again!")
continue
if 2<=racers<=10:
return racers
else:
print("Number not in range 2-10 ")
def race(colors):
turtle = create_turtles(colors)
while True:
for racer in turtle:
distance = random.randrange(1,20)
racer.forward(distance)
x,y = racer.pos()
if y > HEIGHT//2-10:
return colors[turtle.index(racer)]
def create_turtles(colors):
turtles = []
spacingc = WIDTH//(len(colors)+1)
for i, color in enumerate(colors):
racer = turtle.Turtle()
racer.shape('turtle')
racer.color(color)
racer.left(90)
racer.penup()
racer.setpos(-WIDTH/2 + (i+1)*spacingc, -HEIGHT//2 + 20)
racer.pendown()
turtles.append(racer)
return turtles
def init_turtle():
screen = turtle.Screen()
screen.setup(WIDTH, HEIGHT)
screen.title('My turtle Racing')
racer = get_number_of_racer()
init_turtle()
random.shuffle(COLORS)
colors = COLORS[:racer]
winner = race(colors)
print("The winner is the turtle with color: ", winner)
time.sleep(5)