Pygame 在运行时没有响应

问题描述 投票:0回答:1

您好,第一次访问该网站的用户,我遇到了 Pygame 没有响应的问题,并且不知道出了什么问题,我将非常感谢任何形式的帮助。

这是代码 抱歉,如果格式不正确

import pygame
pygame.init
screen = pygame.display.set_mode((500, 500))

while True:
    key = pygame.key.get_pressed()
    if key[pygame.K_a] == True:
        print('works')        
python pygame
1个回答
0
投票

您需要更新您的游戏循环。您提供的代码基本上是“挂起”的,因为

pygame
从不检查它的任何事件。

您可以通过从队列中获取游戏事件来更新逻辑循环。您还需要在顶部的

init
调用中添加括号

import pygame
pygame.init()  # Need to call this as a method. Added ()
screen = pygame.display.set_mode((500, 500))

while True:
    # You need to get events from the queue
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

    key = pygame.key.get_pressed()

    # You can remove `== True` in if 
    if key[pygame.K_a] == True: statements too
        print('works')

https://www.pygame.org/docs/ref/event.html#pygame.event.get

© www.soinside.com 2019 - 2024. All rights reserved.