Day 19
176 Goals: what we will make by the end of the day
177 Python Higher Oder Functions & Event Listeners
178 Make an Etch-A-Sketch App
Object | Class | ||
---|---|---|---|
instance | tim | = | Turtle() |
instance | tommy | = | Turtle() |
#載入函式不然不能使用 random.randint(0, 10), 或是 Turtle(), Screen()
from turtle import Turtle, Screen
import random
is_race_on=False
screen = Screen()
screen.setup(width=500,height=500)
user_bet = screen.textinput(title="Make your bet", prompt="Which tutle will win the race? Enter a color: ")
colors = ["red", "orange", "yellow", "green", "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:
#一次看一隻Turtle,用 for loop, 如果 這隻超過 x 位置 230 就代表第一名產生了,以後的不用再跑了
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!")
#看這次的這一隻 turtle 可以走幾步。有點像大富翁的擲骰子
rand_distance = random.randint(0, 10)
turtle.forward(rand_distance)
screen.exitonclick()