金子邦彦研究室プログラミングPythonCocos2d で,オブジェクトの属性を乱数で変化させる

Cocos2d で,オブジェクトの属性を乱数で変化させる

Cocos2d は2次元ゲームのフレームワーク.Python で動く.

このWebページでは、次の基本的なことを、見本プログラムで演習する.

関連する資料:Cocos2d の概要 [PDF], [パワーポイント]

関連する資料:ゲームエンジン[PDF], [パワーポイント]

関連する外部ページhttp://python.cocos2d.org/doc.html

謝辞

Cocos2d の作者に感謝します.

前準備

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

手順は,別ページ »で説明している.

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

cocos2d, pyglet のインストール

乱数

  1. Python プログラムの実行

    Python プログラムの実行

    Python 開発環境(Jupyter Qt Console, Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract, Spyder, PyCharm, PyScripterなど)も便利である.

    Python のまとめ: 別ページ »にまとめ

    import random
    random.seed()
    
    print( random.random() )
    

    [image]

■ 演習問題: 上の手順を行いなさい。

■ 演習問題: 「random.random()」 を 「random.random() * 10」に変えると 0 以上 10 未満の乱数になることを確認したい。

次のように実行し,結果を確認する.

[image]

Cocos2d で文字列の描画、線の描画

関連する外部ページ

http://nullege.com/codes/search/cocos

Label クラス,Line クラスの属性

Label クラスは,文字や文字列を扱うためのクラス.次のような属性がある.

Line クラスは,線分を扱うためのクラス.次のような属性がある.

※ サブクラスを定義することで、属性は追加可能

マウスクリックで、文字列のランダム移動

  1. Python プログラムの実行

    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() ) )
    

    [image]
  2. 画面が現れるので確認

    [image]
  3. 画面の中で、マウスのボタンを押す(クリック)。すると、オブジェクトが動くので確認.何度かマウスをクリックすること。

    [image]
  4. 結果を確認したら,右上の「x」をクリックして終了. 

    [image]

■ 演習問題: 上の手順を行いなさい。

マウスクリックで、線のランダム移動

  1. Python プログラムの実行

    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() ) )
    

    [image]
  2. 画面が現れるので確認

    [image]
  3. 画面の中で、マウスのボタンを押す(クリック)。すると、オブジェクトが動くので確認.何度かマウスをクリックすること。

    [image]
  4. 結果を確認したら,右上の「x」をクリックして終了. 

    [image]

■ 演習問題: 上の手順を行いなさい。

マウスクリックで、文字列の色のランダム変化

  1. Python プログラムの実行

    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() ) )
    

    [image]
  2. 画面が現れるので確認

    [image]
  3. 画面の中で、マウスのボタンを押す(クリック)。すると、オブジェクトが動くと同時に色が変化するので確認.何度かマウスをクリックすること。

    [image]
  4. 結果を確認したら,右上の「x」をクリックして終了. 

    [image]

■ 演習問題: 上の手順を行いなさい。