How can I move only one turtle at a time?

Nicole

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!

cdlane

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.

edited at
0

Comments

0 comments
Login to comment

Related

I can connect to database only one time

How do I move the turtle in LOGO?

How can i activate .hover() function only on one element at time

How do I move values out of an array one at a time?

How can I schedule a job to run in the future, but only one time ever

How can I make my email show only one time per email?

How can I change turtle images in order every time I press the same button in Python?

Why can only one player move at a time Pygame

How can i avoid adding duplicates into array and only have one value at time for specific attribute?

how I can limit the call to only one time for method "utilities.DecryptStringFromBase64String"

How can I queue up URLSession.DataTaskPublisher requests so that only one is made at a time?

Python server: How can I continously queue tasks while making sure only one at a time is processed

How can i use spread only one time?

How can I print the 'date received' only one time while using a for loop?

I can move back or forth in line only 1 character a time. How I can fix it?

How can I move down one directory

How to move the turtle where I want to go

How can I display repeated values only one time and have '-' if it repeats

How do I get the Turtle canvas screen to move with a Turtle module?

How can I make my turtle move to my cursor?

How can i use the turtle module to move up?

How i can show a toast message only one time per click for the current activity

Login issue, I need to double click to in order to login, how can i fix that to only click one time

how can I call a method only one time in a class? (first time when add "new")

My code receives the message from the queue multiple time, I want to receive it only one time. How can I do it?

How can I only open one popup at a time based on a map function in react?

How can I make it so that only one of the draggable objects in my game can be moved at a time?

How can I move one border down

How can I only pull in data for radio group filtering one time instead of for each entry in the database?