Skip to content

Commit

Permalink
Eighth project
Browse files Browse the repository at this point in the history
  • Loading branch information
Parq254 committed Jun 10, 2024
1 parent f1578fa commit bc40423
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions turtle_racing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import turtle
import time
import random

WIDTH, HEIGHT = 500, 500
COLORS = ['red', 'green', 'blue', 'orange', 'yellow', 'black', 'purple', 'pink', 'brown', 'cyan']


def get_number_of_racers():
racers = 0
while True:
racers = input('Enter number of racers (2-10): ')
if racers.isdigit():
racers = int(racers)
else:
print('Input is not numeric...Try agin!')
continue

if 2 <= racers <= 10:
return racers
else:
print('Number not in range,Try again!')


def race(colors):
turtles = create_turtles(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_turtles(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)
racer.pendown()
turtles.append(racer)

return turtles


def init_turtle():
screen = turtle.Screen()
screen.setup(WIDTH, HEIGHT)
screen.title('Turtle Racer!')


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)


0 comments on commit bc40423

Please sign in to comment.