PyGameアプリケーションがまったく実行されないのはなぜですか?
Dec 12 2020
私は簡単なPygameプログラムを持っています:
#!/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()
イベントとアプリケーションのループも参照してください