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(ScaleBy(10, 2) + ScaleBy(0.1, 2))
def __init__(self): Layer.__init__(self) self.config = Config() self.gallery = Gallery() self.schedule_interval(self.music_start, interval=7) self.intro_sound = load("music/sound_start.wav", streaming=False) self.intro_sound.play() self.is_event_handler = True background = Sprite(self.gallery.content["display"]["title"]) self.optimal_scale = ( self.config.window_width * self.display_pos_size["display_title"]["W"]) / background.width background.image_anchor = 0, 0 background.scale = self.optimal_scale background.x = self.config.window_width * self.display_pos_size[ "display_title"]["X"] background.y = self.config.window_height * self.display_pos_size[ "display_title"]["Y"] self.left_margin = background.x self.bottom_margin = background.y self.optimal_width = background.width self.optimal_height = background.height self.add(background) place_holder_image = Sprite(self.gallery.content["screen"]["title"]) place_holder_image.position = director.window.width / 2, director.window.height / 2 place_holder_image.scale = self.optimal_scale place_holder_image.do(ScaleBy(5.2, duration=25)) self.add(place_holder_image)
def __init__(self): #将层的背景调成蓝色 super(HelloWorld, self).__init__(64, 64, 224, 255) # 新建文字标签用于显示Hello World label = cocos.text.Label( 'Hello,World', # 如果要显示中文,需要使用支持中文的字体,比如"微软雅黑" font_name='Times New Roman', font_size=32, # 设置锚点为正中间 anchor_x='center', anchor_y='center') # 设置文字标签在层的位置.由于锚点为正中间,即"用手捏"标签的正中间,放到(320,240)的位置 label.position = 320, 240 # 把文字标签添加到层 self.add(label) # 新建一个精灵,在这里是一个小人(英文文档没有给示范图片,所以这个icon.png请自行找个q版小人图片,放在代码同目录下) sprite = cocos.sprite.Sprite('black.png') # 精灵锚点默认在正中间,只设置位置就好 sprite.position = 320, 240 # 放大三倍,添加到层,z轴设为1,比层更靠前 sprite.scale = 3 self.add(sprite, z=1) # 定义一个动作,即2秒内放大三倍 scale = ScaleBy(3, duration=2) # 标签的动作:重复执行放大三倍缩小三倍又放大三倍...Repeat即为重复动作,Reverse为相反动作 label.do(Repeat(scale + Reverse(scale))) # 精灵的动作:重复执行缩小三倍放大三倍又缩小三倍.. sprite.do(Repeat(Reverse(scale) + scale)) # 层的动作:重复执行10秒内360度旋转 self.do(RotateBy(360, duration=10))
def __init__(self): # 层调成蓝色 super(HelloWorld, self).__init__(64, 64, 224, 255) label = cocos.text.Label('Hello, World!', font_name='Times New Roman', font_size=32, anchor_x='center', anchor_y='center') label.position = 320, 240 self.add(label) # 新建一个精灵,在这里是一个小人(英文文档没有给示范图片,所以这个icon.png请自行找个q版小人图片,放在代码同目录下) sprite = cocos.sprite.Sprite('icon.jpg') # 精灵锚点默认在正中间,只设置位置就好 sprite.position = 320, 240 # 放大三倍,添加到层,z轴设为1,比层更靠前 sprite.scale = 1 self.add(sprite, z=1) # 定义一个动作,即2秒内放大三倍 scale = ScaleBy(2, duration=2) # 标签的动作:重复执行放大三倍缩小三倍又放大三倍...Repeat即为重复动作,Reverse为相反动作 label.do(Repeat(scale + Reverse(scale))) # 精灵的动作:重复执行缩小三倍放大三倍又缩小三倍... sprite.do(Repeat(Reverse(scale) + scale)) # 层的动作:重复执行10秒内360度旋转 self.do(RotateBy(360, duration=10))
def __init__(self): super(TestLayer, self).__init__() x, y = director.get_window_size() self.sprite = Sprite('grossini.png', (x / 2, y / 2)) self.add(self.sprite) self.sprite2 = Sprite('grossinis_sister1.png', (0, 101)) self.sprite.add(self.sprite2) self.sprite3 = Sprite('grossinis_sister2.png', (0, 102)) self.sprite2.add(self.sprite3) self.sprite.do(Rotate(360, 10)) self.sprite2.do(ScaleBy(2, 5) + ScaleBy(0.5, 5)) self.sprite2.do(Rotate(360, 10)) self.sprite3.do(Rotate(360, 10)) self.sprite3.do(ScaleBy(2, 5) + ScaleBy(0.5, 5))
def __init__(self): super(HelloWorld, self).__init__() label = cocos.text.Label('Hello, world', font_name='Times New Roman', font_size=32, anchor_x='center', anchor_y='center') label.position = 320, 240 self.add(label) sprite = cocos.sprite.Sprite('airplane_710px_1219057_easyicon.net.png') sprite.position = 320, 240 sprite.scale = 3 self.add(sprite, z=1) scale = ScaleBy(3, duration=2) label.do(Repeat(scale + Reverse(scale))) sprite.do(Repeat(Reverse(scale) + scale))
def __init__(self): super().__init__(64, 64, 224, 255) label = cocos.text.Label('Hello, World!', font_name='Times New Roman', font_size=32, anchor_x='center', anchor_y='center') label.position = 320, 240 self.add(label) sprite = cocos.sprite.Sprite('grossini.png') sprite.position = 320, 240 sprite.scale = 3 self.add(sprite, z=1) scale = ScaleBy(3, duration=2) label.do(Repeat(scale + Reverse(scale))) sprite.do(Repeat(Reverse(scale) + scale))
def __init__(self): super(Title, self).__init__() label = cocos.text.Label('测试Cocos2d的适应性', font_size=32, anchor_x='center', anchor_y='center') label.position = 320, 260 self.add(label) sprite = cocos.sprite.Sprite('horngirl.png') sprite.position = 320, 320 sprite.scale = 3 self.add(sprite, z=1) scale = ScaleBy(3, duration=2) label.do(Repeat(scale + Reverse(scale))) self.do(RotateBy(360, duration=10))
def __init__(self): super(Rotate, self).__init__(64, 64, 224, 255) label = cocos.text.Label('haha!', font_name = 'miaomiaomiao', font_size = 32, anchor_x = 'center', anchor_y = 'center') label.position = 320, 240 self.add(label) sprite = cocos.sprite.Sprite('b.png') sprite.position = 320, 240 sprite.scale = 3 self.add(sprite, z=1) scale = ScaleBy(3, duration=2) label.do(Repeat(scale + Reverse(scale))) sprite.do(RotateBy(360, duration=10))
def __init__(self, space, pos): self.is_alive = True self.life = constants.PLAYER_ARMOR # Mouse pointer 'body' # We need to make the hammer follow the cursor, but with some delay to create the effect of flying through the # air. We achieve this by adding an invisible damped string between the mouse pointer and the hammer. For this # we need to create a 'body' for mouse pointer to connect a damped string to. self.aim = pymunk.Body(1, 1) self.aim_shape = pymunk.Circle(self.aim, 1, (0, 0)) self.aim_shape.layers = 0b000 # The 'aim' should not collide with any objects self.aim.position = pos # Hammer body mass = 6 inertia = pymunk.moment_for_box(mass, 34, 45) self.body = pymunk.Body(mass, inertia) self.body.position = pos self.body.angular_velocity_limit = math.pi / 4 self.body_angle_limit = math.pi / 4. # Not to allow upside down flying and also for more natural look and feel body_shape = pymunk.Poly(self.body, [(-17, -22.5), (-17, 22.5), (17, 22.5), (17, -22.5)]) body_shape.elasticity = 0.9 body_shape.friction = 0.8 body_shape.collision_type = 1 body_shape.layers = 0b001 # set layers for body and sword (below) to forbid their collision self.body_shape = body_shape # Connect aim and hammer with a DampedSpring - this should create the effect of flying through the air self.hammer_move = pymunk.constraint.DampedSpring( self.aim, self.body, (0, 0), (0, 0), 1, 4000.0, 1.0) # Hammer sprite self.body_sprite = Sprite('body.png') self.propeller_sprite = Sprite('propeller.png') # Scaling the propeller back and forth will create the effect of its movement action = ScaleBy(0.2, 0.1) self.propeller_sprite.do(Repeat(action + Reverse(action))) # Sword mass = 15 inertia = pymunk.moment_for_box(mass, 8, 120) self.sword = pymunk.Body(mass, inertia) self.sword.position = (pos[0], pos[1] - 60) self.sword.angular_velocity_limit = 1.8 * math.pi # Do not allow too rapid swinging, it's not natural and also # can 'break' physics due to enemies flying through the sword sword_shape = pymunk.Poly(self.sword, [(-4, -60), (-4, 60), (4, 60), (4, -60)]) sword_shape.elasticity = 0.9 sword_shape.friction = 0.8 sword_shape.layers = 0b010 sword_shape.collision_type = 2 self.sword_shape = sword_shape # Connect hammer and sword with a joint - place the joint a little below the center of the mass of the main # body to make the body return to vertical state self.sword_hammer = pymunk.constraint.PivotJoint( self.body, self.sword, (pos[0], pos[1] - 1)) # Sword sprite self.sword_sprite = cocos.sprite.Sprite('sword.png') # Make the hammer 'fly' by applying the force opposite to the gravity self.body.apply_force( -(self.aim.mass + self.body.mass + self.sword.mass) * space.gravity) space.add(self.body, self.body_shape, self.aim, self.aim_shape, self.hammer_move, self.sword, self.sword_shape, self.sword_hammer) self.space = space
# 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,