1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonのゲーム開発ライブラリPyGameの使用方法

Last updated at Posted at 2025-02-28

はじめに

PyGameは、Pythonでゲーム開発するための強力なライブラリです。2Dゲームの作成に適しており、初心者から超級者まで幅広く利用されています。この記事では、PyGameの基本的な使用方法を紹介します。

目次

インストール
基本的な構造
主要な機能

まとめ

インストール

まずPyGameをインストールする必要があります。以下コマンドを使用してインストールできます。

pip install pygame

基本的な構造

PyGameを使用したゲームの基本的な構造は以下のようになります。

import pygame
import sys

# 初期化
pygame.init()

# ウィンドウの作成
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("My PyGame")

# メインループ
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    # ゲームロジック
    
    # 描画
    screen.fill((255, 255, 255))  # 白色で塗りつぶし
    pygame.display.flip()  # 画面更新

# ゲーム終了
pygame.quit()
sys.exit()

主要な機能

描画

PyGameでは、様々な図形や画像を描画できます。

# 四角形を描画
pygame.draw.rect(screen, (0, 255, 0), (100, 100, 50, 50))

# 画像の描画
image = pygame.image.load("player.png")
screen.blit(image, (200, 200))

イベント処理

ユーザーの入力(キーボード、マウス)を処理できます。

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_SPACE:
            # スペースキーが押されたときの処理
    elif event.type == pygame.MOUSEBUTTONDOWN:
        # マウスクリック時の処理

音声

BGMや効果音を再生できます。

pygame.mixer.music.load("bgm.mp3")
pygame.mixer.music.play(-1)  # -1で無限ループ

sound_effect = pygame.mixer.Sound("effect.wav")
sound_effect.play()

衝突検出

オブジェクト間の衝突を検出できます。

player_rect = pygame.Rect(100, 100, 50, 50)
enemy_rect = pygame.Rect(200, 200, 50, 50)

if player_rect.colliderect(enemy_rect):
    print("衝突しました!")

応用例:簡単なゲーム

以下は、動く四角形(プレイヤー)を操作する簡単なゲームの例です。

import pygame
import sys

pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()

player_pos = [400, 300]
player_size = 50

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        player_pos[0] -= 5
    if keys[pygame.K_RIGHT]:
        player_pos[0] += 5
    if keys[pygame.K_UP]:
        player_pos[1] -= 5
    if keys[pygame.K_DOWN]:
        player_pos[1] += 5

    screen.fill((0, 0, 0))
    pygame.draw.rect(screen, (0, 255, 0), (player_pos[0], player_pos[1], player_size, player_size))
    pygame.display.flip()

    clock.tick(60)

pygame.quit()
sys.exit()

このコードでは、矢印キーを使ってプレイヤーを動かすことができます。

まとめ

PyGameは、Pythonでゲームを開発するための強力なツールです。基本的な描画、イベント処理、音声再生、衝突検出などの機能を提供し、2Dゲーム開発を容易にします。この記事では紹介した基本的な使い方を理解すれば、より複雑なゲームの開発に挑戦することができるでしょう。
最後まで読んでくださり、ありがとうございました。もし改善点や質問があれば、ぜひコメントしてください!

1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?