Cocos2d は2次元ゲームのフレームワーク.Python で動く.
このWebページでは、次の基本的なことを、見本プログラムで演習する.
関連する資料:Cocos2d の概要 [PDF], [パワーポイント]
関連する資料:ゲームエンジン[PDF], [パワーポイント]
【関連する外部ページ】 http://python.cocos2d.org/doc.html
謝辞
Cocos2d の作者に感謝します.
手順は,別ページ »で説明している.
インストールを行わず,Google Colaboratory を利用することもありえる. Google Colaboratory の使い方などは, 別ページ »で説明している.
Python プログラムの実行
Python 開発環境(Jupyter Qt Console, Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract, Spyder, PyCharm, PyScripterなど)も便利である.
Python のまとめ: 別ページ »にまとめ
import random random.seed() print( random.random() )
■ 演習問題: 上の手順を行いなさい。
■ 演習問題: 「random.random()」 を 「random.random() * 10」に変えると 0 以上 10 未満の乱数になることを確認したい。
次のように実行し,結果を確認する.
【関連する外部ページ】
http://nullege.com/codes/search/cocos
Label クラスは,文字や文字列を扱うためのクラス.次のような属性がある.
Line クラスは,線分を扱うためのクラス.次のような属性がある.
※ サブクラスを定義することで、属性は追加可能
def on_mouse_press(self, x, y, buttons, modifiers): self.myactor.x = random.random() * 640 self.myactor.y = random.random() * 480
Python プログラムの実行
Python 開発環境(Jupyter Qt Console, Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract, Spyder, PyCharm, PyScripterなど)も便利である.
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() ) )
■ 演習問題: 上の手順を行いなさい。
例えば次のように
def on_mouse_press(self, x, y, buttons, modifiers): self.myactor.x = random.random() * 640 self.myactor.y = random.random() * 480
Python プログラムの実行
Python 開発環境(Jupyter Qt Console, Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract, Spyder, PyCharm, PyScripterなど)も便利である.
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() ) )
■ 演習問題: 上の手順を行いなさい。
Python プログラムの実行
Python 開発環境(Jupyter Qt Console, Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract, Spyder, PyCharm, PyScripterなど)も便利である.
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() ) )
■ 演習問題: 上の手順を行いなさい。