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

ゲームの構成物

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

前準備

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回だけクリックして終了.(ウインドウは消えないが,プログラムは止まる)