dimanche 3 septembre 2017

Python - How to check all the time if Turtle in Position

i'm new at Python 3x, and trying to code a small "Game". Currently i'm working with Turtle. So, i added 2 Turtles with turtle.Turtle(), and want to code, that it checks if for Example Turtle1 is in Turtle2 position.

import turtle
import random

turtle1 = turtle.Turtle()
turtle1.color("blue")
turtle1.shape("square")
turtle1.penup()

turtle2 = turtle.Turtle()
turtle2.color("red")
turtle2.shape("square")
turtle2.penup()

# ---

def k1():
    turtle1.forward(10)

def k2():
    turtle1.left(90)

def k3():
    turtle1.right(90)

turtle.onkeypress(k1, "Up")
turtle.onkey(k2, "Left")
turtle.onkey(k3, "Right")
turtle.listen()

# ---

def npos():
    xpos = random.randrange(0,100)
    ypos = random.randrange(0,100)
    random.Random()
    turtle2.setpos(xpos, ypos)

npos()
# ---   

t2pos = turtle2.pos()

if turtle1.distance(t2pos) < 5:
    npos()

So, i want to check all the time, if turtle1 is near the position of turtle2, and if yes, change turtle2 position with npos()

Thanks.

Aucun commentaire:

Enregistrer un commentaire