Cocos2d で,オブジェクトの属性を乱数で変化させる
Cocos2d は2次元ゲームのフレームワーク.Python で動く.
このWebページでは、次の基本的なことを、見本プログラムで演習する.
- 乱数
- 登場物の属性 x, y を乱数で変化させる
- 登場物の属性 color を乱数で変化させる
関連する資料:Cocos2d の概要 [PDF], [パワーポイント]
関連する資料:ゲームエンジン[PDF], [パワーポイント]
【関連する外部ページ】 https://www.cocos.com/endoc.html
謝辞
Cocos2d の作者に感謝します.
前準備
Python のインストール,pip と setuptools の更新,Python 開発環境のインストール
手順は,別ページ »で説明
インストールを行わず,Google Colaboratory を利用することもありえる. Google Colaboratory の使い方などは, 別ページ »で説明
cocos2d, pyglet のインストール
乱数
- 乱数とは: コンピュータに「でたらめな値」を発生させること
- random.seed() : Python で、乱数の種になる初期値をタイマーを使って設定
- random.random() : Python で、0 以上 1 未満の乱数
- Python プログラムの実行
Python プログラムの実行
- Windows では python (Python ランチャーは py)
- Ubuntu では python3
【サイト内の関連ページ】 Python のまとめ: 別ページ »
import random random.seed() print( random.random() )
* 演習問題: 上の手順を行いなさい。
* 演習問題: 「random.random()」 を 「random.random() * 10」に変えると 0 以上 10 未満の乱数になることを確認したい。
次のように実行し,結果を確認する.

Cocos2d で文字列の描画、線の描画
【関連する外部ページ】
http://nullege.com/codes/search/cocos
Label クラス,Line クラスの属性
Label クラスは,文字や文字列を扱うためのクラス.次のような属性がある.
- x, y: 位置
- element.color: 位置
- element.text: テキスト
Line クラスは,線分を扱うためのクラス.次のような属性がある.
- x, y: 位置
* サブクラスを定義することで、属性は追加可能
マウスクリックで、文字列のランダム移動
- イベント: on_mouse_press (マウスのボタンが押された)
- イベントハンドラ: オブジェクトの x, y 値を変える
def on_mouse_press(self, x, y, buttons, modifiers): self.myactor.x = random.random() * 640 self.myactor.y = random.random() * 480
- Python プログラムの実行
Python プログラムの実行
- Windows では python (Python ランチャーは py)
- Ubuntu では python3
【サイト内の関連ページ】 Python のまとめ: 別ページ »
import random import cocos from cocos import scene from cocos.layer import Layer, ColorLayer from cocos.director import director import pyglet from pyglet.window import key from time import sleep class MyActor(cocos.text.Label): def __init__(self, text, x, y, size, rgba): super(MyActor, self).__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(Layer00, self).__init__() self.myactor = MyActor("A", 0, 0, 32, (0, 200, 255, 255)) self.add(self.myactor) def on_mouse_press(self, x, y, buttons, modifiers): self.myactor.x = random.random() * 640 self.myactor.y = random.random() * 480 director.init(width=640, height=480) director.run( scene.Scene( Layer00() ) )
- 画面が現れるので確認
- 画面の中で、マウスのボタンを押す(クリック)。すると、オブジェクトが動くので確認.何度かマウスをクリックすること。
- 結果を確認したら,右上の「x」をクリックして終了.
* 演習問題: 上の手順を行いなさい。
マウスクリックで、線のランダム移動
- イベント: on_mouse_press (マウスのボタンが押された)
- イベントハンドラ: オブジェクトの x, y 値を変える
例えば次のように
def on_mouse_press(self, x, y, buttons, modifiers): self.myactor.x = random.random() * 640 self.myactor.y = random.random() * 480
- Python プログラムの実行
Python プログラムの実行
- Windows では python (Python ランチャーは py)
- Ubuntu では python3
【サイト内の関連ページ】 Python のまとめ: 別ページ »
import random import cocos from cocos import scene from cocos.layer import Layer, ColorLayer from cocos.director import director import pyglet from pyglet.window import key from time import sleep class MyActor(cocos.draw.Line): def __init__(self, x1, y1, x2, y2, rgba): super(MyActor, self).__init__( (x1, y1), (x2, y2), rgba ) class Layer00(Layer): is_event_handler = True def __init__(self): super(Layer00, self).__init__() self.myactor = MyActor(0, 0, 100, 100, (255, 255, 230, 255)) self.add(self.myactor) def on_mouse_press(self, x, y, buttons, modifiers): self.myactor.x = random.random() * 640 self.myactor.y = random.random() * 480 director.init(width=640, height=480) director.run( scene.Scene( Layer00() ) )
- 画面が現れるので確認
- 画面の中で、マウスのボタンを押す(クリック)。すると、オブジェクトが動くので確認.何度かマウスをクリックすること。
- 結果を確認したら,右上の「x」をクリックして終了.
* 演習問題: 上の手順を行いなさい。
マウスクリックで、文字列の色のランダム変化
- イベント: on_mouse_press (マウスのボタンが押された)
- イベントハンドラ: オブジェクトの color 値を変える
- Python プログラムの実行
Python プログラムの実行
- Windows では python (Python ランチャーは py)
- Ubuntu では python3
【サイト内の関連ページ】 Python のまとめ: 別ページ »
import random import cocos from cocos import scene from cocos.layer import Layer, ColorLayer from cocos.director import director import pyglet from pyglet.window import key from time import sleep class MyActor(cocos.text.Label): def __init__(self, text, x, y, size, rgba): super(MyActor, self).__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(Layer00, self).__init__() self.myactor = MyActor("A", 0, 0, 32, (0, 200, 255, 255)) self.add(self.myactor) def on_mouse_press(self, x, y, buttons, modifiers): self.myactor.element.color = ( int(random.random() * 256.0), int(random.random() * 256.0), int(random.random() * 256.0), int(random.random() * 256.0) ) self.myactor.x = random.random() * 640 self.myactor.y = random.random() * 480 director.init(width=640, height=480) director.run( scene.Scene( Layer00() ) )
- 画面が現れるので確認
- 画面の中で、マウスのボタンを押す(クリック)。すると、オブジェクトが動くと同時に色が変化するので確認.何度かマウスをクリックすること。
- 結果を確認したら,右上の「x」をクリックして終了.
* 演習問題: 上の手順を行いなさい。