So, my program was working before I added the if statements on lines 27 and 42: if (currentTurtle == "") or (currentTurtle == "one"):
and if (currentTurtle == "") or (currentTurtle == "two"):
respectively. Before I added these checks, the turtles would only both move at the same time if the were really close to each other, since I used if statements to check the cursor's distances to the turtles. I tried adding the checks on lines 27 and 42 to only move one at a time, but then my turtles became unresponsive.
Here is my code:
from turtle import Screen, Turtle
screen = Screen()
turt1 = Turtle("turtle")
turt2 = Turtle("turtle")
turt1.speed(0)
turt2.speed(0)
turt1.shape('circle')
turt2.shape('circle')
turt1.color('green')
turt2.color('blue')
turt1.penup()
turt1.goto(-100,100)
turt2.penup()
turt2.goto(100,-100)
currentTurtle = ""
def resetCurrent():
currentTurtle = ""
def dragging(x, y):
if (x <= turt1.xcor() + 10) and (x >= turt1.xcor() - 10):
if (y <= turt1.ycor() + 10) and (y >= turt1.ycor() - 10):
if (currentTurtle == "") or (currentTurtle == "one"):
currentTurtle = "one"
elif (currentTurtle == "one"):
currentTurtle == ""
elif (currentTurtle == "one"):
currentTurtle == ""
if currentTurtle == "one":
if (x <= turt1.xcor() + 10) and (x >= turt1.xcor() - 10):
if (y <= turt1.ycor() + 10) and (y >= turt1.ycor() - 10):
turt1.goto(x, y)
def dragging2(x, y):
if (x <= turt2.xcor() + 10) and (x >= turt2.xcor() - 10):
if (y <= turt2.ycor() + 10) and (y >= turt2.ycor() - 10):
if (currentTurtle == "") or (currentTurtle == "two"):
currentTurtle = "two"
elif (currentTurtle == "two"):
currentTurtle = ""
elif (currentTurtle == "two"):
currentTurtle = ""
if currentTurtle == "two":
if (x <= turt2.xcor() + 10) and (x >= turt2.xcor() - 10):
if (y <= turt2.ycor() + 10) and (y >= turt2.ycor() - 10):
turt2.goto(x, y)
def main(): # This will run the program
screen.listen()
turt1.ondrag(dragging)
turt2.ondrag(dragging2)
screen.mainloop() # This will continue running main()
main()
Any help is greatly appreciated!
Your dragging()
code has become sufficiently complicated that I can't understand what it is you want versus what you are doing. I'm going to start over with a completely different, simpler approach:
from turtle import Screen, Turtle
from functools import partial
def dragging(tortoise, x, y):
for turtle in screen.turtles(): # disable event handlers inside handler
turtle.ondrag(None)
tortoise.goto(x, y)
for turtle in screen.turtles(): # reenable event handers on the way out
turtle.ondrag(partial(dragging, turtle))
def main():
turtle_1.ondrag(partial(dragging, turtle_1))
turtle_2.ondrag(partial(dragging, turtle_2))
screen.mainloop()
screen = Screen()
turtle_1 = Turtle('turtle')
turtle_1.shape('circle')
turtle_1.speed('fastest')
turtle_1.penup()
turtle_1.color('green')
turtle_1.goto(-100, 100)
turtle_2 = turtle_1.clone() # turtle_2 is a lot like turtle_1
turtle_2.color('blue')
turtle_2.goto(100, -100)
main()
Does this give you the control and behavior you desire? If not, let us know in your question what it is you want to happen, not just what the code doesn't do.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments