如何在 Python 海龟比赛中宣布获胜者 [英] How to declare winner in Python turtle race
问题描述
我正在使用 Python 模块turtle"创建一个基本的海龟程序.我遇到的唯一问题是如何宣布获胜者.
I'm creating a basic turtle program using the Python module 'turtle'. The only problem I come across is how to declare a winner.
我将尝试解释我的程序:我从制作一些垂直线和最后的终点线"开始.然后我使用了 3 个形状,并使用 randint()
将这些海龟向前移动以进行比赛.代码如下:
I will try to explain my program: I started with making some vertical lines and a final "Finish Line". Then I used 3 shapes and with use of randint()
I move these turtles forward to run the race. Here's the code:
from turtle import *
from random import randint
speed(0)
penup()
goto(-100,200)
for step in range(15):
write(step, align='center')
right(90)
forward(10)
pendown()
forward(160)
penup()
backward(170)
left(90)
forward(20)
goto(200,250)
write("Finish Line", align='center')
pendown()
right(90)
forward(300)
vince = Turtle()
vince.color('red')
vince.shape('turtle')
vince.penup()
vince.goto(-120,160)
vince.pendown()
lawliet = Turtle()
lawliet.color('blue')
lawliet.shape('turtle')
lawliet.penup()
lawliet.goto(-120,130)
lawliet.pendown()
boyka = Turtle()
boyka.color('green')
boyka.shape('turtle')
boyka.penup()
boyka.goto(-120,100)
boyka.pendown()
for turn in range(100):
speed(0)
vince.forward(randint(1,5))
lawliet.forward(randint(1,5))
boyka.forward(randint(1, 5))
问题来了:我想宣布赢得比赛的形状.但是当我查看 Turtle 库时,没有内置函数可以这样做.有没有办法宣布这场比赛的获胜者?
Here's the problem: I want to declare the shape which won the race. But when I looked in the Turtle library there is no built-in function to do so. Is there any way to declare the winner for this race?
推荐答案
有很多方法可以做到这一点.您需要的两件事是终点线 (200) 的 x 坐标和海龟的 x 坐标,turtle.xcor()
.下面是一个简单的解决方案,其中第一只重心在终点线上方的乌龟变成金色,表示胜利:
There are a number of ways to do this. Two things you need are the x coordinate of the finish line (200) and the x coordinate of the turtle, turtle.xcor()
. Below is a simple solution where the first turtle whose center of mass is over the finish line turns gold, for victory:
from turtle import Screen, Turtle
from random import randint, choice
track = Turtle(visible=False)
track.speed('fastest')
track.penup()
track.goto(-100, 200)
for step in range(15):
track.write(step, align='center')
track.right(90)
track.forward(10)
track.pendown()
track.forward(160)
track.penup()
track.backward(170)
track.left(90)
track.forward(20)
track.goto(200, 250)
track.write("Finish Line", align='center')
track.pendown()
track.right(90)
track.forward(300)
vince = Turtle('turtle')
vince.speed('fastest')
vince.color('red')
vince.penup()
vince.goto(-120, 160)
vince.pendown()
lawliet = Turtle('turtle')
lawliet.speed('fastest')
lawliet.color('blue')
lawliet.penup()
lawliet.goto(-120, 130)
lawliet.pendown()
boyka = Turtle('turtle')
boyka.speed('fastest')
boyka.color('green')
boyka.penup()
boyka.goto(-120, 100)
boyka.pendown()
screen = Screen()
while True:
turtle = choice([vince, lawliet, boyka])
turtle.forward(randint(1, 5))
if turtle.xcor() > 200:
break
turtle.color('gold')
screen.exitonclick()
这篇关于如何在 Python 海龟比赛中宣布获胜者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!