LoginSignup
28

More than 5 years have passed since last update.

Python3_PyGame で2Dゲーム制作

Last updated at Posted at 2017-08-04

Python3とPyGameでゲーム制作メモ ( part1, 準備編 )

Python3の勉強ついでにゲーム制作をしようと思ったが,WWW上には古い情報が多く,現時点での最新版のPython3で作るというものが少なかったのでメモ.内容は,参考文献[1]を用いて勉強したもの.
前提として,python3はすでにインストールされているものとします.

開発環境

・macOS Sierra(10.12.5)
・Python(3.6.2)
・PyGame(1.9.3)

参考URL

・Python公式:https://www.python.org/
・PyGame公式:https://www.pygame.org/news
・PyGameドキュメント:https://www.pygame.org/docs/
・PyGame Wikipedia:https://ja.wikipedia.org/wiki/Pygame

PyGameインストール

まずはインストール
pip install pygame

一応ちゃんとインストールできているかinteractive modeで確認
$ python

>>> import pygame
>>> pygame.ver
'1.9.3'
>>> exit()

きちんとバージョン情報が出たのでOK!

ウィンドウの表示

pygame_sample.py
#_*_coding:utf-8_*_                                                                                                                     
#ウィンドウを表示する                                                                                                                   
import sys
import pygame
from pygame.locals import *

pygame.init() #pygameの初期化                                                                                                           
screen = pygame.display.set_mode((400, 300)) #ウィンドウの大きさ                                                                       
pygame.display.set_caption("PyGame") #タイトルバー                                                                                      

# mainループ                                                                                                                            
def main():
    while True:
        screen.fill((0,0,0)) #ウィンドウの背景色                                          
        #イベントの取得
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit() #閉じるボタンが押されたらプログラムを終了                                                                 
                sys.exit
        pygame.display.update()

if __name__ == '__main__':
    main()

実行すると・・・

window.png

ウィンドウが表示される!

※ ただし,mac版ではインストールしたpythonのパッケージによってターミナルがキー操作を独占してしまい操作ができなくなったり,ウィンドウがターミナルの裏に表示されてしまい前面に出ないなどのバグが発生します.python公式インストーラやHomebrewを用いてインストールすればきちんと動作します.pyenvを用いてインストールしたものだと上記のバグが発生することが確認されています(Anacondaは未確認).

次は画像の表示です.
Python3とPyGameでゲーム制作メモ(part2, 画像の表示)
 
 

参考文献

  1. 田中賢一郎 (2017) 「ゲームを作りながら楽しく学べるPythonプログラミング」 インプレスR&D

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
28