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

【初心者向け】MacでPython+Pygameを使って簡単なゲームを作る方法

Posted at

この記事では、Mac に Python 環境を構築し、Pygame を使って簡単なゲーム(動く四角)を作る手順を紹介します。プログラミング初心者や、ゲーム開発に初挑戦する方に向けた内容です。

✅ 1. Python のバージョン確認

まずは Python がインストールされているか確認します。

python3 --version

Python 3.x.x が表示されれば OK。インストールされていない場合は brew でインストールできます:

brew install python

✅ 2. pip のバージョン確認

次に pip が使えるかを確認します。

pip3 --version

pip を最新にする

python3 -m pip install --upgrade pip

🐍 3. pygame のインストール

Pygame は Python でゲームを作るための人気ライブラリです。

pip3 install pygame

❗️補足:インストール時に「Permission denied」や「not on PATH」などの警告が出たら、以下を実行して PATH を追加してください。

echo 'export PATH=$HOME/Library/Python/3.9/bin:$PATH' >> ~/.zshrc
source ~/.zshrc

✅ 4. ゲームのソースコード作成

🔧 作業フォルダ例

mkdir ~/my_pygame && cd ~/my_pygame

📝 simple_game.py の内容

import pygame
import sys

# 初期化
pygame.init()

# 画面サイズ
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("動く四角ゲーム")

# 色
WHITE = (255, 255, 255)
RED = (255, 0, 0)

# 四角の初期位置
x, y = 300, 220
speed = 5

# ゲームループ
clock = pygame.time.Clock()
running = True
while running:
    screen.fill(WHITE)

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

    # キー操作
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        x -= speed
    if keys[pygame.K_RIGHT]:
        x += speed
    if keys[pygame.K_UP]:
        y -= speed
    if keys[pygame.K_DOWN]:
        y += speed

    # 四角を描画
    pygame.draw.rect(screen, RED, (x, y, 50, 50))

    pygame.display.flip()
    clock.tick(60)

pygame.quit()
sys.exit()

✅ 5. 実行してみよう!

python3 simple_game.py

これで赤い四角が画面に表示され、矢印キーで動かせます!

image.png

📝 まとめ

項目 内容
開発言語 Python 3
使用ライブラリ pygame
実行環境 macOS (Apple Silicon / Intel 両対応)
難易度 初心者向け
0
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
0
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?