如何使一个功能停止工作,同时按下键并使行加粗?

时间:2019-04-16 19:37:25

标签: python pygame

我想让白线在按“ w”时停止移动。我该怎么办?而我该如何使其变得更大胆呢?白线需要绕圈移动,当用户按下“ w”时,白线需要停止移动。

PIVOT = (WINDOW_WIDTH/2, 150)
SWINGLENGTH = PIVOT[1]*0.8


def fill():

class Pendulum(pygame.sprite.Sprite):


pen = Pendulum()

TICK = USEREVENT + 2
pygame.time.set_timer(TICK, TIMETICK)

def input(events):
    for event in events:
        if event.type == QUIT:
            sys.exit(0)
        elif event.type == TICK:
            pen.update()

#while True:
 #   input(pygame.event.get())
  #  pygame.display.flip()


finish = False
while not finish:
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            finish = True

        while True:
            input(pygame.event.get())
            pygame.display.flip()

        if event.type == pygame.KEYDOWN and event.key == pygame.K_w:
            #if event.type == pygame.KEYDOWN and event.key == pygame.K_w:
            print('Forward')
            fill()
            #screen.fill((255, 0, 0))
            #input(pygame.event.get())
            pygame.display.flip()

1 个答案:

答案 0 :(得分:1)

您可以使用pygame.key.get_pressed()获取所有键的当前状态。

通过pygame.key.get_pressed()[pygame.K_w]检查是否按下了 w

例如

finish = False
while not finish:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            finish = True

    allKeys = pygame.key.get_pressed()

    # move the white line if 'w' is not pressed 
    if not allKeys[pygame.K_w]:            

        # [...]

    pygame.display.flip()