내 PyGame 애플리케이션이 전혀 실행되지 않는 이유는 무엇입니까?

Dec 12 2020

간단한 파이 게임 프로그램이 있습니다.

#!/usr/bin/env python

import pygame
from pygame.locals import *

pygame.init()

win = pygame.display.set_mode((400,400))
pygame.display.set_caption("My first game")

하지만 실행하려고 할 때마다 다음과 같이 표시됩니다.

pygame 2.0.0 (SDL 2.0.12, python 3.8.3)
Hello from the pygame community. https://www.pygame.org/contribute.html

그리고 아무 일도 일어나지 않습니다. 이 프로그램을 실행할 수없는 이유는 무엇입니까?

답변

4 Rabbid76 Dec 12 2020 at 19:11

응용 프로그램이 잘 작동합니다. 그러나 응용 프로그램 루프를 구현하지 않았습니다.

#!/usr/bin/env python

import pygame
from pygame.locals import *

pygame.init()

win = pygame.display.set_mode((400,400))
pygame.display.set_caption("My first game")
clock = pygame.time.Clock()

run = True
while run:
    clock.tick(60)
    
    # handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # update game objects
    # [...]

    # clear display
    win.fill((0, 0, 0))

    # draw game objects
    # [...]

    # update display
    pygame.display.flip()

pygame.quit()

일반적인 PyGame 애플리케이션 루프는 다음을 수행해야합니다.

  • pygame.event.pump()또는로 이벤트를 처리합니다 pygame.event.get().
  • 입력 이벤트 및 시간 (각각 프레임)에 따라 게임 상태 및 개체 위치 업데이트
  • 전체 디스플레이를 지우거나 배경을 그립니다.
  • 전체 장면 ( blit모든 개체)을 그 립니다.
  • 에 의해 디스플레이를 업데이트하거나 pygame.display.update()또는pygame.display.flip()

참조 이벤트 및 응용 프로그램 루프를