如何指示哪只乌龟获胜

时间:2019-05-01 09:13:02

标签: python python-3.x turtle-graphics

我有一个简单的乌龟赛车游戏。我正在尝试添加代码以声明/显示当一只乌龟到达终点线(x位置190)时哪四只乌龟赢得了比赛。但是我得到了错误。您能解释一下如何修改此代码以使其起作用吗?

    from turtle import *
    from random import randint
    tur1=Turtle()
    tur1.color("red")
    tur1.shape("turtle")
    tur1.penup()
    tur1.goto(-155,100)
    tur1.pendown()

    tur2=Turtle()
    tur2.color("blue")
    tur2.shape("turtle")
    tur2.penup()
    tur2.goto(-155,70)
    tur2.pendown()

    tur3=Turtle()
    tur3.color("green")
    tur3.shape("turtle")
    tur3.penup()
    tur3.goto(-155,40)
    tur3.pendown()
    tur4=Turtle()
    tur4.color("yellow")
    tur4.shape("turtle")
    tur4.penup()
    tur4.goto(-155,10)
    tur4.pendown()
    """if tur[i]==190(x value): 
      finish_text.write( tur[i] won the race)
    Here I use a for loop to name the winner turtle..tur[i]"""

    for turn in range(102):
        for i in range(4):       "not sure how to set code this part 
        tur[1].forward(randint(1,5))
        tur[2].forward(randint(1,5))
        tur[3].forward(randint(1,5))
        tur[4].forward(randint(1,5))

    import turtle
    wn=turtle.Screen()
    wn.bgcolor("black")
    finish=turtle.Turtle()
    finish.up()
    finish.speed(0)
    finish.goto(190,140)
    finish.down()
    finish.right(90)
    finish.width(3)

    for i in range(16):
        finish.color("blue")
        finish.forward(5)
        finish.color("red")
        finish.forward(5)

    finish_text=Turtle()
    finish_text.up()
    finish_text.speed(10)
    finish_text.color("white")
    finish_text.goto(160,150)
    finish_text.write("Finishing-Line",font=("Times New Roman",12,"bold"))

    finish_text.goto(160,250)
    "Here I am tring to announce which turtle won the race"
    finish_text.write("turtle[i] won the race")


    wn.mainloop()

2 个答案:

答案 0 :(得分:0)

我已经进行了一些调整,但是您确实需要做一个乌龟课程和一个python课程。事实是,我可以看到您正在尝试实现某些明显已经看过别人的事情或已经读过的事情,但是您在实现它们时却不正确。我列出所有错误和错误的时间都太长了,但是我已经更改了您的代码以使其至少可以运行。

我能说的最好的是,您需要查看以下内容:列表,循环,并且需要学习Python课程。我可以推荐Sololearn,这是一门在线编码课程。

修改后的代码在这里:

from turtle import *
from random import randint
tur1=Turtle()
tur1.color("red")
tur1.shape("turtle")
tur1.penup()
tur1.goto(-155,100)
tur1.pendown()

tur2=Turtle()
tur2.color("blue")
tur2.shape("turtle")
tur2.penup()
tur2.goto(-155,70)
tur2.pendown()

tur3=Turtle()
tur3.color("green")
tur3.shape("turtle")
tur3.penup()
tur3.goto(-155,40)
tur3.pendown()
tur4=Turtle()
tur4.color("yellow")
tur4.shape("turtle")
tur4.penup()
tur4.goto(-155,10)
tur4.pendown()
"""if tur[i]==190(x value): 
  finish_text.write( tur[i] won the race)
Here I use a for loop to name the winner turtle..tur[i]"""

for turn in range(102): # why 102...
    #for i in range(4):     # I see what you were trying to do, but no.   
    tur1.forward(randint(1,5)) # FIX: no square brackets
    tur2.forward(randint(1,5))
    tur3.forward(randint(1,5))
    tur4.forward(randint(1,5))

#import turtle # No need for this
wn=turtle.Screen()
wn.bgcolor("black")
finish=turtle.Turtle()
finish.up()
finish.speed(0)
finish.goto(190,140)
finish.down()
finish.right(90)
finish.width(3)

for i in range(16):
    finish.color("blue")
    finish.forward(5)
    finish.color("red")
    finish.forward(5)

finish_text=Turtle()
finish_text.up()
finish_text.speed(10)
finish_text.color("white")
finish_text.goto(160,150)
finish_text.write("Finishing-Line",font=("Times New Roman",12,"bold"))

finish_text.goto(160,250)
"Here I am tring to announce which turtle won the race"
# Well you never detected which turtle won the race...
'''
To detect which turtle won the race, you would have to record how far each
one moves and which one got the correct position first... but you do not record
that.
'''
finish_text.write("turtle[i] won the race") # what is 'i'?
# 'i' will always be 3, because at the end of your 'for' loop 'i' is 3
# before it exits the for loop.


wn.mainloop()

此外,我想指出这种问题不适用于StackOverflow。您可能需要阅读有关提出何种问题的页面。在我看来,您刚刚创建了一个帐户来询问该问题以修复您的代码,应该在StackExchange的CodeReview部分中询问该问题,在这里您可以让其他人修复和改进您的代码。 StackOverflow用于询问其他人将来会帮助自己解决的问题,但是这个问题不能也不会将来帮助任何人。

答案 1 :(得分:0)

您的代码似乎有两种想法,在顶部以一种方式导入乌龟,但在以后以另一种方式重新导入。它将赛车手创建为个人,但在下一步中假定他们位于数组中(不是)。让我们重新编写此代码,使其更加一致并使其基本运行:

from turtle import Screen, Turtle, mainloop
from random import randint

COLORS = ['red', 'blue', 'green', 'yellow']
FONT = ("Times New Roman", 12, 'bold')

screen = Screen()
screen.bgcolor('black')

finish = Turtle(visible=False)
finish.speed('fastest')
finish.penup()

finish.color('white')
finish.goto(160, 150)
finish.write("Finish-Line", font=FONT)

finish.width(3)
finish.goto(190, 140)
finish.right(90)
finish.pendown()

for _ in range(16):
    finish.color('blue')
    finish.forward(5)
    finish.color('red')
    finish.forward(5)

finish.penup()
finish.goto(160, 250)

turtles = []

for gate, color in enumerate(COLORS):
    turtle = Turtle('turtle')
    turtle.color(color)
    turtle.penup()
    turtle.goto(-155, 10 + 30 * gate)
    turtle.pendown()

    turtles.append(turtle)

winner = None

while not winner:
    for turtle in turtles:
        turtle.forward(randint(1, 5))
        if turtle.xcor() >= 190:
            winner = turtle
            break  # we have a winner!
    else: # no break
        continue

    break

finish.pencolor(winner.pencolor())
finish.write("{} won the race!".format(winner.pencolor()), font=FONT)

mainloop()

enter image description here