以前看过一本关于精简工作内容的书,要把工作中重复的东西都简化
今天我就来简化cocos3建立.h文件和.cpp文件
ClassName="TestSprite" Type="Sprite" #.h file write file = open(ClassName+'.h', 'w') str='''#pragma once #include "cocos2d.h" USING_NS_CC; class ClassName:public Type { public: virtual bool init(); CREATE_FUNC(ClassName); }; ''' str=str.replace("ClassName",ClassName) str=str.replace("Type",Type) file.write(str) file.close() #.cpp file write file = open(ClassName+'.cpp', 'w') str='''#include "ClassName.h" bool ClassName::init() { if(!Type::init()) { return false; } return true; } ''' str=str.replace("ClassName",ClassName) str=str.replace("Type",Type) file.write(str) file.close()
14年12月20日跟新
ClassName="Lever3Scene" Type="Scene" #.h file write if Type=="Scene": str='''#pragma once #include "cocos2d.h" USING_NS_CC; class ClassName:public Type { public: virtual bool init(); static Scene* createScene(); CREATE_FUNC(ClassName); }; ''' else : str='''#pragma once #include "cocos2d.h" USING_NS_CC; class ClassName:public Type { public: virtual bool init(); CREATE_FUNC(ClassName); }; ''' file = open(ClassName+'.h', 'w') str=str.replace("ClassName",ClassName) str=str.replace("Type",Type) file.write(str) file.close() #.cpp file write if Type=="Scene": str='''#include "ClassName.h" Scene* ClassName::createScene() { auto scene = Scene::create(); auto layer = ClassName::create(); scene->addChild(layer); return scene; } bool ClassName::init() { if(!Type::init()) { return false; } return true; } ''' else : str='''#include "ClassName.h" bool ClassName::init() { if(!Type::init()) { return false; } return true; } ''' file = open(ClassName+'.cpp', 'w') str=str.replace("ClassName",ClassName) str=str.replace("Type",Type) file.write(str) file.close()