简体   繁体   中英

How to indicate which turtle won the race

I have a simple turtle racing game. I am trying to add code to declare/display which of four turtles won the race when a turtle reaches the finish line (x position 190). But I get errors. Could you please explain how I might modify this code so that it works?

    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()

I have adjusted some things, but you really need to do a turtle course and a python course. The thing is that I can see that you are trying to achieve certain things which you have obviously seen others do or read about, but you are implementing them incorrectly. It would take too long for me to list every error and mistake but I have changed your code to make it at least run.

The best that I can say is that you need to have a look at the following: lists, for loops and you need to do a Python course. I can recommend Sololearn which is an online coding course.

The modified code is here:

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()

Also, I would like to point out that this sort of question is not suitable for StackOverflow. You might want to read the page on what sort of questions to ask. It seems to me that you have just created an account to ask this question to get your code fixed, that sort of question should be asked on the CodeReview section of StackExchange where you can get others to fix and improve your code. StackOverflow is for asking questions which other people will view in the future to help themselves, but this question cannot and will not help anybody in the future.

Your code seems to be of two minds, importing turtle in one way at the top but reimporting it another way later. It creates the racers as individuals but in th next step assumes they're in an array (which they are not.) Let's rework this this code to be more consistent and get it to basically run:

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()

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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