简体   繁体   English

单个动作 python 3.6 的多个按键

[英]Multiple key presses for a single action python 3.6

Im currently attempting to make a game using only python and its built in plugins, not pygame.我目前正在尝试仅使用 python 及其内置插件而不是 pygame 来制作游戏。 I was wondering if it is possible to push two keys to make the character move diagonally.我想知道是否可以按两个键使角色沿对角线移动。 This is my current movement code, I know it looks bad, I'm still obviously a beginner.这是我目前的运动代码,我知道它看起来很糟糕,我显然还是一个初学者。

#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, "")

As you can see, I dont have the key bindings for "go_up_left" because that's what I need help with.如您所见,我没有“go_up_left”的键绑定,因为这是我需要帮助的。

use pygame lib and like this code shows how to handle multiple key pressing through pygame.key.get_pressed()使用 pygame lib 并且像这样的代码显示了如何通过pygame.key.get_pressed()处理多个按键

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM