cocos2d 是一个功能强大的二维游戏开发框架,最初,它只是一个专为 Python 设计的小型项目,但它的跨平台能力和功能丰富的 API 很快就让它崭露头角,成为移动游戏开发的重要工具。
开发者社区也针对 cocos2d 进行了众多拓展,比如 cocos2d-x,它提供了对 Python 3 的支持,是参与现代游戏开发项目的完美选择。
无论是粒子效果、骨骼动画还是物理引擎的集成,cocos2d 都提供了一系列的高级特性来满足你的需要。
与其他的游戏开发框架比如 Pygame 或 Unity2D 相比,cocos2d 提供了更专注于 2D 的工具集和更好的性能,尤其是在动画效果和屏幕渲染方面
pip install cocos2d
# 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)View Code
los-cocos/cocos-site: backup cocos site (github.com)
其中下载doc文档:
运行html网页:
wawacode/cocos2d_fish_game: python使用cocos2d模块实现捕鱼达人的游戏,后期会继续进行加工整理。 (github.com)
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)View Code
标签:__,cocos,self,director,init,cocos2d From: https://www.cnblogs.com/shiningleo007/p/18027095