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(BackgroundLayer, self).__init__() r = Game_elements.Player() self.image = cocos.sprite.Sprite(r.overImg) self.image.position = 400, 75 self.add(self.image, z=0) self.player = cocos.sprite.Sprite(r.overImg) self.player.position = 0, 295 self.add(self.player, z=1) self.enemy = cocos.sprite.Sprite(r.overImg) self.enemy.position = 385, 75 self.add(self.enemy, z=1) self.boss = cocos.sprite.Sprite(r.overImg) self.boss.scale = 0.4 rect = self.boss.get_rect() rect.midbottom = 600, 50 self.boss.position = rect.center self.add(self.boss, z=1) self.player.do(Repeat( MoveBy((-25, 0), 0.25) + MoveBy((50, 0), 0.5) + MoveBy((-25, 0), 0.25))) self.enemy.do(Repeat( MoveBy((-25, 0), 0.25) + MoveBy((50, 0), 0.5) + MoveBy((-25, 0), 0.25))) self.boss.do(Repeat(FadeTo(155, 0.5) + FadeTo(255, 0.5)))
def __init__(self): super(BackgroundLayer, self).__init__() self.image = cocos.sprite.Sprite(resources.background) self.image.position = 400, 325 self.add(self.image, z=0) self.player = cocos.sprite.Sprite(resources.player) self.player.position = 200, 75 self.add(self.player, z=1) self.enemy = cocos.sprite.Sprite(resources.enemy) self.enemy.position = 400, 75 self.add(self.enemy, z=1) self.boss = cocos.sprite.Sprite(resources.boss) self.boss.scale = 0.4 rect = self.boss.get_rect() rect.midbottom = 600, 50 self.boss.position = rect.center self.add(self.boss, z=1) self.player.do(Repeat(JumpBy((0, 0), 100, 1, 1))) self.enemy.do( Repeat( MoveBy((-25, 0), 0.25) + MoveBy((50, 0), 0.5) + MoveBy((-25, 0), 0.25))) self.boss.do(Repeat(FadeTo(155, 0.5) + FadeTo(255, 0.5)))
def __init__(self, box, sprites, time, delay): super(ParallaxBackground, self).__init__() self.batch = cocos.batch.BatchNode() self.imgA = Sprite(sprites[0]) #Sprite A self.imgB = Sprite(sprites[1]) #Sprite B self.box = box #(X, Y, W, H) Window Box (0 = X, 1 = Y, 2 = W, 3 = H) self.time = time #in second w = box[2] h = box[3] self.imgA.position = (w/2, h/2) self.imgB.position = (w + (w /2), h/2) #point A to point B for image A A = MoveBy((-w, 0),time) + Place((w + w/2 ,h/2)) + MoveBy((-w, 0),time) B = MoveBy(( -w - w , 0), time *2) + Place((w + (w /2), h/2)) #IMAGE ANIMATION self.imgA.do(Repeat(A)) self.imgB.do(Repeat(B)) self.batch.add(self.imgA) self.batch.add(self.imgB) self.add(self.batch)
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(Game, self).__init__(102, 102, 225, 255) self.collision_manager = cm.CollisionManagerBruteForce() self.player = cocos.sprite.Sprite('eleph2.png') self.player.position = 400, 25 self.player.velocity = 0, 0 self.player.speed = 150 self.player.scale = .5 self.add(self.player, z=2) self.player.cshape = cm.AARectShape(self.player.position, self.player.width // 2, self.player.height // 2) self.collision_manager.add(self.player) self.boss = cocos.sprite.Sprite('flaregun.png') self.boss.position = 400, 600 self.boss.scale = 0.4 self.add(self.boss, z=1) self.boss.cshape = cm.AARectShape(self.boss.position, self.boss.width // 2, self.boss.height // 2) self.collision_manager.add(self.boss) self.batch = cocos.batch.BatchNode() self.enemies = [cocos.sprite.Sprite('compass.png') for i in range(6)] positions = ((250, 125), (550, 125), (300, 325), (500, 325), (150, 475), (650, 475)) for num, enem in enumerate(self.enemies): enem.position = positions[num] enem.cshape = cm.AARectShape(enem.position, enem.width // 2, enem.height // 2) self.collision_manager.add(enem) self.batch.add(enem) self.add(self.batch, z=1) self.player.do(Move()) move_basic = MoveBy((120, 0), 1) self.enemies[0].do(Repeat(move_basic + Reverse(move_basic))) self.enemies[1].do(Repeat(Reverse(move_basic) + move_basic)) move_complex = (MoveBy((-75, 75), 1) + Delay(0.5) + MoveBy( (-75, -75), 1) + Delay(0.5) + MoveBy( (75, -75), 1) + Delay(0.5) + MoveBy((75, 75), 1) + Delay(0.5)) self.enemies[2].do(Repeat(move_complex)) self.enemies[3].do(Repeat(Reverse(move_complex))) move_jump = AccelDeccel(JumpBy((200, 0), 75, 3, 3)) move_jump_rot = AccelDeccel(RotateBy(360, 3)) self.enemies[4].do(Repeat(move_jump + Reverse(move_jump))) self.enemies[4].do(Repeat(move_jump_rot + Reverse(move_jump_rot))) self.enemies[5].do(Repeat(Reverse(move_jump) + move_jump)) self.enemies[5].do(Repeat(Reverse(move_jump_rot) + move_jump_rot)) self.schedule(self.update)
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 on_new_game(self): game_layer = Game() game_scene = cocos.scene.Scene(game_layer) cocos.director.director.push(game_scene) game_scene.do( Repeat(ShakyTiles3D(randrange=10, grid=(20, 20), duration=1)))
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(TitleLayer, self).__init__() texture = pyglet.image.load('assets/textures/background.png').get_texture() background = cocos.sprite.Sprite(texture, position=(320, 240)) # background.do(Repeat(Rotate(10, 2))) self.add(background) title_label = cocos.text.RichLabel(text="Planetor", position=(320, 320), font_name="monogram", font_size=60, anchor_x="center") self.add(title_label) # menu items self.menu_items = [] self.start_label = cocos.text.RichLabel(text="Game Start", position=(320, 260), font_name="monogram", font_size=24, anchor_x="center") self.menu_items.append(self.start_label) self.add(self.start_label) self.quit_label = cocos.text.RichLabel(text="Quit Game", position=(320, 220), font_name="monogram", font_size=24, anchor_x="center") self.menu_items.append(self.quit_label) self.add(self.quit_label) self.planet = cocos.sprite.Sprite(image="assets/planet.png", position=(320, -947), color=(44, 130, 42)) self.planet.do(Repeat(Rotate(10, 5))) self.add(self.planet)
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.sprite.do( Repeat ( Rotate( 360, 3 ) ) )
def __init__(self, sequence, bpm): super().__init__() self.left_foot = Sprite("./images/flat-black-l.png") self.right_foot = Sprite("./images/flat-black-r.png") self.left_foot.position = 200, 200 self.right_foot.position = 300, 200 self.add(self.left_foot) self.add(self.right_foot) speed = 60 / bpm dance = [s(speed) for s in sequence] right_leg_sequence = chain(*(step[0] for step in dance)) left_leg_sequence = chain(*(step[1] for step in dance)) self.right_foot.do(Repeat(sum(right_leg_sequence, Delay(0)))) self.left_foot.do(Repeat(sum(left_leg_sequence, Delay(0))))
def __init__(self): super(TestLayer, self).__init__() x, y = director.get_window_size() self.sprite = Sprite('grossini.png', (0, y / 2)) self.add(self.sprite) mov = AccelDeccel(MoveBy((x, 0), 4)) self.sprite.do(Repeat(mov + Reverse(mov)))
def __init__(self): super(TestLayer, self).__init__() x, y = director.get_window_size() self.sprite = Sprite('grossini.png', (x // 4, y // 2)) self.add(self.sprite) shake_part = MoveBy((-4.0, 0.0), 0.05) shake = shake_part + Reverse(shake_part) * 2 + shake_part self.sprite.do(MoveTo((x // 2, y // 2), 1) + Repeat(shake))
def take_damage(self, damage): self._life -= damage if self._life <= 0: self._life = 0 self.is_alive = False elif self._life < 1500 and not self.bonus_type: self._game.remove(self.sprite) self.sprite = Sprite("enemy_damaged.png") self.sprite.do(Repeat(RotateBy(360, 1))) self._game.add(self.sprite)
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 ) move = MoveBy((100,0), 1) rot = Rotate(360, 1) seq = Place((x/4,y/2)) + rot+move+rot+move self.sprite.do( Repeat ( seq ) )
def __init__(self,x,y,deltaX,deltaY,t): image = pyglet.image.load("resources/bat1.png") image_gride = pyglet.image.ImageGrid(image,4,4,item_width=32,item_height=32) animation = pyglet.image.Animation.from_image_sequence(image_gride[14:17],0.1,True) self.sprite = Sprite(animation) self.sprite.position = x,y przemieszczenie1 = deltaX, deltaY przemieszczenie2 = -deltaX, -deltaY lot = MoveBy(przemieszczenie1,t)+MoveBy(przemieszczenie2,t) self.sprite.do(Repeat(lot))
def __init__(self): super(Fly, self).__init__() rocket = cocos.sprite.Sprite('rocket.png') rocket.position = 128, 128 rocket.scale = 0.5 #在5秒内往右上方西东,最终横竖坐标分别增加50 #rocket.do( MoveBy( (320,240), duration=2) ) #move = MoveTo( (200,200),2 ) + MoveTo( (400,400),3 ) move = Repeat(RotateBy(360, 2)) rocket.do(move) self.add(rocket, z=0)
def __init__(self): super(MyTest, self).__init__() sprite = cocos.sprite.Sprite('icon.jpg') sprite.position = 240, 320 sprite.scale = 0.5 self.add(sprite) move = MoveBy((50, 100), duration=2) sprite.do(Repeat(Reverse(move) + move))
def __init__(self): super(JumpByTest, self).__init__() sprite = cocos.sprite.Sprite('icon.jpg') sprite.position = 240, 320 sprite.scale = 0.5 self.add(sprite) jump = JumpBy((50, 100), duration=2) sprite.do(Repeat(Reverse(jump) + jump))
def __init__(self, R): super(_PlayerAnimation, self).__init__() self.R = R self.SamplePlayer = Sprite(self.R.PLAYERANIME[0]) self.SamplePlayer.scale = 0.5 self.size = director.get_window_size() #SIMPLE ANIMATION _WH = self.SamplePlayer.get_AABB() _place = (-_WH.width, self.size[1] - 120) _move = MoveBy((self.size[0] + (_WH.width * 2), 0), 3) self.SamplePlayer.do(Repeat(Place(_place) + _move)) self.add(self.SamplePlayer)
def __init__(self): super(Oneself, self).__init__() self.position = (WIDTH / 2, 0 + 100) self.sprite = Sprite('airplane_710px_1219057_easyicon.net.png', scale=0.1) self.add(self.sprite) # self.cshape = cm.AARectShape(eu.Vector2(self.anchor_x, self.anchor_y), self.anchor_x, self.anchor_y) fire = Repeat(Delay(0.2) + CallFunc(self.fire)) self.do(fire)
def __init__(self): super().__init__("images/Audi.png", scale=.33) self.position = 400, 360 self.cshape = cm.AARectShape(eu.Vector2(*self.position), self.width / 4, self.height / 1.9) self.do( Repeat( MoveTo((random.randint(50, 1200), random.randint(50, 670)), 2) + MoveTo((random.randint(50, 1200), random.randint(50, 670)), 2)))
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 creat(self): image = pyglet.resource.image( 'airplane_625px_1234720_easyicon.net.png') sprite = Sprite(image, (randint(24, WIDTH - 24), HEIGHT), rotation=180, scale=0.1) sprite.health = 5 self.add(sprite) fire = Repeat(Delay(0.5) + CallFunc(self.fire, sprite)) sprite.do(fire) length = -sprite.position[1] move = MoveBy((0, length), duration=abs(length) / (200 / 1)) move.stop = lambda: self.remove(sprite) sprite.do(move)
def __init__(self): super(TestLayer, self).__init__() x, y = director.get_window_size() self.color1 = [255, 0, 0, 255] self.color2 = [0, 0, 255, 255] self.label = Label('', (x // 2, y // 2)) self.label.do(Rotate(360, 10)) self.label.do( Repeat( Delay(1) + CallFunc(self.set_color, 0) + Delay(1) + CallFunc(self.set_color, 1) + Delay(1) + CallFunc(self.set_color, 2))) self.add(self.label) self.set_color(2)
def on_key_press(self, k, mod): if (k in self.action_list.keys()): if k not in self.running_actions.keys() or (mod & key.MOD_SHIFT): if k in self.running_actions.keys(): self.view.remove_action(self.running_actions[k]) self.running_actions.pop(k, None) if(isinstance(self.action_list[k], InstantAction)): self.view.do(self.action_list[k]) else: self.running_actions[k] = self.view.do(Repeat(self.action_list[k])) else: self.view.remove_action(self.running_actions[k]) self.running_actions.pop(k, None)
def __init__(self): super(Pic, self).__init__(0, 0, 0, 0, width=160, height=160) # self.addsprite('image/pic.png') # self.cp = cocos.sprite.Sprite('image/cp.png') # self.cp.scale = 160 / self.cp.height # self.cp.position = (80, 80) # self.add(self.cp) self.addcp() self.cp.do(Repeat(RotateBy(360, duration=10))) self.gz = cocos.sprite.Sprite('image/gz.png') self.gz.scale = 0.3 self.gz.image_anchor = (117, 204) self.gz.position = (80, 170) self.add(self.gz, z=1) self.gz.do(RotateBy(25, duration=0.5))
def __init__(self): super(TestLayer, self).__init__() self.add(ProbeRect(w_ini, h_ini, (0, 0, 255, 255)), z=1) border_size = 10 inner = ProbeRect(w_ini - 2 * border_size, h_ini - 2 * border_size, (255, 0, 0, 255)) inner.position = (border_size, border_size) self.add(inner, z=2) outer = ProbeRect(w_ini + 2 * border_size, h_ini + 2 * border_size, (255, 255, 0, 255)) outer.position = (-border_size, -border_size) self.add(outer, z=0) self.size_from_stage = [(800, 600), (800, 640), (840, 600), (w_ini, h_ini)] self.stage = 0 self.do(Repeat(Delay(4) + CallFunc(self.rotate_sizes)))
def __init__(self,x,y,deltaX,deltaY,t): image = pyglet.image.load("resources/bat1.png") image_gride = pyglet.image.ImageGrid(image,4,4,item_width=32,item_height=32) self.animation = pyglet.image.Animation.from_image_sequence(image_gride[13:17],0.1,True) self.sprite = Sprite(self.animation) self.sprite.position = x,y self.sprite.cshape = cm.AARectShape( self.sprite.position, self.sprite.width/3, self.sprite.height/3) przemieszczenie1 = deltaX,deltaY przemieszczenie2 = -deltaX,-deltaY ruch = MoveBy(przemieszczenie1,t)+MoveBy(przemieszczenie2,t) self.sprite.do(Repeat(ruch))