Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

python - How to get if a key is pressed pygame


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The current state of the keys can be get by pygame.key.get_pressed().

Get the sate of the keys keys = pygame.key.get_pressed() and evaluate if the UP or DOWN is pressed by if keys[pygame.K_UP]: respectively if keys[pygame.K_DOWN]:, continuously in the main application loop:

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        # [...]
    if keys[pygame.K_DOWN]:
        # [...]

Note, the states which are returned by pygame.key.get_pressed() are updated when the events are handled by pygame.event.pump() or pygame.event.get().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...