def on_mouse_motion(self, x, y, dx, dy): # check if mouse on menu items for menu_item in self.menu_items: if self.check_mouse_on_label(menu_item, x, y): menu_item.do(ScaleTo(1.2, 0.075)) else: menu_item.do(ScaleTo(1, 0.075))
def emphasize(self, duration=0.10): if self.emphasizable: self.emphasizable = False if duration < 0.10: duration = 0.10 half_duration = duration / 2.0 self.do(ScaleTo(1.2, half_duration) + ScaleTo(1, half_duration)) schedule_once(self.make_emphasizable, duration)
def _add_animation(self, node, drink): """ Construct an animation path for the label. """ node.scale = 0.5 content_offset = (node.scale * node.label.element.content_width + self._drink_distance) / 2 # Make sure only one item has focus minimum_offset = ( (self._focus_length + self._focus_ramp) * self._screen_width) / 2 if content_offset < minimum_offset: content_offset = minimum_offset content_time = float(content_offset) / self._scroll_speed # Set start position node.position = (self._screen_width + content_offset, self._ticker_y) # Construct the path # Move to beginning of screen (coordinates, _) = self._path[0] move_actions = MoveTo(coordinates, content_time) # Move to ramp up (coordinates, time) = self._path[1] move_actions += MoveTo(coordinates, time) # Do the ramp (coordinates, time) = self._path[2] move_actions += (MoveTo(coordinates, time) | (Delay(time / 2) + ScaleTo(1, time / 2)) | CallFunc(self._set_focused_drink, drink)) # Move in focus (coordinates, time) = self._path[3] move_actions += MoveTo(coordinates, time) # Ramp down (coordinates, time) = self._path[4] move_actions += MoveTo(coordinates, time) | (ScaleTo(0.5, time / 2)) # Move to end of screen (coordinates, time) = self._path[5] move_actions += MoveTo(coordinates, time) # Move out of sight move_actions += MoveTo((0 - content_offset, self._ticker_y), content_time) # Prepare spawn point spawn_actions = Delay(content_time * 2) + CallFunc(self._next_drink) self.do(spawn_actions) # Start animation node.do(move_actions + CallFunc(self._safe_kill, node))
def on_enter(self): super(Bg, self).on_enter() sprite = Sprite(self.image) self.add(sprite) sprite.position = 320, 240 sprite.do(ScaleTo(4,0)) action = MoveTo((640, 480), 4) | ( ScaleTo(2,2) + ScaleTo(4,2) ) sprite.do(action)
def main(): director.init() main_scene = cocos.scene.Scene() main_scene.add( ColorLayer( 255, 0, 0, 255 ) ) main_scene.add( TestLayer() ) main_scene.do( ScaleTo( 0.5, 2 ) ) director.run (main_scene)
def __init__(self): super( TestLayer, self ).__init__() x,y = director.get_window_size() self.sprite = Sprite( 'grossini.png', (x//2, y//2), scale = 0.1 ) self.add( self.sprite ) self.sprite.do( ScaleTo( 10, 2 ) )
def __init__(self): super(TestScene, self).__init__() scroller = layer.ScrollingManager() scrollable = tiles.load('road-map.tmx')['map0'] scroller.add(scrollable) self.add(scroller) template_action = (CallFunc(scroller.set_focus, 0, 0) + Delay(1) + CallFunc(scroller.set_focus, 768, 0) + Delay(1) + CallFunc(scroller.set_focus, 768, 768) + Delay(1) + CallFunc(scroller.set_focus, 1500, 768) + Delay(1) + ScaleTo(0.75, 1) + Delay(1) + CallFunc(scrollable.set_debug, True) + Delay(1) + CallFunc(director.window.set_size, 800, 600)) scroller.do(template_action)
# import cocos from cocos.director import director from cocos.actions import ScaleTo from cocos.sprite import Sprite from cocos.layer import * import pyglet class TestLayer(cocos.layer.Layer): def __init__(self): super( TestLayer, self ).__init__() x,y = director.get_window_size() sprite1 = Sprite( 'grossini.png' , (x/4, y/2) ) sprite2 = Sprite( 'grossinis_sister1.png', (x/2, y/2) ) sprite3 = Sprite( 'grossinis_sister2.png', (x/(4/3.0), y/2) ) self.add( sprite2 ) self.add( sprite1 ) self.add( sprite3 ) if __name__ == "__main__": director.init() main_scene = cocos.scene.Scene() main_scene.add( ColorLayer( 255, 0, 0, 255 ) ) main_scene.add( TestLayer() ) main_scene.do( ScaleTo( 0.5, 2 ) ) director.run (main_scene)
def zoom_out(): return ScaleTo(1.0, duration=0.2)
def zoom_in(): return ScaleTo(1.3, duration=0.2)
# This code is so you can run the samples without installing the package import sys import os sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) from itertools import cycle import cocos from pyglet import clock import random from cocos.actions import (MoveBy, MoveTo, RotateBy, RotateTo, ScaleBy, ScaleTo) HOP = ScaleBy(0.5, 0.15) + ScaleTo(1.0, 0.15) class HelloWorld(cocos.layer.Layer): def __init__(self): super(HelloWorld, self).__init__() # a cocos.text.Label is a wrapper of pyglet.text.Label # with the benefit of being a cocosnode self.left = cocos.sprite.Sprite('foot.png', (300, 100)) self.right = cocos.sprite.Sprite('footr.png', (375, 100)) self.add(self.left) self.add(self.right) self.steps = cycle([ random.choice([ self.hop_left,