Python Game - Turtle Race

 Python Game - Turtle Race


Instructions:

1. You need to install the turtle module and random module into your python folder, you can install them by using the following code in the terminal.

>>>pip install turtle
>>>pip install random 


Code:

from turtle import Turtle, Screen
import random

is_race_on = False
screen = Screen()
screen.setup(width=500, height=400)
user_bet = screen.textinput(title="Make your bet", prompt="Which turtle will win the race? Enter the color")
colors = ["red", "orange", "green", "yellow", "blue", "purple"]
y_positions = [-70, -40, -10, 20, 50, 80]
all_turtles = []

for turtle_index in range(0, 6):
new_turtle = Turtle(shape='turtle')
new_turtle.penup()
new_turtle.color(colors[turtle_index])
new_turtle.goto(x=-230, y=y_positions[turtle_index])
all_turtles.append(new_turtle)

if user_bet:
is_race_on = True

while is_race_on:
for turtle in all_turtles:
if turtle.xcor() > 230:
is_race_on = False
winning_color = turtle.pencolor()
if winning_color == user_bet:
print(f"You've won! The {winning_color} turtle is the winner!")
else:
print(f"You've lost! The {winning_color} turtle is the winner!")
random_distance = random.randint(0, 10)
turtle.forward(random_distance)

screen.exitonclick()

Output:
For output visit this link: Output







Comments

Popular posts from this blog

Draw any Image using Python Turtle Module

Convert Image To Sketch Using Python