ゲームエンジン pygame を使ってみる

概要

pygame は,Python 用の2次元ゲームライブラリである.ウインドウの作成,図形描画,文字表示,キーボード・マウス操作の受け取りができる.

目次

ゲームの構成物

ゲームの構成物には,次のようなものがある.

前準備

cocos2d, pyglet のインストール

Spyder の起動

Python を使う.

* Anaconda3 に入っている開発環境 Spyder を使うのが簡単である.

  1. Spyder を起動する.

    Windows のスタートメニュー「Anaconda3」のにある.

  2. Spyder の画面が開くので確認する.

    左側がエディタになっている.上に実行ボタンがある.

ゲーム画面を開く

黒い画面が開くだけのアプリを作る.

  1. Spyder で次の Python プログラムを実行する.

    エディタの画面にコピー&ペーストして,実行ボタンをクリックする.

    import os, sys
    import pygame
    from pygame.locals import *
    class PyManMain:
        def __init__(self, width=640,height=480):
            pygame.init()
            self.width = width
            self.height = height
            self.screen = pygame.display.set_mode((self.width, self.height))
        def MainLoop(self):
            while 1:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        sys.exit()
    
    MainWindow = PyManMain()
    MainWindow.MainLoop()
    

    黒いだけの画面が開く.右上の「x」を1回だけクリックして終了する.(ウインドウは消えないが,プログラムは止まる

  2. 直線

    * Python で対話的に確認したい場合は, PyCharm などにある Python コンソールが便利である.

    import os, sys
    import pygame
    from pygame.locals import *
    class PyManMain:
        def __init__(self, width=640,height=480):
            pygame.init()
            self.width = width
            self.height = height
            self.screen = pygame.display.set_mode((self.width, self.height))
        def MainLoop(self):
            pygame.draw.line(self.screen, (0,95,0), (0,0), (80,80), 5)
            pygame.display.update()
            while 1:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        sys.exit()
    
    MainWindow = PyManMain()
    MainWindow.MainLoop()
    

    右上の「x」を1回だけクリックして終了する.(ウインドウは消えないが,プログラムは止まる)

  3. 四角形

    * Python で対話的に確認したい場合は, PyCharm などにある Python コンソールが便利である.

    import os, sys
    import pygame
    from pygame.locals import *
    class PyManMain:
        def __init__(self, width=640,height=480):
            pygame.init()
            self.width = width
            self.height = height
            self.screen = pygame.display.set_mode((self.width, self.height))
        def MainLoop(self):
            pygame.draw.rect(self.screen,(0,80,0),Rect(10,10,80,50),5)
            pygame.display.update()
            while 1:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        sys.exit()
    
    MainWindow = PyManMain()
    MainWindow.MainLoop()
    

    右上の「x」を1回だけクリックして終了する.(ウインドウは消えないが,プログラムは止まる)

  4. 楕円

    * Python で対話的に確認したい場合は, PyCharm などにある Python コンソールが便利である.

    import os, sys
    import pygame
    from pygame.locals import *
    class PyManMain:
        def __init__(self, width=640,height=480):
            pygame.init()
            self.width = width
            self.height = height
            self.screen = pygame.display.set_mode((self.width, self.height))
        def MainLoop(self):
            pygame.draw.ellipse(self.screen,(0,100,0),(50,50,200,100))
            pygame.display.update()
            while 1:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        sys.exit()
    
    MainWindow = PyManMain()
    MainWindow.MainLoop()
    

    右上の「x」を1回だけクリックして終了する.(ウインドウは消えないが,プログラムは止まる)

  5. テキスト

    * Python で対話的に確認したい場合は, PyCharm などにある Python コンソールが便利である.

    import os, sys
    import pygame
    from pygame.locals import *
    class PyManMain:
        def __init__(self, width=640,height=480):
            pygame.init()
            self.width = width
            self.height = height
            self.screen = pygame.display.set_mode((self.width, self.height))
        def MainLoop(self):
            font = pygame.font.Font(None, 55)
            text = font.render("hello, world", True, (255,255,255))
            self.screen.blit(text, [20, 100])
            pygame.display.update()
            while 1:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        sys.exit()
    
    MainWindow = PyManMain()
    MainWindow.MainLoop()
    

    右上の「x」を1回だけクリックして終了する.(ウインドウは消えないが,プログラムは止まる)

  6. キーボードの矢印キー(上,下,右,左)

    * Python で対話的に確認したい場合は, PyCharm などにある Python コンソールが便利である.

    実行したら,矢印キーを連打すること(押しっぱなしではイベントが発生しない.連打する).

    import os, sys
    import pygame
    from pygame.locals import *
    class PyManMain:
        def __init__(self, width=640,height=480):
            pygame.init()
            self.width = width
            self.height = height
            self.screen = pygame.display.set_mode((self.width, self.height))
        def MainLoop(self):
            x = 100
            y = 200
            while 1:
                pygame.display.update()
                pygame.time.wait(30)
                pygame.draw.circle(self.screen, (0, 200, 0), (x, y), 5)
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        sys.exit()
                    if event.type == KEYDOWN:
                        if event.key == K_ESCAPE:
                            pygame.quit()
                            sys.exit()
                        if event.key == K_LEFT:
                            x -= 5
                        if event.key == K_RIGHT:
                            x += 5
                        if event.key == K_UP:
                            y -= 5
                        if event.key == K_DOWN:
                            y += 5
    
    MainWindow = PyManMain()
    MainWindow.MainLoop()
    

    右上の「x」を1回だけクリックして終了する.(ウインドウは消えないが,プログラムは止まる)