我目前正尝试仅使用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”的键绑定,因为这是我需要的帮助。
答案 0 :(得分:0)
使用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