金子邦彦研究室プログラミングPythonCocos2d サンプルプログラム

Cocos2d サンプルプログラム

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

このWebページでは、サンプルプログラムを掲載する

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

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

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

関連する外部ページhttp://docplayer.net/62131747-Python-game-programming-by-example.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
    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, vx, vy, size, rgba):
            super(MyActor, self).__init__(
                text, 
                font_name = "Times New Roman", 
                font_size = size, 
                anchor_x = 'center', 
                anchor_y = 'center', 
                color = rgba 
            )
            self.vx = vx
            self.vy = vy
            self.position = cocos.euclid.Vector2(x, y) 
            self.type="MyActor"
    
    class Player(cocos.text.Label):
        def __init__(self, text, x, y, size, rgba):
            super(Player, 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) 
            self.type="Player"
    
    class Layer00(Layer):
        is_event_handler = True
        def __init__(self):
            super(Layer00, self).__init__()
            # random.seed()
            self.kao = MyActor("?", 320, 240, -150, -150, 40, (255, 255, 255, 255)) 
            self.add( self.kao )
            self.balls = []
            for i in range(1, 4):
                for j in range(1, 11):
                    self.balls.append( MyActor("□", 50 * j + 50, 480 - (47 * i), 0, 0, 70, (255, 255, 255, 255)) )
                    self.add(self.balls[(j-1)+((i-1)*10)])
            self.player = Player("ー", 200, 80, 100, (120, 200, 255, 255))
            self.add(self.player)
            self.schedule(self.update) 
        def update(self, dt): 
            for _, node in self.children: 
                if ( type(node) == MyActor ):
                    node.x += node.vx * dt
                    node.y += node.vy * dt
                    a = ""
                    if ( node.x >= 640-15 and a != "right"):
                        node.vx = -node.vx
                        a = "right"
                    if ( node.x < 0+15 and a != "left"):
                        node.vx = -node.vx
                        a = "left"
                    if ( node.y >= 480-8 and a != "up"):
                        node.vy = -node.vy
                        a = "up"
                    if ( node.y < 0+22 and a != "down"):
                        node.vy = -node.vy
                        a = "down"
                        director.replace(scene.Scene(Layer01()))
                    if ( ( ( self.player.x - 50 ) < node.x ) and ( node.x < ( self.player.x + 50 ) ) and ( ( self.player.y - 10 ) < node.y ) and ( node.y < ( self.player.y + 25 ) ) ): 
                        node.vy = - node.vy
            for z in range(0, len(self.balls)):
                a = 22
                b = 22
                c = 20
               # if self.balls[z] != None:
                if ( self.balls[z].x - b < ( self.kao.x + a) ) and ( ( self.kao.x + a ) < self.balls[z].x + b and self.balls[z].y - c < ( self.kao.y ) ) and ( ( self.kao.y ) < self.balls[z].y + c): 
                    self.kao.vx = - self.kao.vx
                    
                    self.remove(self.balls[z])
                    self.balls.pop(z)
                    return
                if ( self.balls[z].x - b < ( self.kao.x - a) ) and ( ( self.kao.x - a ) < self.balls[z].x + b and self.balls[z].y - c < ( self.kao.y ) ) and ( ( self.kao.y ) < self.balls[z].y + c): 
                    self.kao.vx = - self.kao.vx
                    
                    self.remove(self.balls[z])
                    self.balls.pop(z)
                    return
                if ( self.balls[z].x - b < ( self.kao.x ) ) and ( ( self.kao.x ) < self.balls[z].x + b and self.balls[z].y - c < ( self.kao.y + a) ) and ( ( self.kao.y + a) < self.balls[z].y + c): 
                    self.kao.vy = - self.kao.vy
                    
                    self.remove(self.balls[z])
                    self.balls.pop(z)
                    return
                if ( self.balls[z].x - b < ( self.kao.x ) ) and ( ( self.kao.x ) < self.balls[z].x + b and self.balls[z].y - c < ( self.kao.y - a) ) and ( ( self.kao.y - a) < self.balls[z].y + c): 
                    self.kao.vy = - self.kao.vy
                    
                    self.remove(self.balls[z])
                    self.balls.pop(z)
                    return
        def on_key_press(self, symbol, modifiers):
            if symbol == key.RIGHT:
                self.player.x += 20
            elif symbol == key.LEFT:
                self.player.x -= 20
        
            #elif symbol == key.UP:
            #    self.player.y += 20
            #elif symbol == key.DOWN:
            #    self.player.y -= 20
    
    class MyActor2(cocos.text.Label):
        def __init__(self, text, x, y, size, rgba):
            super(MyActor2, 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 Layer01(ColorLayer):
        is_event_handler = True
        def __init__(self):
            super(Layer01, self).__init__(231, 76, 60, 1000)
            self.myactor = MyActor2("GAMEOVER", 400, 240, 32, (0, 200, 255, 255))
            self.add(self.myactor)
        def on_key_press(self, key, modifiers):
            director.replace(scene.Scene(Layer00()))
    
    director.init(width=640, height=480)
    director.run( scene.Scene( Layer00() ) )
    

    [image]

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