Ejemplo n.º 1
0
def get_test_scene():
    template_action = ac.Repeat(  # repeat forever (
        ac.Delay(1.0)  # wait 1 second
        +  # and then
        ac.Rotate(360, 2.0)  # rotate 360 degrees in 2 seconds
        +  # and then
        ac.Delay(1.0)  # wait 1 second
        +  # and then
        ac.ScaleTo(2.0, 1.0)  #  zoom to 2x in 1 second
        +  # and then
        ac.ScaleTo(1.0, 1.0)  # zoom to 1x in 1 second
    )

    w, h = director.get_window_size()
    world = cocos.layer.Layer()

    # bluish square
    bluish_square = cocos.layer.ColorLayer(0,
                                           160,
                                           176,
                                           255,
                                           width=100,
                                           height=100)
    # transform_anchor set to the center position of itself
    bluish_square.transform_anchor = (bluish_square.width / 2.0,
                                      bluish_square.height / 2.0)
    bluish_square.position = (w // 3, h // 2)
    bluish_square.do(template_action)
    world.add(bluish_square, z=2)

    mark_anchor = cocos.layer.ColorLayer(161, 191, 54, 255, width=8, height=8)
    mark_anchor.position = (bluish_square.x + bluish_square.transform_anchor_x,
                            bluish_square.y + bluish_square.transform_anchor_y)
    world.add(mark_anchor, z=3)

    # redish square
    redish_square = cocos.layer.ColorLayer(201,
                                           43,
                                           0,
                                           255,
                                           width=100,
                                           height=100)
    # transform_anchor set to the bottomleft corner of itself
    redish_square.transform_anchor = (0, 0)
    redish_square.position = (w * 2 // 3, h // 2)
    redish_square.do(template_action)
    world.add(redish_square, z=2)

    mark_anchor = cocos.layer.ColorLayer(161, 191, 54, 255, width=8, height=8)
    mark_anchor.position = (redish_square.x + redish_square.transform_anchor_x,
                            redish_square.y + redish_square.transform_anchor_y)
    world.add(mark_anchor, z=3)

    scene = cocos.scene.Scene()
    scene.add(world)
    return scene
Ejemplo n.º 2
0
    def test_target_set(self):
        global rec, next_done
        node = CocosNode()
        name1 = '1'
        a1 = UAction(name1)
        composite = ac.Repeat(a1)

        rec = []
        a_copy = node.do(composite)
        assert a_copy.action.target == node
Ejemplo n.º 3
0
    def test_instantiation(self):
        global rec, next_done
        name1 = '1'
        a1 = UAction(name1)
        assert isinstance(a1, ac.Action)

        rec = []
        composite = ac.Repeat(a1)
        assert isinstance(composite, ac.Action)
        assert composite.duration is None
        assert len(rec) == 0
Ejemplo n.º 4
0
    def test_target_set_next_time(self):
        global rec, next_done
        node = CocosNode()
        name1 = '1'
        a1 = UAction(name1)
        composite = ac.Repeat(a1)

        a_copy = node.do(composite)
        dt = 0.1
        next_done = 1
        node._step(dt)
        assert a_copy.action.target == node
Ejemplo n.º 5
0
 def create_ghosts(self):
     ghosts_start = self.scenario.ghosts_start
     blinky_x, blinky_y = ghosts_start[0]
     clyde_x, clyde_y = ghosts_start[1]
     inky_x, inky_y = ghosts_start[2]
     pinky_x, pinky_y = ghosts_start[3]
     self.add(
         actors.Blinky(
             blinky_x, blinky_y,
             ac.Delay(5) + ac.Repeat(self.scenario.ghosts_action[0])))
     self.add(
         actors.Clyde(
             clyde_x, clyde_y,
             ac.Delay(7) + ac.Repeat(self.scenario.ghosts_action[1])))
     self.add(
         actors.Inky(
             inky_x, inky_y,
             ac.Delay(9) + ac.Repeat(self.scenario.ghosts_action[2])))
     self.add(
         actors.Pinky(
             pinky_x, pinky_y,
             ac.Delay(11) + ac.Repeat(self.scenario.ghosts_action[3])))
Ejemplo n.º 6
0
    def __init__(self):
        # Blueish color
        super(HelloActions, self).__init__(
            r=64,
            g=64,
            b=224,
            a=255,
        )

        label = text.Label(
            'Hello World',
            font_name='Microsoft YaHei UI',
            font_size=32,
            anchor_x='center',
            anchor_y='center',
        )
        label.position = 320, 240

        self.add(label)

        # Add a cocos Sprite.
        sprite_ = sprite.Sprite('HSCard.png')
        sprite_.position = 320, 240

        # [NOTE] The sprite will 3 times bigger.
        sprite_.scale = 0.58

        # [NOTE] z is the precedence of the sprite. This sprite will on the top of the label.
        self.add(sprite_, z=1)

        # [LEARN] We create a ScaleBy action. It will scale 3 times the object in 2 seconds:
        scale = actions.ScaleBy(3, duration=2)

        # [LEARN] '+' is sequence action here.
        # 'Reverse' reverse the action.
        label.do(actions.Repeat(scale + actions.Reverse(scale)))

        sprite_.do(actions.Repeat(actions.Reverse(scale) + scale))
Ejemplo n.º 7
0
    def test_life_cycle(self):
        global rec, next_done
        next_done = 0
        name1 = '1'
        a1 = UAction(name1)
        node = CocosNode()
        composite = ac.Repeat(a1)

        #1st start
        rec = []
        a_copy = node.do(composite)
        assert rec[0] == (name1, 'start')
        assert len(rec) == 1
        assert not a_copy.done()

        #step in first repetition
        dt = 0.1
        next_done = 0
        rec = []
        node._step(dt)
        assert rec[0] == (name1, 'step', dt)
        assert len(rec) == 1
        assert not a_copy.done()

        #termination first repetion, start second repetition
        next_done = 1
        rec = []
        node._step(dt)
        assert rec[0] == (name1, 'step', dt)
        assert rec[1] == (name1, 'stop')
        assert rec[2] == (name1, 'start')
        assert len(rec) == 3
        assert not a_copy.done()

        #step in second repetition
        next_done = 0
        rec = []
        node._step(dt)
        assert rec[0] == (name1, 'step', dt)
        assert len(rec) == 1
        assert not a_copy.done()
Ejemplo n.º 8
0
def main():
  global keyboard # Declare this as global so it can be accessed within class methods.
  # Initialize the window
  director.init(width=size[0], height=size[1], autoscale=True, resizable=True)

  # Create a layer and add a sprite to it.
  player_layer = layer.Layer()
  molecule = sprite.Sprite('sprites/molecule.png')
  molecule.scale = 2
  player_layer.add(molecule, z=1)
  scale = actions.ScaleBy(3, duration=2)

  # Add a Label, because we can.
  label = cocos.text.Label('Hello, world@' + str(deltaTime), font_name='Times New Roman', font_size=32, anchor_x='left', anchor_y='center')
  label.position = 0, size[1]/2
  label.velocity = 0, 0
  player_layer.add(label)

  # Set initial position and velocity.
  molecule.position = (size[0]/2, size[1]/2)
  molecule.velocity = (0, 0)

  # Set the sprite's movement class and run some actions.
  molecule.do(actions.Repeat(scale + actions.Reverse(scale)))

  label.do(Me())

  # Rotate the entire player_layer (includes ALL nodes, will rotate ONCE)
  player_layer.do(actions.RotateBy(360, duration=10))

  # Create a scene and set its initial layer.
  main_scene = scene.Scene(player_layer)

  # Set the sprite's movement class.
  keyboard = key.KeyStateHandler()
  director.window.push_handlers(keyboard)

  # Play the scene in the window.
  director.run(main_scene)