【问题标题】:(PyGame) How can I make the character moving when I press a key?(PyGame) 如何让角色在按键时移动?
【发布时间】:2018-02-18 16:37:02
【问题描述】:

我知道之前有人问过它,但我无法让它工作,我正在使用空闲 3.6 和 PyGame。

这是代码。(我刚开始我一直在看一些教程,但除此之外我是 PyGame 的新手)

for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                location += 1

【问题讨论】:

    标签: python-3.x pygame


    【解决方案1】:

    您可以使用布尔标志来跟踪哪个键被按下释放。比如:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                pressedRight = True
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                pressedRight = False
    
    # Now in your game loop
    if pressedRight:
        location += 1
    

    基本上,这段代码模拟了一种方法来检查当前时间是否按下了键,从而允许您使用连续移动。

    【讨论】:

    • 我赞成...它不可见,因为我没有足够的代表
    • oahhhhhhhh okkk
    猜你喜欢
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    相关资源
    最近更新 更多