0
0

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で2Dゲームを作ろう!Pygameの始め方まとめ

Posted at

Pythonでゲームを作ってみたい!
そんな方におすすめなのが Pygame ライブラリです。
この記事では、Pygameの始め方を5ステップで超わかりやすく解説します。

🧰 環境
OS:Windows / macOS / Linux

Python:3.x系(最新版推奨)

Pygame:2.x系

🔽 Step 1:Pythonをインストール
まだPythonが入っていない方は、公式サイトからインストールしてください。

👉 https://www.python.org/downloads/

※ Windowsでは「Add Python to PATH」にチェックを忘れずに!

💡 Step 2:Pygameをインストール

ターミナルまたはコマンドプロンプトを開いて以下を入力:

pip install pygame
✅ 正常にインストールできたか確認したい場合:

import pygame
print(pygame.ver)
バージョンが表示されればOKです!

🧪 Step 3:サンプルコードを動かしてみよう
以下のコードを game.py というファイル名で保存してください。

import pygame
import sys

初期化

pygame.init()

画面サイズ

WIDTH, HEIGHT = 640, 480
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My First Game")

メインループ

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

screen.fill((0, 0, 0))  # 黒背景
pygame.display.flip()

pygame.quit()
sys.exit()
▶️ Step 4:ゲームを起動!
以下のコマンドでゲームを起動できます。

python game.py
✅ 黒いウィンドウが開けば成功です!

🎯 おわりに
Pygameを使えば、2Dゲームの開発がとても簡単になります。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?