[英]How to make a tiebreaker on Turtle Race?
嘿伙計們,我正在用 100 天的代碼編寫海龜競賽模塊,到目前為止我已經能夠讓它工作。 但是,我無法將決勝局 function 編碼到其中。 我使用基於 boolean 的“if”語句來進行基本的決勝局,但只有當你在游戲中的猜測是兩個獲勝者之一時它才有效。 如果打成平手,而你猜到的都不是贏家,我只能說“你輸了”。 有誰知道如何更好地編寫代碼?
from turtle import Turtle, Screen
import random
is_race_on= False
screen = Screen()
screen.setup(500, 400)
user_bet = screen.textinput(title="Make your
bet",prompt= "Which turtle will win the race?
Enter a color: ")
colors = ['red', 'orange', 'yellow', 'green',
'blue', 'purple']
y_positions= [-125, -75, -25, 25, 75, 125]
all_turtles= []
for turtle_index in range(0, 6):
new_turtle = Turtle()
new_turtle.color(colors[turtle_index])
new_turtle.penup()
new_turtle.shape("turtle")
new_turtle.goto(x= -230, y=
y_positions[turtle_index])
new_turtle.pendown()
all_turtles.append(new_turtle)
win = False
lose = False
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:
win = True
win_color = winning_color
else:
lose = True
lose_color = winning_color
if win and lose == True:
print(f"There was a tie between
{win_color}, and {lose_color}")
elif win == True and lose == False:
print(f"You've Won!! The {win_color}
turtle is the winner!")
elif win == False and lose == True:
print(f"You lost you loser you! The {lose_color} turtle is the winner!!")
random_distance= random.randint(0, 10)
turtle.forward(random_distance)
screen.exitonclick()
我會點這個有點不同。 在 while 循環開始時,我將檢查每只海龜的 position 以及任何越過終點線的海龜,我將保存到獲勝者列表中。 如果列表的長度為 0,則沒有人獲勝。 然后你可以再次循環遍歷海龜,增加它們,然后重新開始。 如果一只烏龜贏了並且列表的大小為 1,您可以支付該贏家。 如果列表大於 1,則您有平局,可以按照您認為合適的方式處理。 這會導致您在海龜之間循環兩次,但會更好地組織自己。 希望這是有道理的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.