LoginSignup
1
2

More than 1 year has passed since last update.

pythonでゲーム作成 pygame基礎

Last updated at Posted at 2022-05-07

仮想環境作成

python -m venv game
.\game\Scripts\activate

ライブラリインストール

pip install pygame

初期化

import pygame
pygame.init()

ライブラリインポートし、初期化する。

画面を表示

import sys
import pygame
from pygame.locals import QUIT

pygame.init()
screen = pygame.display.set_mode((600, 400))

def main():
    while True:
        pygame.display.update() 
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

if __name__ == '__main__':
    main()

スクリーンサイズを設定し、画面表示。
閉じる(QUIT)ボタンが押されたら、pygameを終了し、プログラムが終了する。
以下実行時の表示画面。
sample0.png

基礎練習

背景を白に設定し、中心に円を描画する。

import sys
import pygame
from pygame.locals import QUIT

pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("国旗")

white = (255,255,255)
red = (255,0,0)

def main():
    while True:
        screen.fill(white)
        pygame.draw.circle(screen, red, (300,200), 100)
        pygame.display.update() 
        for event in pygame.event.get():
            if event.type == QUIT: 
                pygame.quit()
                sys.exit() 

if __name__ == '__main__':
    main()

set_captionを利用することで、タイトルを編集可能。
sreen.fillで背景色を設定する。
draw.circle関数で円を描画する。第3引数は円の描画中心座標、第4引数は半径を示している。
以下実行時の表示画面。
sample1.png

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2