Python键盘控件

时间:2016-06-10 00:08:34

标签: python keyboard pygame

这是我的代码,我遇到了麻烦,它是一个基本的侧卷轴游戏,但我的键盘控制定义有问题,它们似乎无法正常工作,我无法找到问题。

if keys[K_LEFT]:
    newmove= LEFT
    moveLeft(guy,10)
    climb(guy)      
if keys[K_RIGHT]:
    newmove=RIGHT
    moveRight(guy,10)
    climb(guy)
if keys[K_SPACE] and guy[ONGROUND]:
    guy[VY] = -14

else:
    frame=0

if move==newmove:
    frame=frame+0.1
    if frame>=len(pics[move]):
        frame=1
elif newmove!=-1:
    move=newmove
    frame=1

1 个答案:

答案 0 :(得分:0)

你试过这个吗?或类似的东西?

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    xChange = -5
                    print("Left")
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    xChange = 5
                    print("Right")
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    xChange = 0
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    xChange = 0