Made an etch-a-sketch and turtle race game!
Update README.md
This commit is contained in:
parent
bc3301c6a7
commit
8f4c47043d
3 changed files with 91 additions and 0 deletions
43
Day 19/turtle-race.py
Normal file
43
Day 19/turtle-race.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
from turtle import Turtle, Screen
|
||||
import random
|
||||
|
||||
# Game settings
|
||||
is_race_on = False
|
||||
screen = Screen()
|
||||
screen.title("The Great Turtle Race!")
|
||||
screen.setup(width=500, height=400)
|
||||
|
||||
# Get user guess
|
||||
user_selection = ""
|
||||
while not user_selection:
|
||||
user_selection = screen.textinput(title="Who will win?", prompt="Which turtle will win the race?\nType blue, green, red, orange, turquoise, or purple.\nEnter a color: ")
|
||||
|
||||
# Setup turtle objects after input
|
||||
colors = ["blue", "green", "turquoise", "orange", "red", "purple"]
|
||||
turtles = [Turtle(shape="turtle") for turtle in range(6)]
|
||||
for i in range(6):
|
||||
turtles[i].color(colors[i])
|
||||
turtles[i].penup()
|
||||
turtles[i].goto(x=-230, y=-100+(i*40))
|
||||
|
||||
# Start race!
|
||||
result = Turtle()
|
||||
result.penup()
|
||||
result.hideturtle()
|
||||
result.goto(x=0, y=100)
|
||||
is_race_on = True
|
||||
while is_race_on:
|
||||
for turtle in turtles:
|
||||
# Move turtle forward a random distance
|
||||
rand_distance = random.randint(0, 10)
|
||||
turtle.forward(rand_distance)
|
||||
# Check if a turtle won the race
|
||||
if turtle.xcor() > 230:
|
||||
is_race_on = False
|
||||
winning_color = turtle.pencolor()
|
||||
if winning_color == user_selection:
|
||||
result.write(f"You've won! The {winning_color} turtle is the winner!", align="center", font=("Open Sans", 16, "normal"))
|
||||
else:
|
||||
result.write(f"You've lost! The {winning_color} turtle is the winner!", align="center", font=("Open Sans", 16, "normal"))
|
||||
|
||||
screen.exitonclick()
|
||||
Loading…
Add table
Add a link
Reference in a new issue