pygame を使ってみる

pygame は2次元ゲームのフレームワーク.Python で動く. このWebページでは,pygame の次の機能を試してみる.

ここではPythonを使う.

Anaconda3 に入っている開発環境 spyder を使うのが簡単.

前準備

Python のインストール,pip と setuptools の更新,Python 開発環境のインストール

手順は,別ページ »で説明

インストールを行わず,Google Colaboratory を利用することもありえる. Google Colaboratory の使い方などは, 別ページ »で説明

pygame のインストール

pygame で2次元のグラフィックス,イベント

画面(ウインドウ)を開く

  1. Python プログラムの実行

    Python プログラムの実行

    【サイト内の関連ページ】 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()
    
  2. 黒いだけの画面が開く. 右上の「x」を1回だけクリックして終了.(ウインドウは消えないが,プログラムは止まる

* 演習問題: spyder を起動し、上の手順を行いなさい.

直線

  1. Python プログラムの実行

    Python プログラムの実行

    【サイト内の関連ページ】 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()
    
  2. 右上の「x」を1回だけクリックして終了.(ウインドウは消えないが,プログラムは止まる

* 演習問題: 「(0,95,0)」を書き換えて、を変えてみなさい. 実行ボタンを押して確認すること

* 演習問題: 「(0,0), (80,80)」を書き換えて、を変えてみなさい.実行ボタンを押して確認すること

四角形

  1. Python プログラムの実行

    Python プログラムの実行

    【サイト内の関連ページ】 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()
    
  2. 右上の「x」を1回だけクリックして終了.(ウインドウは消えないが,プログラムは止まる

* 演習問題: 「(0,80,0)」を書き換えて、を変えてみなさい.実行ボタンを押して確認すること

* 演習問題: 「(10,10,80,50)」を書き換えて、を変えてみなさい.実行ボタンを押して確認すること

楕円

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

* 演習問題: 「(0,100,0)」を書き換えて、を変えてみなさい.実行ボタンを押して確認すること

* 演習問題: 「(50,50,200,100)」を書き換えて、を変えてみなさい.実行ボタンを押して確認すること

テキスト

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

* 演習問題: 「(255,255,255)」を書き換えて、を変えてみなさい.実行ボタンを押して確認すること

* 演習問題: 「hello,world」を書き換えて、表示されるテキストを変えてみなさい.実行ボタンを押して確認すること

pygame でイベント処理

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

  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):
            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 = x - 5
                        if event.key == K_RIGHT:
                            x = x + 5
                        if event.key == K_UP:
                            y = y - 5
                        if event.key == K_DOWN:
                            y = y + 5
    
    MainWindow = PyManMain()
    MainWindow.MainLoop()
    
  2. 起動したら,矢印キーを連打してみる (押しっぱなしではイベントが発生しない.連打)
  3. 右上の「x」を1回だけクリックして終了.(ウインドウは消えないが,プログラムは止まる

* 演習問題: 上の手順を行いなさい.