cocos2d
cocos2d 是一个功能强大的二维游戏开发框架,最初,它只是一个专为 Python 设计的小型项目,但它的跨平台能力和功能丰富的 API 很快就让它崭露头角,成为移动游戏开发的重要工具。
开发者社区也针对 cocos2d 进行了众多拓展,比如 cocos2d-x,它提供了对 Python 3 的支持,是参与现代游戏开发项目的完美选择。
无论是粒子效果、骨骼动画还是物理引擎的集成,cocos2d 都提供了一系列的高级特性来满足你的需要。
与其他的游戏开发框架比如 Pygame 或 Unity2D 相比,cocos2d 提供了更专注于 2D 的工具集和更好的性能,尤其是在动画效果和屏幕渲染方面
pip install cocos2d
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# This is a sample Python script. # Press Shift+F10 to execute it or replace it with your code. # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. # # def print_hi(name): # # Use a breakpoint in the code line below to debug your script. # print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint. # # # # Press the green button in the gutter to run the script. # if __name__ == '__main__': # print_hi('PyCharm') # See PyCharm help at https://www.jetbrains.com/help/pycharm/ import cocos class HelloWorld(cocos.layer.Layer): def __init__(self): super(HelloWorld,self).__init__() #创建并添加一个标签到这个layer label=cocos.text.Label('Hello,world', font_name='TimesNewRoman', font_size=32, anchor_x='center', anchor_y='center') label.position=320,240 self.add(label) if __name__=="__main__": #初始化导演 cocos.director.director.init() #创建一个 layer hello_layer=HelloWorld() #创建一个场景包含这个layer main_scene=cocos.scene.Scene(hello_layer) #运行场景 cocos.director.director.run(main_scene)
los-cocos/cocos-site: backup cocos site (github.com)
其中下载doc文档:
运行html网页:
wawacode/cocos2d_fish_game: python使用cocos2d模块实现捕鱼达人的游戏,后期会继续进行加工整理。 (github.com)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import cocos import pyglet import random class Fish(cocos.sprite.Sprite): def __init__(self,index): index = "0" + str(index) if index < 10 else str(index) textures=[] for i in range(1,11): name_i="0"+str(i) if i<10 else str(i) fish_name_i="textures/fish"+index+"_"+name_i+".png" texture=pyglet.resource.image(fish_name_i) textures.append(texture) animation=pyglet.image.Animation.from_image_sequence(textures,0.1) super(Fish, self).__init__(animation) self.y=random.randint(10,480) self.position = 800,self.y self.swim() def swim(self): self.y=random.randint(10,480) self.position=800,self.y minutes=random.randint(2,8) self.do(cocos.actions.MoveTo((-20,self.y),minutes)+ cocos.actions.CallFunc(self.swim)) def on_enter(self): super(Fish, self).on_enter() cocos.director.director.window.push_handlers(self.on_mouse_press) def on_mouse_press(self,x,y,button,modifier): if x > self.x - self.width * 1 / 2 and x < self.x + self.width * 1 / 2 and y > self.y - self.height * 1 / 2 and y < self.y + self.height * 1 / 2: self.explode() def explode(self): self.stop() self.kill() class Background(cocos.layer.Layer): def __init__(self): super(Background,self).__init__() self.width,self.height=cocos.director.director.get_window_size() sprite=cocos.sprite.Sprite("textures/bg.jpg") sprite.position=self.width//2,self.height//2 self.add(sprite) for i in range(2,11): fish=Fish(i) self.add(fish) if __name__=="__main__": cocos.director.director.init(width=800,height=480); background=Background(); main_scene=cocos.scene.Scene(background) cocos.director.director.run(main_scene)
cocos/samples at master · los-cocos/cocos (github.com)
例子里的 demo_grid_effects.py
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# # cocos2d # http://los-cocos.github.io/cocos-site/ # # Particle Engine done by Phil Hassey # http://www.imitationpickles.org # from __future__ import division, print_function, unicode_literals import six import sys import os sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) import pyglet from pyglet.gl import * from cocos.director import * from cocos.menu import * from cocos.scene import * from cocos.layer import * from cocos.actions import * from cocos.sprite import Sprite import random rr = random.randrange class Fire: def __init__(self, x, y, vy, frame, size): self.x, self.y, self.vy, self.frame, self.size = x, y, vy, frame, size class FireManager(Layer): def __init__(self, view_width, num): super(FireManager, self).__init__() self.view_width = view_width self.goodies = [] self.batch = pyglet.graphics.Batch() self.fimg = pyglet.resource.image('fire.jpg') self.group = pyglet.sprite.SpriteGroup(self.fimg, blend_src=GL_SRC_ALPHA, blend_dest=GL_ONE) self.vertex_list = self.batch.add(4 * num, GL_QUADS, self.group, 'v2i', 'c4B', ('t3f', self.fimg.tex_coords * num)) for n in range(0, num): f = Fire(0, 0, 0, 0, 0) self.goodies.append(f) self.vertex_list.vertices[n * 8:(n + 1) * 8] = [0, 0, 0, 0, 0, 0, 0, 0] self.vertex_list.colors[n * 16:(n + 1) * 16] = [0, 0, 0, 0, ] * 4 self.schedule(self.step) def step(self, dt): w, h = self.fimg.width, self.fimg.height fires = self.goodies verts, clrs = self.vertex_list.vertices, self.vertex_list.colors for n, f in enumerate(fires): if not f.frame: f.x = rr(0, self.view_width) f.y = rr(-120, -80) f.vy = rr(40, 70) / 100.0 f.frame = rr(50, 250) f.size = 8 + pow(rr(0.0, 100) / 100.0, 2.0) * 32 f.scale = f.size / 32.0 x = f.x = f.x + rr(-50, 50) / 100.0 y = f.y = f.y + f.vy * 4 c = 3 * f.frame / 255.0 r, g, b = (min(255, int(c * 0xc2)), min(255, int(c * 0x41)), min(255, int(c * 0x21))) f.frame -= 1 ww, hh = w * f.scale, h * f.scale x -= ww / 2 if six.PY2: vs = map(int, [x, y, x + ww, y, x + ww, y + hh, x, y + hh]) else: vs = list(map(int, [x, y, x + ww, y, x + ww, y + hh, x, y + hh])) verts[n * 8:(n + 1) * 8] = vs clrs[n * 16:(n + 1) * 16] = [r, g, b, 255] * 4 def draw(self): glPushMatrix() self.transform() self.batch.draw() glPopMatrix() class SpriteLayer(Layer): def __init__(self): super(SpriteLayer, self).__init__() sprite1 = Sprite('grossini.png') sprite2 = Sprite('grossinis_sister1.png') sprite3 = Sprite('grossinis_sister2.png') sprite1.position = (320, 240) sprite2.position = (620, 100) sprite3.position = (20, 100) self.add(sprite1) self.add(sprite2) self.add(sprite3) ju_right = JumpBy((600, 0), height=100, jumps=4, duration=5) ju_left = JumpBy((-600, 0), height=100, jumps=4, duration=5) rot1 = Rotate(180 * 4, duration=5) sprite1.opacity = 128 sc = ScaleBy(9, 5) rot = Rotate(180, 5) sprite1.do(Repeat(sc + Reverse(sc))) sprite1.do(Repeat(rot + Reverse(rot))) sprite2.do(Repeat(ju_left + Reverse(ju_left))) sprite2.do(Repeat(Reverse(rot1) + rot1)) sprite3.do(Repeat(ju_right + Reverse(ju_right))) sprite3.do(Repeat(rot1 + Reverse(rot1))) class MainMenu(Menu): def __init__(self): # call superclass with the title super(MainMenu, self).__init__("GROSSINI'S SISTERS") pyglet.font.add_directory('.') # you can override the font that will be used for the title and the items self.font_title['font_name'] = 'You Are Loved' self.font_title['font_size'] = 72 self.font_item['font_name'] = 'You Are Loved' self.font_item_selected['font_name'] = 'You Are Loved' # you can also override the font size and the colors. see menu.py for # more info # example: menus can be vertical aligned and horizontal aligned self.menu_valign = CENTER self.menu_halign = CENTER items = [] items.append(MenuItem('New Game', self.on_new_game)) items.append(MenuItem('Options', self.on_options)) items.append(MenuItem('Scores', self.on_scores)) items.append(MenuItem('Quit', self.on_quit)) self.create_menu(items, zoom_in(), zoom_out()) # Callbacks def on_new_game(self): # director.set_scene(StartGame()) print("on_new_game()") def on_scores(self): self.parent.switch_to(2) def on_options(self): self.parent.switch_to(1) def on_quit(self): director.pop() class OptionMenu(Menu): def __init__(self): super(OptionMenu, self).__init__("GROSSINI'S SISTERS") self.font_title['font_name'] = 'You Are Loved' self.font_title['font_size'] = 72 self.font_item['font_name'] = 'You Are Loved' self.font_item_selected['font_name'] = 'You Are Loved' self.menu_valign = BOTTOM self.menu_halign = RIGHT items = [] items.append(MenuItem('Fullscreen', self.on_fullscreen)) items.append(ToggleMenuItem('Show FPS: ', self.on_show_fps, True)) items.append(MenuItem('OK', self.on_quit)) self.create_menu(items, shake(), shake_back()) # Callbacks def on_fullscreen(self): director.window.set_fullscreen(not director.window.fullscreen) def on_quit(self): self.parent.switch_to(0) def on_show_fps(self, value): director.show_FPS = value class ScoreMenu(Menu): def __init__(self): super(ScoreMenu, self).__init__("GROSSINI'S SISTERS") self.font_title['font_name'] = 'You Are Loved' self.font_title['font_size'] = 72 self.font_item['font_name'] = 'You Are Loved' self.font_item_selected['font_name'] = 'You Are Loved' self.menu_valign = BOTTOM self.menu_halign = LEFT self.create_menu([MenuItem('Go Back', self.on_quit)]) def on_quit(self): self.parent.switch_to(0) def init(): director.init(resizable=True, width=640, height=480) def start(): director.set_depth_test() firelayer = FireManager(director.get_window_size()[0], 250) spritelayer = SpriteLayer() menulayer = MultiplexLayer(MainMenu(), OptionMenu(), ScoreMenu()) scene = Scene(firelayer, spritelayer, menulayer) twirl_normal = Twirl(center=(320, 240), grid=(16, 12), duration=15, twirls=6, amplitude=6) twirl = AccelDeccelAmplitude(twirl_normal, rate=4.0) lens = Lens3D(radius=240, center=(320, 240), grid=(32, 24), duration=5) waves3d = AccelDeccelAmplitude( Waves3D(waves=18, amplitude=80, grid=(32, 24), duration=15), rate=4.0) flipx = FlipX3D(duration=1) flipy = FlipY3D(duration=1) flip = Flip(duration=1) liquid = Liquid(grid=(16, 12), duration=4) ripple = Ripple3D(grid=(32, 24), waves=7, duration=10, amplitude=100, radius=320) shakyt = ShakyTiles3D(grid=(16, 12), duration=3) corners = CornerSwap(duration=1) waves = AccelAmplitude(Waves(waves=8, amplitude=50, grid=(32, 24), duration=5), rate=2.0) shaky = Shaky3D(randrange=10, grid=(32, 24), duration=5) quadmove = QuadMoveBy( delta0=(320, 240), delta1=(-630, 0), delta2=(-320, -240), delta3=(630, 0), duration=2) fadeout = FadeOutTRTiles(grid=(16, 12), duration=2) cornerup = MoveCornerUp(duration=1) cornerdown = MoveCornerDown(duration=1) shatter = ShatteredTiles3D(randrange=16, grid=(16, 12), duration=4) shuffle = ShuffleTiles(grid=(16, 12), duration=1) orbit = OrbitCamera( radius=1, delta_radius=2, angle_x=0, delta_x=-90, angle_z=0, delta_z=180, duration=4) jumptiles = JumpTiles3D(jumps=2, duration=4, amplitude=80, grid=(16, 12)) wavestiles = WavesTiles3D(waves=3, amplitude=60, duration=8, grid=(16, 12)) turnoff = TurnOffTiles(grid=(16, 12), duration=2) # firelayer.do( # spritelayer.do( # menulayer.do( scene.do( Delay(3) + ripple + Delay(2) + wavestiles + Delay(1) + twirl + liquid + Delay(2) + shakyt + Delay(2) + ReuseGrid() + shuffle + Delay(4) + ReuseGrid() + turnoff + Reverse(turnoff) + Delay(1) + shatter + flip + Delay(2) + Reverse(flip) + flipx + Delay(2) + ReuseGrid() + flipy + Delay(2) + ReuseGrid() + flipx + Delay(2) + ReuseGrid() + flipy + Delay(2) + lens + ReuseGrid() + ((orbit + Reverse(orbit)) | waves3d) + Delay(1) + corners + Delay(2) + Reverse(corners) + waves + Delay(2) + ReuseGrid() + shaky + jumptiles + Delay(1) + cornerup + Delay(1) + Reverse(cornerdown) + Delay(1) + fadeout + Reverse(fadeout) + Delay(2) + quadmove + Delay(1) + Reverse(quadmove) + StopGrid() ) scene.do(Delay(10) + OrbitCamera(delta_z=-360 * 3, duration=10 * 4)) firelayer.do(Delay(4) + Repeat(RotateBy(360, 10))) return scene def run(scene): director.run(scene) if __name__ == "__main__": init() s = start() run(s)
,Best Wish 不负年华