简体   繁体   中英

Python turtle race: go to a point and turn back

Hi I have another question: how do you make a turtle go to a point and turn back? I want the turtles to go to the point with x coordinate 160 and turn back, continuing the race at the same time (meaning that they don't all move at the same speed).

Here is my code:

import turtle
from random import randint

turtle.speed(0)

turtle.penup()
turtle.goto(-140,140)

for step in range(15):
  turtle.write(step,align='center')
  turtle.right(90)
  turtle.forward(10)
  turtle.pendown()
  turtle.forward(180)
  turtle.penup()
  turtle.backward(190)
  turtle.left(90)
  turtle.forward(20)

roy = turtle.Turtle()
roy.color('red')
roy.shape('turtle')

roy.penup()
roy.goto(-160,100)
roy.pendown()

bob = turtle.Turtle()
bob.color('blue')
bob.shape('turtle')

bob.penup()
bob.goto(-160,70)
bob.pendown()

oreo = turtle.Turtle()
oreo.color('orange')
oreo.shape('turtle')

oreo.penup()
oreo.goto(-160,40)
oreo.pendown()

yay = turtle.Turtle()
yay.color('yellow')
yay.shape('turtle')

yay.penup()
yay.goto(-160,10)
yay.pendown()

go = turtle.Turtle()
go.color('green')
go.shape('turtle')

go.penup()
go.goto(-160,-20)
go.pendown()

for turn in range(100):
  roy.forward(randint(1,5))
  bob.forward(randint(1,5))
  oreo.forward(randint(1,5))
  yay.forward(randint(1,5))
  go.forward(randint(1,5))

  if roy.xcor() >= 160:
    roy.left(180)
  else:
    break
  if bob.xcor() >= 160:
    bob.left(180)
  else:
    break
  if yay.xcor() >= 160:
    yay.left(180)
  else:
    break
  if oreo.xcor() >= 160:
    oreo.left(180)
  else:
    break
  if go.xcor() >= 160:
    go.left(180)
  else:
    break

I know there's something wrong with my "if" loop in the end, but I don't know what happened.

By the way my turtle turns out like this

Help is appreciated!

Your else break is a problem, it ends the race when the first turtle tries to turn around. Also, left(180) can be problematic if the turtle overshoots the turn point a bit as it will start to spin. Using setheading(180) is safer as it will avoid the spin and send the turtle back towards the starting line:

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

screen = Screen()

turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')
turtle.penup()
turtle.goto(-140, 140)

for step in range(15):
    turtle.write(step, align='center')
    turtle.right(90)
    turtle.forward(10)
    turtle.pendown()
    turtle.forward(180)
    turtle.penup()
    turtle.backward(190)
    turtle.left(90)
    turtle.forward(20)

roy = Turtle()
roy.color('red')
roy.shape('turtle')

roy.penup()
roy.goto(-160, 100)
roy.pendown()

bob = Turtle()
bob.color('blue')
bob.shape('turtle')

bob.penup()
bob.goto(-160, 70)
bob.pendown()

oreo = Turtle()
oreo.color('orange')
oreo.shape('turtle')

oreo.penup()
oreo.goto(-160, 40)
oreo.pendown()

yay = Turtle()
yay.color('yellow')
yay.shape('turtle')

yay.penup()
yay.goto(-160, 10)
yay.pendown()

go = Turtle()
go.color('green')
go.shape('turtle')

go.penup()
go.goto(-160, -20)
go.pendown()

while True:
    racer = choice([roy, bob, oreo, yay, go])
    racer.forward(randint(1, 5))

    if racer.xcor() > 160:
        racer.setheading(180)
    elif racer.xcor() < -160:
        break  # we have a winner!

screen.mainloop()

在此处输入图像描述

Your while True: doesn't really belong in an event-driven environment like turtle and can be replaced with an ontimer() event. But, one step at a time.

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