如何阻止精灵使用Pygame对角移动?

时间:2016-11-20 22:35:30

标签: python pygame sprite game-development

我正在制作蛇游戏。每当我按箭头键向一个方向移动然后按另一个方向的键,蛇就会对角线。 (例如,如果我先按下然后按下。)即使释放了上一个键,也会发生这种情况。我怎么能阻止这个?

# x and y marks the player's position

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
        x_change = -10
    if event.key == pygame.K_RIGHT:
        x_change = 10
    if event.key == pygame.K_UP:
        y_change = -10
    if event.key == pygame.K_DOWN:
        y_change = 10
    x += x_change
    y += y_change

1 个答案:

答案 0 :(得分:3)

我添加了x_change = 0if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x_change = -10 y_change = 0 if event.key == pygame.K_RIGHT: x_change = 10 y_change = 0 if event.key == pygame.K_UP: y_change = -10 x_change = 0 if event.key == pygame.K_DOWN: y_change = 10 x_change = 0 x += x_change y += y_change 来重置值,只保持正交移动。

parser = argparse.ArgumentParser(description=scriptdesc)
parser.add_argument("-l", "--list", help="Show current running sesssions", dest="l_list", type=str, default=None)