首页 > 其他分享 >cocos2d

cocos2d

时间:2024-02-22 13:00:39浏览次数:23  
标签:__ cocos self director init cocos2d

cocos2d 是一个功能强大的二维游戏开发框架,最初,它只是一个专为 Python 设计的小型项目,但它的跨平台能力和功能丰富的 API 很快就让它崭露头角,成为移动游戏开发的重要工具。

开发者社区也针对 cocos2d 进行了众多拓展,比如 cocos2d-x,它提供了对 Python 3 的支持,是参与现代游戏开发项目的完美选择。

无论是粒子效果、骨骼动画还是物理引擎的集成,cocos2d 都提供了一系列的高级特性来满足你的需要。

与其他的游戏开发框架比如 Pygame 或 Unity2D 相比,cocos2d 提供了更专注于 2D 的工具集和更好的性能,尤其是在动画效果和屏幕渲染方面

 

pip install cocos2d

cocos2d (los-cocos.github.io)

# 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

相关文章

  • Cocos2dx中应用内部横竖屏切换
    {未实践https://blog.csdn.net/ByAlick/article/details/83009941}{语言环境:Cocos2dx3.6Lua5.1AndroidStudio3.0.1问题:如何在应用内部进行横竖屏切换?解决思路:         由于产品需求,必须在应用内部进行横竖屏切换(苦比〜_〜),没办法,网上教程一大把,大多数是在一......
  • Cocos Creator 2.X(Cocos2d-js)游戏资源目录分析&逆向还原
    拿到了某变态服游戏,打开lib看到了libcocos2djs.so,判断版本为2.4。游戏把资源文件下载到了/data下,因此需要root。目录结构如下:g4-start/├──config.json├──import│ ├──1e│ │ └──1ea6e4bcd.json│ └──e7│   └──e76cb2d2-fa48-4916-......
  • mac m1 编译cocos2d-x 在模拟器上运行 一些问题汇总
     如果涉及到侵权请联系本人删除 1  》〉/Users/yzfhkms-m/Library/Developer/Xcode/DerivedData/formi-dlcfwgxcmidqefdkxnvnfwfprpfs/Build/Products/Debug-iphonesimulator/formi-mobile.appisnotavalidpathtoanexecutablefile.Pleaserebuildtheprojectto......
  • 【教程】步兵 cocos2dx 加密和混淆
    文章目录摘要引言正文代码加密具体步骤代码加密具体步骤测试和配置阶段IPA重签名操作步骤总结参考资料 摘要本篇博客介绍了针对iOS应用中的Lua代码进行加密和混淆的相关技术。通过对Lua代码进行加密处理,可以确保应用代码的安全性,同时提高性能表现。文......
  • 【终极教程】Cocos2dx服务端重构(优化cocos2dx服务端)
    【终极教程】Cocos2dx服务端重构(优化cocos2dx服务端)文章目录概述问题概述1.代码混淆代码加密具体步骤测试和配置阶段IPA重签名操作步骤2.缺乏文档3.缺乏推荐的最佳实践4.性能问题总结 概述Cocos2dx是一个非常流行的跨平台游戏引擎,开发者可以使用这个引擎来开......
  • 【教程】使用ipagurd打包与混淆Cocos2d-x的Lua脚本
    【教程】使用ipagurd打包与混淆Cocos2d-x的Lua脚本文章目录摘要引言正文1.准备工作2.使用ipaguard处理Lua文件3.运行ipagurd进行混淆代码加密具体步骤测试和配置阶段IPA重签名操作步骤4.IPA重签名与发布总结 摘要本文将介绍如何使用ipagurd工具对Cocos2d-......
  • 【教程】cocos2dx资源加密混淆方案详解
    ​ 【教程】cocos2dx资源加密混淆方案详解1,加密,采用blowfish或其他2,自定是32个字符的混淆code3,对文件做blowfish加密,入口文件加密前将混淆code按约定格式(自定义的文件头或文件尾部)写入到文件4,遍历资源目录,对每个文件做md5混淆,混淆原始串=“相对路径”+“文件名”+混......
  • A piece of code for loading and caching Skeleton Animation in IO task [Cocos2dx.
    /****************************************************************************Copyright(c)2017-2018XiamenYajiSoftwareCo.,Ltd.http://www.cocos2d-x.orgPermissionisherebygranted,freeofcharge,toanypersonobtainingacopyofthissoft......
  • cocos2dx 3.4配置vs2013 + lua环境
    cocos2dx3.4版本及3.4以下版本都可以用cocoside调试lua,说实话,那个编辑比较差,卡就卡半天。我们就用vs来进行调试lua环境配置步骤如下:第1步:安装vs2013第2步,下载插件,地址:https://babelua.codeplex.com/第3步,安装..BabeLuaFor2013.....vsix第4部,安装完后,菜单栏会显示lua菜单,并且会......
  • 如何实现字幕效果,cocos2dx ,Lua
    实现这个字幕效果,其实很简单,只需要画一个遮罩即可完成,带遮罩内部显示,外部隐藏,如下有C++,lua两个版本的代码:functionGameClientView:updateAdvertisement() --body localGG=self._Panel:getChildByName("notification_bg") localGGW=GG:getContentSize().width local......