python 3.6为一个动作多次按键

python 3.6为一个动作多次按键,python,python-3.x,turtle-graphics,Python,Python 3.x,Turtle Graphics,我目前正试图制作一个只使用python及其内置插件的游戏,而不是pygame。我想知道是否可以按下两个键使角色沿对角线移动。这是我目前的动作代码,我知道它看起来很糟糕,我显然还是个初学者 #movement functions and stuff def go_up(): char.direction = "up" def go_down(): char.direction = "down" def go_left(): char.direction = "left"

我目前正试图制作一个只使用python及其内置插件的游戏,而不是pygame。我想知道是否可以按下两个键使角色沿对角线移动。这是我目前的动作代码,我知道它看起来很糟糕,我显然还是个初学者

#movement functions and stuff
def go_up():
    char.direction = "up"

def go_down():
    char.direction = "down"

def go_left():
    char.direction = "left"

def go_right():
    char.direction = "right"

def go_up_left():
    char.direction = "up_left"

def move():

    if char.direction == "up":
        y = char.ycor()
        char.sety(y + speed)


    if char.direction == "down":
        y = char.ycor()
        char.sety(y - speed)


    if char.direction == "left":
        x = char.xcor()
        char.setx(x - speed)


    if char.direction == "right":
        x = char.xcor()
        char.setx(x + speed)

    if char.direction == "up_left":
        x = char.xcor()
        y = char.ycor()
        char.setx(x - speed)
        char.sety(y + speed)

# key bindings
wn.listen()
wn.onkeypress(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_left, "a")
wn.onkeypress(go_right, "d")
wn.onkeypress(go_up_left, "")

如您所见,我没有go_up_left的键绑定,因为这正是我需要帮助的地方。

使用pygame库,下面的代码演示了如何通过pygame.key.get_按下多个键


我希望有一种不使用pygame的方法,但是谢谢你的帮助,如果我找不到其他方法,那么我会使用这个。
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] and keys[pygame.K_RIGHT]:
    movementx+y+() #example to move up and right at the same time