Skip to content

Commit bc40423

Browse files
committedJun 10, 2024·
Eighth project
1 parent f1578fa commit bc40423

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed
 

‎.idea/workspace.xml

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎turtle_racing.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import turtle
2+
import time
3+
import random
4+
5+
WIDTH, HEIGHT = 500, 500
6+
COLORS = ['red', 'green', 'blue', 'orange', 'yellow', 'black', 'purple', 'pink', 'brown', 'cyan']
7+
8+
9+
def get_number_of_racers():
10+
racers = 0
11+
while True:
12+
racers = input('Enter number of racers (2-10): ')
13+
if racers.isdigit():
14+
racers = int(racers)
15+
else:
16+
print('Input is not numeric...Try agin!')
17+
continue
18+
19+
if 2 <= racers <= 10:
20+
return racers
21+
else:
22+
print('Number not in range,Try again!')
23+
24+
25+
def race(colors):
26+
turtles = create_turtles(colors)
27+
28+
while True:
29+
for racer in turtles:
30+
distance = random.randrange(1, 20)
31+
racer.forward(distance)
32+
33+
x, y = racer.pos()
34+
if y >= HEIGHT // 2 - 10:
35+
return colors[turtles.index(racer)]
36+
37+
38+
def create_turtles(colors):
39+
turtles = []
40+
spacingx = WIDTH // (len(colors) + 1)
41+
for i, color in enumerate(colors):
42+
racer = turtle.Turtle()
43+
racer.color(color)
44+
racer.shape('turtle')
45+
racer.left(90)
46+
racer.penup()
47+
racer.setpos(-WIDTH //2 + (i + 1) * spacingx, -HEIGHT//2 + 20)
48+
racer.pendown()
49+
turtles.append(racer)
50+
51+
return turtles
52+
53+
54+
def init_turtle():
55+
screen = turtle.Screen()
56+
screen.setup(WIDTH, HEIGHT)
57+
screen.title('Turtle Racer!')
58+
59+
60+
racers = get_number_of_racers()
61+
init_turtle()
62+
63+
random.shuffle(COLORS)
64+
colors = COLORS[:racers]
65+
66+
winner = race(colors)
67+
print('The winner is the Turtle with color:', winner)
68+
time.sleep(5)
69+
70+

0 commit comments

Comments
 (0)
Please sign in to comment.