| [サイトマップへ] |
Cocos2d は2次元ゲームのフレームワーク.Python で動く.
このWebページでは、次の基本的なことを、見本プログラムで演習する.
関連する資料:Cocos2d の概要 [PDF版], [パワーポイント版]
関連する資料:ゲームエンジン[PDF版], [パワーポイント版]
参考Webページ: http://python.cocos2d.org/doc.html
謝辞
Cocos2d の作者に感謝します.
このときに Anacoda のインストールも行う.
Python 処理系を使う.
※ Anacondaに入っている開発環境 spyder を使うのが簡単.
Windows のスタートメニュー「Anaconda3」の下にある.
左側がエディタになっている.上に実行ボタンがある
エディタの画面に,次の Python プログラムをコピー&ペーストし, 実行ボタンをクリック.
import random random.seed() print( random.random() )
■ 演習問題: 上の手順を行いなさい。
■ 演習問題: 「random.random()」 を 「random.random() * 10」に変えると 0 以上 10 未満の乱数になることを確認したい。
次のように書き替える
参考 Web ページ
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 プログラムをコピー&ペーストし, 実行ボタンをクリック.
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 プログラムをコピー&ペーストし, 実行ボタンをクリック.
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 プログラムをコピー&ペーストし, 実行ボタンをクリック.
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() ) )
■ 演習問題: 上の手順を行いなさい。