Today This Free Friday I am working on a project on YouTube a Turtle racing Tutorial
import turtle
def get_number_of_racerss():
racers = 0
while True:
racers = input('Enter the number of racers (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. Try Again!')
racers = get_number_of_racerss()
print(racers)
- This part of the code is to tells the users to Enter a number of racers and to make sure it is in the range of racers in the code and make sure it is a number.
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.pendown()
racer.setpos(-WIDTH//2 + (i + 1) * spacingx, -HEIGHT//2 + 20)
racer.pendown()
turtles.append(racer)
return turtles
- This part of the code is to create the racing Turtle in the game
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)]
- This part of the code is the race that the turtle racers race in the game.