Day 19-Turtle Racing Game


Posted by pei_______ on 2022-04-29

learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022


# Turtle Drawing

from turtle import Turtle, Screen
timmy = Turtle()
screen = Screen()
timmy.speed("fastest")

def go_forward():
    timmy.forward(10)

def go_backward():
    timmy.backward(10)

def turn_left():
    timmy.left(10)

def turn_right():
    timmy.right(10)

def clear():
    timmy.goto(0, 0)
    timmy.clear()

screen.listen()
screen.onkey(go_forward, "w")
screen.onkey(go_backward, "s")
screen.onkey(turn_left, "a")
screen.onkey(turn_right, "d")
screen.onkey(clear, "c")

screen.exitonclick()
# Turtle Race

from turtle import Turtle, Screen
import random
screen = Screen()
screen.setup(width=500, height=400)

is_race_start = False

user_guess = screen.textinput(title="Make a Bet",prompt="Which turtle will win the race? Enter a color: ")
turtle_color = ["red", "orange", "yellow", "green", "blue", "purple"]
y_positions = [-125, -75, -25, 25, 75, 125]

all_turtle = []

for turtle_index in range(0, 6):
    new_turtle = Turtle(shape="turtle")
    new_turtle.penup()
    new_turtle.color(turtle_color[turtle_index])
    new_turtle.goto(x=-200, y=y_positions[turtle_index])
    all_turtle.append(new_turtle)

if user_guess:
    is_race_start = True

while is_race_start:

    for turtle in all_turtle:
        if turtle.xcor() > 230:
            is_race_start = False
            winner = turtle.pencolor()
            if winner == user_guess:
                print(f"You win! The winner is {winner}")
            else:
                print(f"You lose! The winner is {winner}")
            break

        random_move = random.randint(1, 10)
        turtle.forward(random_move)

screen.exitonclick()

#Python #課堂筆記 #100 Days of Code







Related Posts

Markdown

Markdown

相見恨晚的 chrome 插件 — Octotree - GitHub code tree

相見恨晚的 chrome 插件 — Octotree - GitHub code tree

Python Table Manners - 安全性檢查

Python Table Manners - 安全性檢查


Comments