ゲームエンジン pygame を使ってみる
ゲームの構成物
ゲームの構成物には,次のようなものがある.
- ゲーム画面(=ウインドウ)
- グラフィックス(線,四角,丸など)
- 文字 ※ 文字の表示では,表示場所,フォントサイズ,フォントの種類などを指定
- イベント(キーボード操作,マウス操作)
- イベントハンドラ = 特定のイベントに対して,どうコンピュータが処理するか
前準備
cocos2d, pyglet のインストール
Spyder の起動
Pythonを使う.
* Anaconda3 に入っている開発環境 spyder を使うのが簡単.
ゲーム画面を開く
黒い画面が開くだけというアプリ)
- 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回だけクリックして終了.(ウインドウは消えないが,プログラムは止まる)
- 直線
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回だけクリックして終了.(ウインドウは消えないが,プログラムは止まる)
- 四角形
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回だけクリックして終了.(ウインドウは消えないが,プログラムは止まる)
- 楕円
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回だけクリックして終了.(ウインドウは消えないが,プログラムは止まる)
- テキスト
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回だけクリックして終了.(ウインドウは消えないが,プログラムは止まる)
- キーボードの矢印キー(上,下,右,左)
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回だけクリックして終了.(ウインドウは消えないが,プログラムは止まる)