Question

So here is the code I have, is there a way to code so that when the ISS and rocket meet (at the same position) destory the window and create a new Tkinter window?

from turtle import *

def move(thing, distance):
    thing.circle(250, distance)

def main():
    rocket = Turtle()
    ISS = Turtle()
    rocket.speed(10)
    ISS.speed(10)
    counter = 1
    title("ISS")
    screensize(750, 750)
    ISS.hideturtle()
    rocket.hideturtle()
    ISS.penup()
    ISS.left(90)
    ISS.fd(250)
    ISS.left(90)
    ISS.showturtle()
    ISS.pendown()
    rocket.penup()
    rocket.fd(250)
    rocket.left(90)
    rocket.showturtle()
    rocket.pendown()

    while counter == 1:
        move(ISS, 3)
        move(rocket, 4)

Thank You!!

Était-ce utile?

La solution

http://docs.python.org/2/library/turtle.html#turtle.position

"Return the turtle’s current location (x,y) (as a Vec2D vector)."

However, due to floating point errors, you should consider them overlapping even if they are very slightly apart, e.g.

epsilon = 0.000001

if abs(ISS.xcor() - rocket.xcor()) < epsilon and abs(ISS.ycor() - rocket.ycor()) < epsilon:
    do stuff

If you want to pretend they are circles and ISS has a radius of r1 and the rocket has a radius of r2, then you can measure distance like:

sqrt((ISS.xcor() - rocket.xcor())**2 + (ISS.ycor() - rocket.ycor())**2) < (r1 + r2)

If this is true, they are overlapping circles.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top