저는 현재 PyGame 모듈을 사용하여 파이썬과 게임을하고 있습니다. 두 클래스, 게임 클래스와 자동차 클래스가 있습니다. 게임 클래스에는 게임 루프 메서드가 있습니다. 다른 메서드를 통해 루프를 만들었습니다. 하나는 자동차 클래스의 이벤트 메서드입니다. 프로그램을 실행하면 모든 것이 정상적으로로드됩니다. 그러나 객체를 이동하려고하면 키보드 입력이 처리 속도가 느려지고 이동 키를 스팸하면 일부 입력이 전혀 인식되지 않습니다.게임 루프가 입력을 처리하는 데 너무 느린 이유는 무엇입니까?
게임 루프를 구성하는 방법에 근본적인 문제가 있습니까?
def game_loop(self):
running = True
self.test_car = car()
while running:
pygame.display.set_caption("Project G")
self.event_handler()
self.screen.blit(self.background, (0,0))
self.test_car.event_handler()
self.test_car.update()
pygame.display.flip()
을 그리고이 자동차 클래스입니다 : 다음과 같이
게임 루프 방법은 간다 당신은 아마 하나 개의 루프가 pygame.event.get()
를 호출 가진해야
class car(game):
def __init__(self):
super(car, self).__init__()
self.init_x_pos = 100
self.init_y_pos = 100
self.x_speed = 0
self.y_speed = 0
self.load_img = load()
self.car_img = pygame.image.load(self.load_img.car_img)
def event_handler(self):
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_DOWN:
self.y_speed += 1
print "DOWN"
elif event.key == K_UP:
self.y_speed -= 1
print "UP"
elif event.key == K_RIGHT:
self.x_speed += 1
print "RIGHT"
elif event.key == K_LEFT:
self.x_speed -= 1
print "LEFT"
def update(self):
self.screen.blit(self.car_img, (self.init_x_pos + self.x_speed, self.init_y_pos + self.y_speed))
아마도 : http://stackoverflow.com/questions/6348952/handling-keyboard-events-in-python – paddy
@paddy : 아니오.이 질문은 키보드에 액세스하는 표준 방법이있는 파이 게임에 관한 것입니다. 다른 질문은 터미널에서 눌려진 키를 얻는 것과 관련이 있습니다. – icktoofay