Cocos2d サンプルプログラム
1. エグゼクティブサマリー
Cocos2d は2次元ゲームのフレームワークである.Python で動作する.
このWebページでは,Cocos2d を用いたブロック崩しゲームのサンプルプログラムを掲載する.プレイヤーは左右キーでパドルを操作し,ボールを跳ね返してブロックを破壊する.スコア表示,ゲームオーバー画面,クリア画面を備える.
関連する資料:Cocos2d の概要 [PDF], [パワーポイント]
関連する資料:ゲームエンジン [PDF], [パワーポイント]
【関連する外部ページ】 https://www.cocos.com/endoc.html
【関連する外部ページ】 http://docplayer.net/62131747-Python-game-programming-by-example.html
2. 前準備(必要ソフトウェアの入手)
ここでは、最低限の事前準備について説明する。機械学習や深層学習を行う場合は、NVIDIA CUDA、Visual Studio、Cursorなどを追加でインストールすると便利である。これらについては別ページ https://www.kkaneko.jp/cc/dev/aiassist.htmlで詳しく解説しているので、必要に応じて参照してください。
Python 3.12 のインストール(Windows 上) [クリックして展開]
以下のいずれかの方法で Python 3.12 をインストールする。Python がインストール済みの場合、この手順は不要である。
方法1:winget によるインストール
管理者権限のコマンドプロンプトで以下を実行する。管理者権限のコマンドプロンプトを起動するには、Windows キーまたはスタートメニューから「cmd」と入力し、表示された「コマンドプロンプト」を右クリックして「管理者として実行」を選択する。
winget install --scope machine --id Python.Python.3.12 -e --silent --disable-interactivity --force --accept-source-agreements --accept-package-agreements --override "/quiet InstallAllUsers=1 PrependPath=1 Include_pip=1 Include_test=0 Include_launcher=1 InstallLauncherAllUsers=1"
--scope machine を指定することで、システム全体(全ユーザー向け)にインストールされる。このオプションの実行には管理者権限が必要である。インストール完了後、コマンドプロンプトを再起動すると PATH が自動的に設定される。
方法2:インストーラーによるインストール
- Python 公式サイト(https://www.python.org/downloads/)にアクセスし、「Download Python 3.x.x」ボタンから Windows 用インストーラーをダウンロードする。
- ダウンロードしたインストーラーを実行する。
- 初期画面の下部に表示される「Add python.exe to PATH」に必ずチェックを入れてから「Customize installation」を選択する。このチェックを入れ忘れると、コマンドプロンプトから
pythonコマンドを実行できない。 - 「Install Python 3.xx for all users」にチェックを入れ、「Install」をクリックする。
インストールの確認
コマンドプロンプトで以下を実行する。
python --version
バージョン番号(例:Python 3.12.x)が表示されればインストール成功である。「'python' は、内部コマンドまたは外部コマンドとして認識されていません。」と表示される場合は、インストールが正常に完了していない。
AIエディタ Windsurf のインストール(Windows 上) [クリックして展開]
Pythonプログラムの編集・実行には、AIエディタの利用を推奨する。ここでは、Windsurfのインストールを説明する。Windsurf がインストール済みの場合、この手順は不要である。
管理者権限のコマンドプロンプトで以下を実行する。管理者権限のコマンドプロンプトを起動するには、Windows キーまたはスタートメニューから「cmd」と入力し、表示された「コマンドプロンプト」を右クリックして「管理者として実行」を選択する。
winget install --scope machine --id Codeium.Windsurf -e --silent --disable-interactivity --force --accept-source-agreements --accept-package-agreements --custom "/SP- /SUPPRESSMSGBOXES /NORESTART /CLOSEAPPLICATIONS /DIR=""C:\Program Files\Windsurf"" /MERGETASKS=!runcode,addtopath,associatewithfiles,!desktopicon"
powershell -Command "$env:Path=[System.Environment]::GetEnvironmentVariable('Path','Machine')+';'+[System.Environment]::GetEnvironmentVariable('Path','User'); windsurf --install-extension MS-CEINTL.vscode-language-pack-ja --force; windsurf --install-extension ms-python.python --force; windsurf --install-extension Codeium.windsurfPyright --force"
--scope machine を指定することで、システム全体(全ユーザー向け)にインストールされる。このオプションの実行には管理者権限が必要である。インストール完了後、コマンドプロンプトを再起動すると PATH が自動的に設定される。
【関連する外部ページ】
Windsurf の公式ページ: https://windsurf.com/
Windows での cocos2d, pyglet のインストールは,別ページ »で説明
3. 実行のための準備とその確認手順(Windows 前提)
3.1 プログラムファイルの準備
第5章のソースコードをテキストエディタ(メモ帳,Windsurf 等)に貼り付け,main.py として保存する(文字コード:UTF-8).
3.2 実行コマンド
コマンドプロンプトでファイルの保存先ディレクトリに移動し,以下を実行する.
python main.py
3.3 動作確認チェックリスト
| 確認項目 | 期待される結果 |
|---|---|
| プログラム起動時 | 640×480 のウィンドウが開き,ブロック(「□」),ボール(「?」),パドル(「ー」),スコア(「Score: 0」)が表示される |
| ボールの移動 | ボールが画面内を移動し,上壁・左壁・右壁で反射する |
| 左右キーの操作 | パドルが左右に移動する.画面端を超えない |
| パドルとの衝突 | ボールがパドルに当たると上方向に反射する |
| ブロックとの衝突 | ブロックが消滅し,スコアが10点加算される |
| ボールが画面下端に到達 | 赤い背景に「GAME OVER」と最終スコアが表示される |
| 全ブロック破壊 | 緑の背景に「CLEAR」と最終スコアが表示される |
| ゲームオーバー画面またはクリア画面でキーを押す | ゲームが最初から再開される |
| 終了操作 | 右上の「x」をクリックするとウィンドウが閉じる |
4. 概要・使い方・実行上の注意
画面上部に3段×10列,合計30個のブロックが配置される.ボールは起動と同時に移動を開始し,壁およびパドルで反射する.プレイヤーは左右矢印キーでパドルを操作する.
ボールがブロックに衝突すると,衝突方向に応じてボールが反射し,当該ブロックが消滅する.ブロック1つにつきスコアが10点加算され,画面左上の表示がリアルタイムに更新される.
ボールが画面下端に到達するとゲームオーバーとなり,赤い背景に「GAME OVER」と最終スコアが表示される.全ブロックを破壊するとクリアとなり,緑の背景に「CLEAR」と最終スコアが表示される.いずれの画面でもキーを押すとゲームが再開される.
結果を確認したら,右上の「x」をクリックして終了する.
5. ソースコード
import cocos
from cocos import scene
from cocos.layer import Layer, ColorLayer
from cocos.director import director
from pyglet.window import key
WIDTH, HEIGHT = 640, 480
BALL_SPEED = 150
class MyActor(cocos.text.Label):
def __init__(self, text, x, y, vx, vy, size, rgba):
super().__init__(text, font_name="Times New Roman", font_size=size,
anchor_x='center', anchor_y='center', color=rgba)
self.vx, self.vy = vx, vy
self.position = cocos.euclid.Vector2(x, y)
class Player(cocos.text.Label):
def __init__(self, text, x, y, size, rgba):
super().__init__(text, font_name="Times New Roman", font_size=size,
anchor_x='center', anchor_y='center', color=rgba)
self.position = cocos.euclid.Vector2(x, y)
class Layer00(Layer):
is_event_handler = True
def __init__(self):
super().__init__()
self.score = 0
self.score_label = cocos.text.Label("Score: 0", font_name="Times New Roman",
font_size=16, anchor_x='left', anchor_y='top',
color=(255, 255, 255, 255))
self.score_label.position = (10, HEIGHT - 10)
self.add(self.score_label)
self.kao = MyActor("?", 320, 240, -BALL_SPEED, -BALL_SPEED, 40, (255, 255, 255, 255))
self.add(self.kao)
self.balls = []
for i in range(3):
for j in range(10):
b = MyActor("□", 50 * (j + 1) + 50, 480 - 47 * (i + 1), 0, 0, 70, (255, 255, 255, 255))
self.balls.append(b)
self.add(b)
self.player = Player("ー", 200, 80, 100, (120, 200, 255, 255))
self.add(self.player)
self.schedule(self.update)
def update(self, dt):
kao = self.kao
kao.x += kao.vx * dt
kao.y += kao.vy * dt
if kao.x >= WIDTH - 15 or kao.x < 15:
kao.vx = -kao.vx
if kao.y >= HEIGHT - 8:
kao.vy = -kao.vy
if kao.y < 22:
kao.vy = -kao.vy
director.replace(scene.Scene(Layer01(self.score)))
return
if (self.player.x - 50 < kao.x < self.player.x + 50 and
self.player.y - 10 < kao.y < self.player.y + 25):
kao.vy = -kao.vy
a, b, c = 22, 22, 20
for z in range(len(self.balls) - 1, -1, -1):
bx, by = self.balls[z].x, self.balls[z].y
if bx - b < kao.x + a < bx + b and by - c < kao.y < by + c:
kao.vx = -kao.vx
elif bx - b < kao.x - a < bx + b and by - c < kao.y < by + c:
kao.vx = -kao.vx
elif bx - b < kao.x < bx + b and by - c < kao.y + a < by + c:
kao.vy = -kao.vy
elif bx - b < kao.x < bx + b and by - c < kao.y - a < by + c:
kao.vy = -kao.vy
else:
continue
self.remove(self.balls[z])
self.balls.pop(z)
self.score += 10
self.score_label.element.text = "Score: " + str(self.score)
if not self.balls:
director.replace(scene.Scene(LayerClear(self.score)))
return
def on_key_press(self, symbol, modifiers):
if symbol == key.RIGHT:
self.player.x = min(self.player.x + 20, WIDTH - 50)
elif symbol == key.LEFT:
self.player.x = max(self.player.x - 20, 50)
class Layer01(ColorLayer):
is_event_handler = True
def __init__(self, score=0):
super().__init__(231, 76, 60, 255)
self.add(cocos.text.Label("GAME OVER", font_name="Times New Roman", font_size=32,
anchor_x='center', anchor_y='center',
color=(0, 200, 255, 255), position=(WIDTH // 2, HEIGHT // 2 + 30)))
self.add(cocos.text.Label("Score: " + str(score), font_name="Times New Roman", font_size=20,
anchor_x='center', anchor_y='center',
color=(255, 255, 255, 255), position=(WIDTH // 2, HEIGHT // 2 - 30)))
def on_key_press(self, key, modifiers):
director.replace(scene.Scene(Layer00()))
class LayerClear(ColorLayer):
is_event_handler = True
def __init__(self, score=0):
super().__init__(46, 139, 87, 255)
self.add(cocos.text.Label("CLEAR", font_name="Times New Roman", font_size=32,
anchor_x='center', anchor_y='center',
color=(255, 255, 255, 255), position=(WIDTH // 2, HEIGHT // 2 + 30)))
self.add(cocos.text.Label("Score: " + str(score), font_name="Times New Roman", font_size=20,
anchor_x='center', anchor_y='center',
color=(255, 255, 255, 255), position=(WIDTH // 2, HEIGHT // 2 - 30)))
def on_key_press(self, key, modifiers):
director.replace(scene.Scene(Layer00()))
director.init(width=WIDTH, height=HEIGHT)
director.run(scene.Scene(Layer00()))
6. まとめ
Cocos2d フレームワーク
Cocos2d は2次元ゲームのフレームワークであり,Python で動作する.レイヤー,シーン,ラベルなどを用いてゲーム画面を構成する.
レイヤーとシーンによる画面管理
ゲームプレイ画面(Layer00),ゲームオーバー画面(Layer01),クリア画面(LayerClear)をそれぞれ独立したレイヤーとして実装し,director.replace でシーンを切り替える.
衝突判定とボール反射
ボールの座標とブロックやパドルの領域を比較して衝突を判定する.衝突方向に応じてボールの速度を反転させ,反射を実現する.
キーボード入力によるプレイヤー操作
pyglet のキーイベント(on_key_press)で左右矢印キーによるパドル移動を実現する.移動範囲は画面端で制限される.
スコア管理とゲーム進行
ブロック破壊ごとに10点が加算され,画面左上に表示される.全ブロック破壊でクリア画面,ボールが画面下端に到達するとゲームオーバー画面へ遷移し,最終スコアが表示される.