识别Pygame KEYDOWN事件

时间:2018-12-08 18:03:31

标签: python pygame

我正在创建2048游戏,并且开发过程还很早,但是已经遇到了一个令我完全困惑的问题。 这是我的主要功能(忽略缩进错误,我是Stackoverflow的新手)

def main():

while True:

    #Process input
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            closeGame()

        if event.type == pygame.KEYDOWN and event.key == keyESC:
            closeGame()

        if event.type == pygame.KEYDOWN and event.key == keyLEFT:
            gameArray[3][2] = 1

    drawGame()

我希望在按下鼠标左键(该位置在其他地方用keyLEFT = (pygame.K_LEFT)定义)后,更改gameArray[3][2] = 1中提到的变量。

然后我具有绘制游戏的功能

def drawGame():
screen.fill(WHITE)
pygame.draw.rect(screen, BLACK, (0, 0, 450, 450), 0)
# Printing the rows
for i in range (4):
    # Printing the cells
    for j in range (4):
        pygame.draw.rect(screen, getColour(gameArray[i][j]), (tileGAP + ((tileSPACE) * j), tileGAP + (tileSPACE * i), tileWIDTH, tileWIDTH), 0)


pygame.display.flip()

我的问题是,为什么在按下左键后,gameArray [3] [2]的平方不改变? 抱歉,如果我真的设置不好,这是我的新手。

1 个答案:

答案 0 :(得分:1)

尝试根据pygame documentation将“ keyLEFT”替换为“ pygame.K_LEFT”。

也请参见此similar question

相关问题