Reputation: 127
I am making a game in python called pong.
Can I get 2 different turtles in turtle graphics to respond simultaneously to keybindings?
Here is the code:
import turtle
class paddle(turtle.Turtle):
def __init__(self, x_cord, keybindings):
super().__init__("square")
self.color("white")
self.penup()
self.goto(x_cord, 0)
self.turtlesize(stretch_wid=5, stretch_len=1, outline=1)
self.screen = turtle.Screen()
self.screen.bgcolor("black")
self.screen.tracer(0)
self.screen.listen()
self.screen.update()
def up():
self.goto(self.xcor(), self.ycor() + 10)
self.screen.update()
def down():
self.goto(self.xcor(), self.ycor() - 10)
self.screen.update()
self.screen.onkey(up, keybindings[0])
self.screen.onkey(down, keybindings[1])
paddle_1 = paddle(-350, ["Up", "Down"])
paddle_2 = paddle(350, ["w", "s"])
food.screen.exitonclick()
Upvotes: 4
Views: 900
Reputation: 27577
This was once a problem I struggled with for a long time, and came to the conclusion that it's not possible (please prove me wrong, as I'm interested in the solution if there is one).
I've analyzed this great answer that explains how to bind two arrow keys for diagonal movement, but it only works one step at a time, just like how your code allows for simultaneous movement of the turtles as long as making them move one step at a time.
Anyways, that situation pushed me further into embracing the versatile Pygame python package.
Upvotes: 1