如何让多个键绑定在乌龟图形中同时工作



我正在用python制作一个名为pong的游戏。

我可以让乌龟图形中的两个不同乌龟同时对键绑定做出响应吗?

这是代码:

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()

这曾经是一个我纠结了很长时间的问题,得出的结论是这是不可能的(请证明我错了,因为我对解决方案感兴趣,如果有的话(

我已经分析了这个很好的答案,它解释了如何绑定两个箭头键进行对角线移动,但它一次只工作一步,就像你的代码允许海龟同时移动一样,只要让它们一步一步地移动。

无论如何,这种情况促使我进一步接受了多功能的Pygame python包。

最新更新