Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

How do you make a collision detection for turtle in python?

I'm having a problem, When I use the code:

from turtle import Turtle, Screen

playGround = Screen()
playGround.screensize(500, 500)
playGround.title("Race")
x=0

run = Turtle("turtle")
run.speed("fastest")
run.color("blue")
run.penup()
run.setposition(250, 250)

follow = Turtle("turtle")
follow.speed("fastest")
follow.color("red")
follow.penup()
follow.setposition(-250, -250)

def k1():
    run.forward(10)

def k2():
    run.left(20)

def k3():
    run.right(45)

def k4():
    run.backward(20)


def follow_runner():
    if x==0:
        follow.setheading(follow.towards(run))
        follow.forward(0.7)
        playGround.ontimer(follow_runner, 10)
        
while 1==1:
    playGround.onkey(k1, "Up")  # the up arrow key
    playGround.onkey(k2, "Left")  # the left arrow key
    playGround.onkey(k3, "Right") #It's obvious  
    playGround.onkey(k4, "Down")

    playGround.listen()

    follow_runner()
    runx= run.xcor()
    runy= run.ycor()
    followx = follow.xcor()
    followy= follow.ycor()

    playGround.mainloop()
    if  runx==followx and runy==followy:
        playGround.clear()
        x=1

When the follow touches run, It just continues and it means that the code failed.
I want to get it to work and make the arrow keys able to hold. Can you help me? Please don't close this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want the turtle to move in key hold use onkeypress(). Also, note that using a while loop is not necessary.

Here use this:

from turtle import Turtle, Screen
import turtle
playGround = Screen()
playGround.screensize(500, 500)
playGround.title("Race")
x=0

run = Turtle("turtle")
run.speed("fastest")
run.color("blue")
run.penup()
run.setposition(250, 250)


follow = Turtle("turtle")
follow.speed("fastest")
follow.color("red")
follow.penup()
follow.setposition(-250, -250)


def k1():
    run.forward(10)

def k2():
    run.left(20)

def k3():
    run.right(45)

def k4():
    run.backward(20)


def follow_runner():
    global x
    runx= run.xcor()
    runy= run.ycor()
    followx = follow.xcor()
    followy= follow.ycor()

    if x==0:
        follow.setheading(follow.towards(run))
        follow.forward(0.7)
        playGround.ontimer(follow_runner, 10)
    
    if int(runx) == int(followx) or int(runy) == int(followy):
       
        x=1
        playGround.clear()


playGround.onkeypress(k1, "Up")  # the up arrow key
playGround.onkeypress(k2, "Left")  # the left arrow key
playGround.onkeypress(k3, "Right") #It's obvious  
playGround.onkeypress(k4, "Down")


follow_runner()

playGround.listen()
playGround.mainloop()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...