简体   繁体   English

如何在Python乌龟竞赛中宣布获胜者

[英]How to declare winner in Python turtle race

I'm creating a basic turtle program using the Python module 'turtle'. 我正在使用Python模块“ turtle”创建一个基本的turtle程序。 The only problem I come across is how to declare a winner. 我遇到的唯一问题是如何宣布获胜者。

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. 然后我使用了3种形状,并使用randint()将这些海龟向前移动以进行比赛。 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))

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. 但是,当我查看Turtle库时,没有内置函数可以这样做。 Is there any way to declare the winner for this race? 有什么办法宣布这场比赛的冠军吗?

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() . 您需要做的两件事是终点线(200)的x坐标和乌龟的x坐标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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM