How do I run two turtles in python simultaneously?
I'm trying to make two turtles move together instead of one moving subsequently after the other. For example:
a = turtle.Turtle()
b = turtle.Turtle()
a.forward(100)
b.forward(100)
But this only makes them move one after the other.
Is there a way to make them move together simultaneously?
python python-3.x turtle-graphics simultaneous
add a comment |
I'm trying to make two turtles move together instead of one moving subsequently after the other. For example:
a = turtle.Turtle()
b = turtle.Turtle()
a.forward(100)
b.forward(100)
But this only makes them move one after the other.
Is there a way to make them move together simultaneously?
python python-3.x turtle-graphics simultaneous
How is turtle class defined? Could you provide the code?
– Banghua Zhao
Nov 8 '18 at 0:08
In SO you should not add [ANSWERED] or similar in the title of the question, you must mark as correct the answer that is the solution to your problem, if no answer will help you then we invite you to publish your solution and mark it as correct.
– eyllanesc
Nov 15 '18 at 22:32
add a comment |
I'm trying to make two turtles move together instead of one moving subsequently after the other. For example:
a = turtle.Turtle()
b = turtle.Turtle()
a.forward(100)
b.forward(100)
But this only makes them move one after the other.
Is there a way to make them move together simultaneously?
python python-3.x turtle-graphics simultaneous
I'm trying to make two turtles move together instead of one moving subsequently after the other. For example:
a = turtle.Turtle()
b = turtle.Turtle()
a.forward(100)
b.forward(100)
But this only makes them move one after the other.
Is there a way to make them move together simultaneously?
python python-3.x turtle-graphics simultaneous
python python-3.x turtle-graphics simultaneous
edited Nov 15 '18 at 22:30
eyllanesc
84.1k103562
84.1k103562
asked Nov 7 '18 at 23:29
rasabirasabi
385
385
How is turtle class defined? Could you provide the code?
– Banghua Zhao
Nov 8 '18 at 0:08
In SO you should not add [ANSWERED] or similar in the title of the question, you must mark as correct the answer that is the solution to your problem, if no answer will help you then we invite you to publish your solution and mark it as correct.
– eyllanesc
Nov 15 '18 at 22:32
add a comment |
How is turtle class defined? Could you provide the code?
– Banghua Zhao
Nov 8 '18 at 0:08
In SO you should not add [ANSWERED] or similar in the title of the question, you must mark as correct the answer that is the solution to your problem, if no answer will help you then we invite you to publish your solution and mark it as correct.
– eyllanesc
Nov 15 '18 at 22:32
How is turtle class defined? Could you provide the code?
– Banghua Zhao
Nov 8 '18 at 0:08
How is turtle class defined? Could you provide the code?
– Banghua Zhao
Nov 8 '18 at 0:08
In SO you should not add [ANSWERED] or similar in the title of the question, you must mark as correct the answer that is the solution to your problem, if no answer will help you then we invite you to publish your solution and mark it as correct.
– eyllanesc
Nov 15 '18 at 22:32
In SO you should not add [ANSWERED] or similar in the title of the question, you must mark as correct the answer that is the solution to your problem, if no answer will help you then we invite you to publish your solution and mark it as correct.
– eyllanesc
Nov 15 '18 at 22:32
add a comment |
2 Answers
2
active
oldest
votes
Is there a way to make them move together simultaneously?
The best we can hope to do is make them appear to move simultaneously. Below are three increasingly complex approaches to this problem. But first, let's establish our baseline code, two turtles heading at each other and stopping when they meet at the origin:
from turtle import Screen, Turtle
screen = Screen()
a = Turtle('square', visible=False)
a.speed('slow')
a.color('red')
a.penup()
a.setx(-300)
a.setheading(0)
a.pendown()
a.showturtle()
b = Turtle('circle', visible=False)
b.speed('slow')
b.color('green')
b.penup()
b.setx(300)
b.setheading(180)
b.pendown()
b.showturtle()
### Subsequent variations start here ###
a.forward(300)
b.forward(300)
### Subsequent variations end here ###
screen.mainloop()
The above doesn't do what we want as one turtle moves and then the other. For our first variation, we simply chop up the motion into smaller units and alternate:
###
for _ in range(300):
a.forward(1)
b.forward(1)
###
Our next variation uses timer events to control the motion of the two turtles:
###
def move(turtle):
turtle.forward(1)
if turtle.distance(0, 0) > 1 :
screen.ontimer(lambda t=turtle: move(t), 50)
move(a)
move(b)
###
Our final variation uses threading to independently control the two turtles. Each turtle is a thread and there's a third, main thread that handles all the graphics operations for the turtle threads. This is needed as turtle operates atop tkinter which has issues handling graphics from secondary threads:
###
from threading import Thread, active_count
from queue import Queue
QUEUE_SIZE = 1
def process_queue():
while not actions.empty():
action, *arguments = actions.get()
action(*arguments)
if active_count() > 1:
screen.ontimer(process_queue, 100)
actions = Queue(QUEUE_SIZE) # a thread-safe data structure
def move(turtle):
while turtle.distance(0, 0) > 1:
actions.put((turtle.forward, 1))
Thread(target=move, args=[a], daemon=True).start()
Thread(target=move, args=[b], daemon=True).start()
process_queue()
###
Thanks! Since python is a pretty basic (but strong) language, I'm sticking to making them appear to be moving "simultaneously".
– rasabi
Nov 15 '18 at 22:17
add a comment |
You can't do this as a guaranteed part of the Turtle interface. You could try doing these in parallel processes, but there's no guarantee of simultaneous movement.
Some Python run-time systems handle parallel processes with sequential time-slicing of a single process. You may also find that one move completes while the other process is initializing. If you want to try tighter management, build two processes that hold on a process lock; have the main program release both locks in a critical section ... and you might get something close to the desired functionality.
If you're trying to do something this graphically mature, you might consider a larger graphics package, such as PyGame.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199459%2fhow-do-i-run-two-turtles-in-python-simultaneously%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is there a way to make them move together simultaneously?
The best we can hope to do is make them appear to move simultaneously. Below are three increasingly complex approaches to this problem. But first, let's establish our baseline code, two turtles heading at each other and stopping when they meet at the origin:
from turtle import Screen, Turtle
screen = Screen()
a = Turtle('square', visible=False)
a.speed('slow')
a.color('red')
a.penup()
a.setx(-300)
a.setheading(0)
a.pendown()
a.showturtle()
b = Turtle('circle', visible=False)
b.speed('slow')
b.color('green')
b.penup()
b.setx(300)
b.setheading(180)
b.pendown()
b.showturtle()
### Subsequent variations start here ###
a.forward(300)
b.forward(300)
### Subsequent variations end here ###
screen.mainloop()
The above doesn't do what we want as one turtle moves and then the other. For our first variation, we simply chop up the motion into smaller units and alternate:
###
for _ in range(300):
a.forward(1)
b.forward(1)
###
Our next variation uses timer events to control the motion of the two turtles:
###
def move(turtle):
turtle.forward(1)
if turtle.distance(0, 0) > 1 :
screen.ontimer(lambda t=turtle: move(t), 50)
move(a)
move(b)
###
Our final variation uses threading to independently control the two turtles. Each turtle is a thread and there's a third, main thread that handles all the graphics operations for the turtle threads. This is needed as turtle operates atop tkinter which has issues handling graphics from secondary threads:
###
from threading import Thread, active_count
from queue import Queue
QUEUE_SIZE = 1
def process_queue():
while not actions.empty():
action, *arguments = actions.get()
action(*arguments)
if active_count() > 1:
screen.ontimer(process_queue, 100)
actions = Queue(QUEUE_SIZE) # a thread-safe data structure
def move(turtle):
while turtle.distance(0, 0) > 1:
actions.put((turtle.forward, 1))
Thread(target=move, args=[a], daemon=True).start()
Thread(target=move, args=[b], daemon=True).start()
process_queue()
###
Thanks! Since python is a pretty basic (but strong) language, I'm sticking to making them appear to be moving "simultaneously".
– rasabi
Nov 15 '18 at 22:17
add a comment |
Is there a way to make them move together simultaneously?
The best we can hope to do is make them appear to move simultaneously. Below are three increasingly complex approaches to this problem. But first, let's establish our baseline code, two turtles heading at each other and stopping when they meet at the origin:
from turtle import Screen, Turtle
screen = Screen()
a = Turtle('square', visible=False)
a.speed('slow')
a.color('red')
a.penup()
a.setx(-300)
a.setheading(0)
a.pendown()
a.showturtle()
b = Turtle('circle', visible=False)
b.speed('slow')
b.color('green')
b.penup()
b.setx(300)
b.setheading(180)
b.pendown()
b.showturtle()
### Subsequent variations start here ###
a.forward(300)
b.forward(300)
### Subsequent variations end here ###
screen.mainloop()
The above doesn't do what we want as one turtle moves and then the other. For our first variation, we simply chop up the motion into smaller units and alternate:
###
for _ in range(300):
a.forward(1)
b.forward(1)
###
Our next variation uses timer events to control the motion of the two turtles:
###
def move(turtle):
turtle.forward(1)
if turtle.distance(0, 0) > 1 :
screen.ontimer(lambda t=turtle: move(t), 50)
move(a)
move(b)
###
Our final variation uses threading to independently control the two turtles. Each turtle is a thread and there's a third, main thread that handles all the graphics operations for the turtle threads. This is needed as turtle operates atop tkinter which has issues handling graphics from secondary threads:
###
from threading import Thread, active_count
from queue import Queue
QUEUE_SIZE = 1
def process_queue():
while not actions.empty():
action, *arguments = actions.get()
action(*arguments)
if active_count() > 1:
screen.ontimer(process_queue, 100)
actions = Queue(QUEUE_SIZE) # a thread-safe data structure
def move(turtle):
while turtle.distance(0, 0) > 1:
actions.put((turtle.forward, 1))
Thread(target=move, args=[a], daemon=True).start()
Thread(target=move, args=[b], daemon=True).start()
process_queue()
###
Thanks! Since python is a pretty basic (but strong) language, I'm sticking to making them appear to be moving "simultaneously".
– rasabi
Nov 15 '18 at 22:17
add a comment |
Is there a way to make them move together simultaneously?
The best we can hope to do is make them appear to move simultaneously. Below are three increasingly complex approaches to this problem. But first, let's establish our baseline code, two turtles heading at each other and stopping when they meet at the origin:
from turtle import Screen, Turtle
screen = Screen()
a = Turtle('square', visible=False)
a.speed('slow')
a.color('red')
a.penup()
a.setx(-300)
a.setheading(0)
a.pendown()
a.showturtle()
b = Turtle('circle', visible=False)
b.speed('slow')
b.color('green')
b.penup()
b.setx(300)
b.setheading(180)
b.pendown()
b.showturtle()
### Subsequent variations start here ###
a.forward(300)
b.forward(300)
### Subsequent variations end here ###
screen.mainloop()
The above doesn't do what we want as one turtle moves and then the other. For our first variation, we simply chop up the motion into smaller units and alternate:
###
for _ in range(300):
a.forward(1)
b.forward(1)
###
Our next variation uses timer events to control the motion of the two turtles:
###
def move(turtle):
turtle.forward(1)
if turtle.distance(0, 0) > 1 :
screen.ontimer(lambda t=turtle: move(t), 50)
move(a)
move(b)
###
Our final variation uses threading to independently control the two turtles. Each turtle is a thread and there's a third, main thread that handles all the graphics operations for the turtle threads. This is needed as turtle operates atop tkinter which has issues handling graphics from secondary threads:
###
from threading import Thread, active_count
from queue import Queue
QUEUE_SIZE = 1
def process_queue():
while not actions.empty():
action, *arguments = actions.get()
action(*arguments)
if active_count() > 1:
screen.ontimer(process_queue, 100)
actions = Queue(QUEUE_SIZE) # a thread-safe data structure
def move(turtle):
while turtle.distance(0, 0) > 1:
actions.put((turtle.forward, 1))
Thread(target=move, args=[a], daemon=True).start()
Thread(target=move, args=[b], daemon=True).start()
process_queue()
###
Is there a way to make them move together simultaneously?
The best we can hope to do is make them appear to move simultaneously. Below are three increasingly complex approaches to this problem. But first, let's establish our baseline code, two turtles heading at each other and stopping when they meet at the origin:
from turtle import Screen, Turtle
screen = Screen()
a = Turtle('square', visible=False)
a.speed('slow')
a.color('red')
a.penup()
a.setx(-300)
a.setheading(0)
a.pendown()
a.showturtle()
b = Turtle('circle', visible=False)
b.speed('slow')
b.color('green')
b.penup()
b.setx(300)
b.setheading(180)
b.pendown()
b.showturtle()
### Subsequent variations start here ###
a.forward(300)
b.forward(300)
### Subsequent variations end here ###
screen.mainloop()
The above doesn't do what we want as one turtle moves and then the other. For our first variation, we simply chop up the motion into smaller units and alternate:
###
for _ in range(300):
a.forward(1)
b.forward(1)
###
Our next variation uses timer events to control the motion of the two turtles:
###
def move(turtle):
turtle.forward(1)
if turtle.distance(0, 0) > 1 :
screen.ontimer(lambda t=turtle: move(t), 50)
move(a)
move(b)
###
Our final variation uses threading to independently control the two turtles. Each turtle is a thread and there's a third, main thread that handles all the graphics operations for the turtle threads. This is needed as turtle operates atop tkinter which has issues handling graphics from secondary threads:
###
from threading import Thread, active_count
from queue import Queue
QUEUE_SIZE = 1
def process_queue():
while not actions.empty():
action, *arguments = actions.get()
action(*arguments)
if active_count() > 1:
screen.ontimer(process_queue, 100)
actions = Queue(QUEUE_SIZE) # a thread-safe data structure
def move(turtle):
while turtle.distance(0, 0) > 1:
actions.put((turtle.forward, 1))
Thread(target=move, args=[a], daemon=True).start()
Thread(target=move, args=[b], daemon=True).start()
process_queue()
###
edited Nov 8 '18 at 18:41
answered Nov 8 '18 at 18:27
cdlanecdlane
19.5k21245
19.5k21245
Thanks! Since python is a pretty basic (but strong) language, I'm sticking to making them appear to be moving "simultaneously".
– rasabi
Nov 15 '18 at 22:17
add a comment |
Thanks! Since python is a pretty basic (but strong) language, I'm sticking to making them appear to be moving "simultaneously".
– rasabi
Nov 15 '18 at 22:17
Thanks! Since python is a pretty basic (but strong) language, I'm sticking to making them appear to be moving "simultaneously".
– rasabi
Nov 15 '18 at 22:17
Thanks! Since python is a pretty basic (but strong) language, I'm sticking to making them appear to be moving "simultaneously".
– rasabi
Nov 15 '18 at 22:17
add a comment |
You can't do this as a guaranteed part of the Turtle interface. You could try doing these in parallel processes, but there's no guarantee of simultaneous movement.
Some Python run-time systems handle parallel processes with sequential time-slicing of a single process. You may also find that one move completes while the other process is initializing. If you want to try tighter management, build two processes that hold on a process lock; have the main program release both locks in a critical section ... and you might get something close to the desired functionality.
If you're trying to do something this graphically mature, you might consider a larger graphics package, such as PyGame.
add a comment |
You can't do this as a guaranteed part of the Turtle interface. You could try doing these in parallel processes, but there's no guarantee of simultaneous movement.
Some Python run-time systems handle parallel processes with sequential time-slicing of a single process. You may also find that one move completes while the other process is initializing. If you want to try tighter management, build two processes that hold on a process lock; have the main program release both locks in a critical section ... and you might get something close to the desired functionality.
If you're trying to do something this graphically mature, you might consider a larger graphics package, such as PyGame.
add a comment |
You can't do this as a guaranteed part of the Turtle interface. You could try doing these in parallel processes, but there's no guarantee of simultaneous movement.
Some Python run-time systems handle parallel processes with sequential time-slicing of a single process. You may also find that one move completes while the other process is initializing. If you want to try tighter management, build two processes that hold on a process lock; have the main program release both locks in a critical section ... and you might get something close to the desired functionality.
If you're trying to do something this graphically mature, you might consider a larger graphics package, such as PyGame.
You can't do this as a guaranteed part of the Turtle interface. You could try doing these in parallel processes, but there's no guarantee of simultaneous movement.
Some Python run-time systems handle parallel processes with sequential time-slicing of a single process. You may also find that one move completes while the other process is initializing. If you want to try tighter management, build two processes that hold on a process lock; have the main program release both locks in a critical section ... and you might get something close to the desired functionality.
If you're trying to do something this graphically mature, you might consider a larger graphics package, such as PyGame.
answered Nov 7 '18 at 23:47
PrunePrune
45.4k143559
45.4k143559
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199459%2fhow-do-i-run-two-turtles-in-python-simultaneously%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How is turtle class defined? Could you provide the code?
– Banghua Zhao
Nov 8 '18 at 0:08
In SO you should not add [ANSWERED] or similar in the title of the question, you must mark as correct the answer that is the solution to your problem, if no answer will help you then we invite you to publish your solution and mark it as correct.
– eyllanesc
Nov 15 '18 at 22:32