1. 程式人生 > >python cocos2d 常用的操作函式解析

python cocos2d 常用的操作函式解析

移動目標:

move = MoveBy((200,0), duration=5) # Moves 200 pixels to the right in 5 seconds.
move = MoveTo((320,240), duration=5) # Moves to the pixel (320,240) in 5 seconds
jump = JumpBy((320,0), 100, 5, duration=5) # Jumps to the right 320 pixels
                                            # doing 5 jumps of 100 pixels
                                            # of height in 5 seconds

accel_move = Accelerate(move)               # accelerates action move 可以將上面的兩個move變數合起來同事操作
當然還可以用 
move = MoveTo((200, 200), 2) + MoveTo((400, 200), 3) + MoveTo((100, 300), 1) 
-------------------------------------------------------------------------------
這樣的方式
# Move the sprite to coords x=50, y=10 in 8 seconds
action = MoveTo((50,10), 8)
# Move the sprite 50 pixels to the left in 8 seconds
action = MoveBy((-50,0), 8)
----------------------------------------------------------
action = Jump(50,200, 5, 6)    # Move the sprite 200 pixels to the right
sprite.do(action)            # in 6 seconds, doing 5 jumps
                               # of 50 pixels of height
-----------------------------------------------------------
action = JumpTo(50,200, 5, 6)  # Move the sprite 200 pixels to the right
sprite.do(action)            # in 6 seconds, doing 5 jumps
                               # of 50 pixels of height
-------------------------------------------------------------
# Move the sprite 200 pixels to the right and up
action = JumpBy((100,100),200, 5, 6)
sprite.do(action)            # in 6 seconds, doing 5 jumps
                               # of 200 pixels of height
---------------------------------------------------------------------
action = Bezier(bezier_conf.path1, 5)   # Moves the sprite using the
sprite.do(action)                       # bezier path 'bezier_conf.path1'
                                          # in 5 seconds
------------------------------------------------------------------------
# rotates the sprite to angle 180 in 2 seconds
action = RotateTo(180, 2)
sprite.do(action)
-------------------------------------------------------------------------
# rotates the sprite 180 degrees in 2 seconds
action = RotateBy(180, 2)
sprite.do(action)
-------------------------------------------------------------------------
# scales the sprite to 5x in 2 seconds
action = ScaleTo(5, 2)
sprite.do(action)
-------------------------------------------------------------------------
# scales the sprite by 5x in 2 seconds
action = ScaleBy(5, 2)
sprite.do(action)
-------------------------------------------------------------------------
action = Delay(2.5)
sprite.do(action)
-------------------------------------------------------------------------
action = RandomDelay(2.5, 4.5)      # delays the action between 2.5 and 4.5 seconds
sprite.do(action)
-------------------------------------------------------------------------
action = FadeOut(2)
sprite.do(action)
-------------------------------------------------------------------------
action = FadeIn(2)
sprite.do(action)
-------------------------------------------------------------------------
action = FadeTo(128, 2)
sprite.do(action)
-------------------------------------------------------------------------
# Blinks 10 times in 2 seconds
action = Blink(10, 2)
sprite.do(action)
-------------------------------------------------------------------------
# rotates the sprite 180 degrees in 2 seconds clockwise
# it starts slow and ends fast
action = Accelerate(Rotate(180, 2), 4)
sprite.do(action)
-------------------------------------------------------------------------
# rotates the sprite 180 degrees in 2 seconds clockwise
# it starts slow, gets fast and ends slow
action = AccelDeccel(RotateBy(180, 2))
sprite.do(action)
-------------------------------------------------------------------------
# rotates the sprite 180 degrees in 1 secondclockwise
action = Speed(Rotate(180, 2), 2)
sprite.do(action)





import cocos
from cocos.actions import MoveBy, Place, MoveTo, RotateBy, JumpBy, Accelerate
from cocos.director import director
import pyglet


class KeyDisplay(cocos.layer.Layer):
    is_event_handler = True

    def __init__(self):
        super(KeyDisplay, self).__init__()

        self.text = cocos.text.Label('Keys: ', font_size=18, x=100, y=280)
        self.add(self.text)

        self.keys_pressed = set()

    def update_text(self):
        key_names = [pyglet.window.key.symbol_string(k) for k in self.keys_pressed]
        self.text.element.text = 'Keys: ' + ','.join(key_names)

    def on_key_press(self, key, modifiers):
        # 按下按鍵自動觸發本方法
        self.keys_pressed.add(key)
        self.update_text()

    def on_key_release(self, key, modifiers):
        # 鬆開按鍵自動觸發本方法
        self.keys_pressed.remove(key)
        self.update_text()


class MouseDisplay(cocos.layer.Layer):
    is_event_handler = True

    def __init__(self):
        super(MouseDisplay, self).__init__()

        self.text = cocos.text.Label('Mouse @', font_size=18,
                                     x=100, y=240)
        self.add(self.text)

    def on_mouse_motion(self, x, y, dx, dy):
        # dx,dy為向量,表示滑鼠移動方向
        self.text.element.text = 'Mouse @ {}, {}, {}, {}'.format(x, y, dx, dy)

    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
        self.text.element.text = 'Mouse @ {}, {}, {}, {}'.format(x, y, buttons, modifiers)

    def on_mouse_press(self, x, y, buttons, modifiers):
        # 按下滑鼠按鍵不僅更新滑鼠位置,還改變標籤的位置.這裡使用director.get_virtual_coordinates(),用於保證即使視窗縮放過也能正確更新位置,如果直接用x,y會位置錯亂,原因不明
        self.text.element.text = 'Mouse @ {}, {}, {}, {}'.format(x, y, buttons, modifiers)
        self.text.element.x, self.text.element.y = director.get_virtual_coordinates(x, y)


class LayerBlue(cocos.layer.ColorLayer):
    def __init__(self):
        super(LayerBlue, self).__init__(0, 128, 128, 255,
                                        width=120, height=80)
        self.position = (50, 50)


class LayerRed(cocos.layer.ColorLayer):
    def __init__(self):
        super(LayerRed, self).__init__(128, 0, 128, 255,
                                       width=120, height=80)
        self.position = (100, 80)


class LayerYellow(cocos.layer.ColorLayer):
    def __init__(self):
        super(LayerYellow, self).__init__(128, 128, 0, 255,
                                          width=120, height=80)
        self.position = (150, 110)


class Fly(cocos.layer.Layer):
    def __init__(self):
        super(Fly, self).__init__()
        rocket = cocos.sprite.Sprite('image/rocket.png')
        rocket.position = 128, 128
        rocket.scale = 0.10
        # rocket.do(MoveBy((500, 350), duration=0.5))
        # 把精靈瞬移到座標(120, 330)
        # rocket.do(Place((120, 330)))
        # move = MoveTo((200, 200), 2) + MoveTo((400, 200), 3) + MoveTo((100, 300), 1)
        # move = MoveTo((500,400),10) | RotateBy(12000000,100)
        # jump = JumpBy((320, 0), 200, 6, duration=5)
        # action = MoveBy((50, 0), 2)
        # rocket.do(move)
        move = MoveBy((200, 0), duration=5)  # Moves 200 pixels to the right in 5 seconds.

        move = MoveTo((320, 240), duration=5)  # Moves to the pixel (320,240) in 5 seconds

        jump = JumpBy((320, 0), 100, 5, duration=5)  # Jumps to the right 320 pixels
        # doing 5 jumps of 100 pixels
        # of height in 5 seconds

        accel_move = Accelerate(move)  # accelerates action move
        rocket.do(accel_move)
        self.add(rocket, z=0)