【24点游戏】cocos2dx 源码

1.  4个数字 24点判断

double Calc(double a, double b, string oper)
{
	double result = 0;
	const char *p = oper.c_str();

	if (p != NULL)
	{
		switch (*p)
		{
		case ‘+‘:
			result = a + b; break;
		case ‘-‘:
			result = a - b; break;
		case ‘*‘:
			result = a * b; break;
		case ‘/‘:
			result = a / b; break;
		default: result = 0; break;
		}
	}
	return result;
}

int main(int argc,char* argv[])
{
#if 0
	int value; 

	value=cal();
	printf("the result is:%d\n",value); 

	int src[]={8, 8, 8, 1};
	printf("  %d \n",calc(src));

	//getchar();
#endif
	//-----------------------------------
	double nums[] = { 8, 8, 8, 1 };
	string x = "", y = "", z = "";
	double a = 0, b = 0, c = 0, d = 0;
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			for (int k = 0; k < 4; k++)
			{
				x = i == 0 ? "+" : i == 1 ? "-" : i == 2 ? "*" : "/";
				y = j == 0 ? "+" : j == 1 ? "-" : j == 2 ? "*" : "/";
				z = k == 0 ? "+" : k == 1 ? "-" : k == 2 ? "*" : "/";
				for (int ia = 0; ia < 4; ia++)
				{
					a = nums[ia];
					for (int ib = 0; ib < 4; ib++)
					{
						if (ia == ib)
						{
							continue;
						}
						b = nums[ib];
						for (int ic = 0; ic < 4; ic++)
						{
							if (ic == ia || ic == ib)
							{
								continue;
							}
							c = nums[ic];
							d = nums[6 - ia - ib - ic];
							if (abs(Calc(Calc(a, b, x), Calc(c, d, z), y) - (double)24) < 0.01)
							{
								printf("(%f %s %f) %s ( %f %s %f) = 24 \n", a, x.c_str(), b, y.c_str(), c, z.c_str(), d);
								break;

							}
							if (abs(Calc(Calc(Calc(a, b, x), c, y), d, z) - (double)24) < 0.01)
							{
								printf("((%f %s %f)%s %f)%s %f = 24 \n", a, x.c_str(), b, y.c_str(), c, z.c_str(), d);
								break;

							}
							if (abs(Calc(Calc(a, Calc(b, c, y), x), d, z) - (double)24) < 0.01)
							{
								printf("(%f %s (%f %s %f)) %s %f = 24 \n",a, x.c_str(), b, y.c_str(), c, z.c_str(), d);
								break;

							}
							if (abs(Calc(a, Calc(b, Calc(c, d, z), y), x) - (double)24) < 0.01)
							{
								printf("%f %s (%f %s (%f %s  %f)) = 24 \n", a, x.c_str(), b, y.c_str(), c, z.c_str(), d);
								break;

								//System.Console.WriteLine(Math.Abs(Calc(a, Calc(b, Calc(c, d, z), y), x)));
							}
							if (abs(Calc(a, Calc(Calc(b, c, y), d, z), x) - (double)24) < 0.01)
							{
								printf("%f %s ((%f %s %f)%s %f) = 24 \n", a, x.c_str(), b, y.c_str(), c, z.c_str(), d);
								break;

							}
						}
					}
				}

			}
		}
	}

	getchar();
	return 0;
}

1. HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

#include "Box2D/Box2D.h"

#include "MenuLayer.h"

#include "SimpleAudioEngine.h"

class HelloWorld : public cocos2d::CCLayer
{
public:
    // Here‘s a difference. Method ‘init‘ in cocos2d-x returns bool, instead of returning ‘id‘ in cocos2d-iphone
    virtual bool init();  

    // there‘s no ‘id‘ in cpp, so we recommand to return the exactly class pointer
    static cocos2d::CCScene* scene();

    // a selector callback
    virtual void menuStartCallback(CCObject* pSender);

    // implement the "static node()" method manually

    LAYER_NODE_FUNC(HelloWorld);
};

#endif  // __HELLOWORLD_SCENE_H__

3. helloworldscene.cpp

#include "HelloWorldScene.h"

#include "MenuLayer.h"

using namespace cocos2d;

CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do
    {
        // ‘scene‘ is an autorelease object
        scene = CCScene::node();
        CC_BREAK_IF(! scene);

        // ‘layer‘ is an autorelease object
        HelloWorld *layer = HelloWorld::node();
        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    bool bRet = false;
    do
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////

        CC_BREAK_IF(! CCLayer::init());

        //////////////////////////////////////////////////////////////////////////
        // add your codes below...
        //////////////////////////////////////////////////////////////////////////

        CCSize mysize = CCDirector::sharedDirector()->getWinSize();

        MenuLayerMainMenu *MyMenu = new MenuLayerMainMenu();
        this->addChild(MyMenu);

        bRet = true;
    } while (0);

    return bRet;
}

void HelloWorld::menuStartCallback(CCObject* pSender)
{
    CCLog("1234");
}

4.menuLayer.h

#ifndef __MENULAYER_H__
#define __MENULAYER_H__

#include "cocos2d.h"

using namespace cocos2d;

class MenuLayerMainMenu : public CCLayer
{

public:
    MenuLayerMainMenu();
    ~MenuLayerMainMenu()
    {

    }

public:
    virtual void menuCallbackEnable(CCObject* pSender);

};

#endif

5. menuLayer.cpp

#include "cocos2d.h"
#include "MenuLayer.h"
#include "HelloWorldScene.h"
#include "GameScene.h"

MenuLayerMainMenu::MenuLayerMainMenu()
{
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    // Font Item
    CCMenuItemFont *itemFont = CCMenuItemFont::itemWithString("Start Game", this, menu_selector(MenuLayerMainMenu::menuCallbackEnable) );

    itemFont->setFontSizeObj(20);
    itemFont->setFontName("Marker Felt");
    itemFont->setColor( ccc3(200,200,255) );
    itemFont->setPosition(ccp(size.width/2,size.height/2));

    CCMenu* menu = CCMenu::menuWithItems(itemFont, NULL);

    addChild(menu);
    menu->setPosition(CCPointZero);

}
void MenuLayerMainMenu::menuCallbackEnable(CCObject* pSender)
{
    CCLog("1234");
    CCScene *s=GameScene::scene();
    CCDirector::sharedDirector()->replaceScene(s);
}

6. poker.h

#ifndef _POKER_H_
#define _POKER_H_
#include "cocos2d.h"
using namespace cocos2d;
class PokerSprite:public CCSprite
{
public:
    PokerSprite();
    static PokerSprite* createPokerSprite(int number,float pointx,float pointy,int width,int height);
    void setPokerRect(const CCRect& rect);
    CCRect& GetPokerRect();
    int getNumber();
    void setNumber(int number);
   // LAYER_NODE_FUNC(HelloWorld);
private:
    int m_number;
    CCLabelTTF* m_lablenumber;
    CCLayerColor* m_layercolor;
    CCRect PokerRect;
    void initPokerSprite(int number,float pointx,float pointy,int width,int height);
};
#endif // !_SQUARE_SPRITE_H_

7. poker.cpp

#include "Poker.h"

PokerSprite::PokerSprite()
:m_number(0)
,m_lablenumber(NULL)
,m_layercolor(NULL)
{
}
PokerSprite* PokerSprite::createPokerSprite(int number,float pointx,float pointy,int width,int height)
{
    PokerSprite *pSprite = new PokerSprite();
    if (pSprite && pSprite->init())
    {
        pSprite->autorelease();
        pSprite->initPokerSprite( number, pointx, pointy, width, height);
        return pSprite;
    }
    CC_SAFE_DELETE(pSprite);
    return NULL;
}
int PokerSprite::getNumber()
{
    return m_number;
}
void PokerSprite::setNumber(int number)
{
    m_number=number;
    m_lablenumber->setString("");
    m_layercolor->setColor(ccc3(200,190,180));
    if (m_number>0)
    {
        CCString Poker ;
        Poker.initWithFormat("%i",m_number);
        m_lablenumber->setString(Poker.getCString());

    }
}

void PokerSprite::setPokerRect(const CCRect& rect)
{
    PokerRect = rect;
}

CCRect& PokerSprite::GetPokerRect()
{
    return PokerRect;
}
void PokerSprite::initPokerSprite(int number,float pointx,float pointy,int width,int height)
{
    m_number=number;
    m_layercolor=CCLayerColor::layerWithColor(ccc4(200,190,180,255),width,height);
    m_layercolor->setPosition(ccp(pointx,pointy));
    this->addChild(m_layercolor);
    //创建字体
    m_lablenumber=CCLabelTTF::labelWithString("","arial.ttf",50);
    m_lablenumber->setColor(ccc3(0,0,0));
    m_lablenumber->setPosition(ccp(m_layercolor->getContentSize().width/2,m_layercolor->getContentSize().height/2));
    m_layercolor->addChild(m_lablenumber);
    setNumber(number);
    CCRect PokerRect = CCRect(pointx, pointy, width, height);
    setPokerRect(PokerRect);
}

8. GameScene.h

#ifndef __GAME_SCENCE_H__
#define __GAME_SCENCE_H__

#include "cocos2d.h"
#include "Poker.h"

class GameScene : public cocos2d::CCLayer
{
public:

    virtual bool init();  

    virtual void registerWithTouchDispatcher();
    virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
    virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);  

    static cocos2d::CCScene* scene();
public:
    virtual void menuCallbackSUBMIT(CCObject* pSender);
    virtual void menuCallbackNEXT(CCObject* pSender);
    virtual void menuCallbackPLUS(CCObject* pSender);
    virtual void menuCallbackDEC(CCObject* pSender);
    virtual void menuCallbackMULTIPLY(CCObject* pSender);
    virtual void menuCallbackDIVIDE(CCObject* pSender);
    virtual void menuCallbackLBracket(CCObject* pSender);
    virtual void menuCallbackRBracket(CCObject* pSender);
    virtual void menuCallbackDEL(CCObject* pSender);

    PokerSprite* m_PokerSquares[4];  

    LAYER_NODE_FUNC(GameScene);

public:
    int PokerNum[4];
};

#endif

9. GameScene.cpp

#include "cocos2d.h"
#include "GameScene.h"
#include "Poker.h"

#define  LABEL_IN_PUT   1

int add_fun(int x,int y){return x+y;}
int sub_fun(int x,int y){return x-y;}
int mul_fun(int x,int y){return x*y;}
int div_fun(int x,int y){return x/y;}
int (*func[])(int ,int)={add_fun,sub_fun,mul_fun,div_fun}; 

int num,curch;
char chtbl[]="+-*/()=";
char corch[]="+-*/()=0123456789";
char * test = NULL;

int getid()
{
    int i;
    if(*test>=‘0‘&& *test<=‘9‘)
    {
        for(num=0;*test>=‘0‘&&*test<=‘9‘;test++)
            num=10*num+(*test-‘0‘);
        return -1;
    }
    else
    {
        for(i=0;chtbl[i];i++)
            if(chtbl[i]==*test)
                break;
        if(i<=5)
            test++;
        return i;
    }
} 

int cal()
{
    int x1,x2,x3,op1,op2,i;
    i=getid();
    if(i==4)
        x1=cal();
    else
        x1=num;
    op1=getid();
    if(op1>=5)
        return x1;
    i=getid();
    if(i==4)
        x2=cal();
    else
        x2=num;
    op2=getid();
    while(op2!=6 && op2!= 5)
    {
        i=getid();
        if(i==4)
            x3=cal();
        else
            x3=num;
        if((op1/2==0)&&(op2/2==1))
            x2=(*func[op2])(x2,x3);
        else
        {
            x1=(*func[op1])(x1,x2);
            x2=x3;
            op1=op2;
        }
        op2=getid();
    }
    return (*func[op1])(x1,x2);
}
bool GameScene::init()
{
    bool ret=false;
    do
    {
        CC_BREAK_IF(!CCLayer::init());
        /* 设置允许触屏事件 */
        this->setIsTouchEnabled(true);

        CCSize winSize = CCDirector::sharedDirector()->getWinSize();
        int SquareSize=(winSize.width-10*5)/4;

        CCLayerColor* layerColorBG= CCLayerColor::layerWithColor(ccc4(180,170,160,255));
        this->addChild(layerColorBG,0);  

        for (int x = 0; x < 4; x++)
        {   int randNum = rand() % 13;
            randNum++;
            //PokerNum[x] = randNum;
            PokerSprite* pSquareSprite=PokerSprite::createPokerSprite(randNum,(winSize.width - SquareSize*4)/2 + (x) * (SquareSize),winSize.height - SquareSize ,SquareSize,SquareSize);
            m_PokerSquares[x] =  pSquareSprite;
            this->addChild(pSquareSprite);
        }

        /* 下一题按钮 */
        CCMenuItemFont *itemNEXT = CCMenuItemFont::itemWithString("NEXT", this, menu_selector(GameScene::menuCallbackNEXT) );

        itemNEXT->setFontSizeObj(25);
        itemNEXT->setFontName("Marker Felt");
        itemNEXT->setColor( ccc3(0,0,0) );
        itemNEXT->setPosition(ccp(winSize.width / 2 + 100,winSize.height/5));
        /* 提交按钮 */
        CCMenuItemFont *itemSUBMIT = CCMenuItemFont::itemWithString("SUBMIT", this, menu_selector(GameScene::menuCallbackSUBMIT) );
        itemSUBMIT->setFontSizeObj(25);
        itemSUBMIT->setFontName("Marker Felt");
        itemSUBMIT->setColor( ccc3(0,0,0) );
        itemSUBMIT->setPosition(ccp(winSize.width / 2 + 200,winSize.height/5));
        /* + - * / ( ) 按钮 */
         CCMenuItemFont *itemPLUS = CCMenuItemFont::itemWithString(" + ", this, menu_selector(GameScene::menuCallbackPLUS) );
         itemPLUS->setColor( ccc3(0,0,0) );
         itemPLUS->setPosition(ccp(20,winSize.height/5));
         CCMenuItemFont *itemDEC = CCMenuItemFont::itemWithString(" - ", this, menu_selector(GameScene::menuCallbackDEC) );
         itemDEC->setColor( ccc3(0,0,0) );
         itemDEC->setPosition(ccp(70,winSize.height/5));
         CCMenuItemFont *itemMULTIPLY = CCMenuItemFont::itemWithString(" * ", this, menu_selector(GameScene::menuCallbackMULTIPLY) );
         itemMULTIPLY->setColor( ccc3(0,0,0) );
         itemMULTIPLY->setPosition(ccp(120,winSize.height/5));
         CCMenuItemFont *itemDIVIDE = CCMenuItemFont::itemWithString(" / ", this, menu_selector(GameScene::menuCallbackDIVIDE) );
         itemDIVIDE->setColor( ccc3(0,0,0) );
         itemDIVIDE->setPosition(ccp(170,winSize.height/5));
         CCMenuItemFont *itemLBracket = CCMenuItemFont::itemWithString(" ( ", this, menu_selector(GameScene::menuCallbackLBracket) );
         itemLBracket->setColor( ccc3(0,0,0) );
         itemLBracket->setPosition(ccp(220,winSize.height/5));
         CCMenuItemFont *itemRBracket = CCMenuItemFont::itemWithString(" ) ", this, menu_selector(GameScene::menuCallbackRBracket) );
         itemRBracket->setColor( ccc3(0,0,0) );
         itemRBracket->setPosition(ccp(270,winSize.height/5));

         CCMenuItemFont *itemDEL = CCMenuItemFont::itemWithString(" Clean ", this, menu_selector(GameScene::menuCallbackDEL) );
         itemDEL->setFontSizeObj(20);
         itemDEL->setColor( ccc3(0,0,0) );
         itemDEL->setPosition(ccp(winSize.width - 30, winSize.height/2));

        CCMenu* menu = CCMenu::menuWithItems(itemNEXT,itemSUBMIT,itemPLUS,itemDEC,itemMULTIPLY,itemDIVIDE,itemLBracket,itemRBracket,itemDEL, NULL);

        this->addChild(menu);
        menu->setPosition(CCPointZero);

        /* 静态文本框 */
        CCLabelTTF* label = CCLabelTTF::labelWithString("", "Arial", 30);
        addChild(label, 1, LABEL_IN_PUT);
        label->setPosition( ccp(winSize.width/2, winSize.height/2) );

        ret=true;
    }while(0);

    return ret;
}

CCScene* GameScene::scene()
{
    CCScene *scene=NULL;
    do
    {
        scene=CCScene::node();
        CC_BREAK_IF(!scene);

        GameScene *my=GameScene::node();//需要定义CREATE_FUNC(MyLayer)宏
        CC_BREAK_IF(!my);
        scene->addChild(my);

    }while(0);
    return scene;
}

void GameScene::menuCallbackDEL(CCObject* pSender)
{
    CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT);
    label->setString("");
}

void GameScene::menuCallbackNEXT(CCObject* pSender)
{
    for (int x = 0; x < 4; x++)
    {   int randNum = rand() % 13;
        randNum++;
        //PokerNum[x] = randNum;
        m_PokerSquares[x]->setNumber(randNum);

    }
     CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT);
     label->setString("");
}

void GameScene::menuCallbackSUBMIT(CCObject* pSender)
{
    /* 计算值 */
    CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT);
    const char * StringALready = label->getString();
    int size = strlen(StringALready);
    char * TempString = (char *)malloc(size + 2);
    TempString = strcpy(TempString, StringALready);
    *(TempString + size ) = ‘=‘;
    *(TempString + size + 1 ) = ‘\0‘;

    test = TempString;
    int value = cal();

    CCLog("%d",value);

}

/* +按钮回调函数 */
void GameScene::menuCallbackPLUS(CCObject* pSender)
{
    CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT);
    const char * StringALready = label->getString();
    int size = strlen(StringALready);
    char * TempString = (char *)malloc(size + 2);
    TempString = strcpy(TempString, StringALready);
    *(TempString + size ) = ‘+‘;
    *(TempString + size + 1 ) = ‘\0‘;
    label->setString(TempString);
}
/* -按钮回调函数 */
void GameScene::menuCallbackDEC(CCObject* pSender)
{
    CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT);
    const char * StringALready = label->getString();
    int size = strlen(StringALready);
    char * TempString = (char *)malloc(size + 2);
    TempString = strcpy(TempString, StringALready);
    *(TempString + size ) = ‘-‘;
    *(TempString + size + 1 ) = ‘\0‘;
    label->setString(TempString);
}
/* *按钮回调函数 */
void GameScene::menuCallbackMULTIPLY(CCObject* pSender)
{
    CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT);
    const char * StringALready = label->getString();
    int size = strlen(StringALready);
    char * TempString = (char *)malloc(size + 2);
    TempString = strcpy(TempString, StringALready);
    *(TempString + size ) = ‘*‘;
    *(TempString + size + 1 ) = ‘\0‘;
    label->setString(TempString);
}
/* /按钮回调函数 */
void GameScene::menuCallbackDIVIDE(CCObject* pSender)
{
    CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT);
    const char * StringALready = label->getString();
    int size = strlen(StringALready);
    char * TempString = (char *)malloc(size + 2);
    TempString = strcpy(TempString, StringALready);
    *(TempString + size ) = ‘/‘;
    *(TempString + size + 1 ) = ‘\0‘;
    label->setString(TempString);
}
/* (按钮回调函数 */
void GameScene::menuCallbackLBracket(CCObject* pSender)
{
    CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT);
    const char * StringALready = label->getString();
    int size = strlen(StringALready);
    char * TempString = (char *)malloc(size + 2);
    TempString = strcpy(TempString, StringALready);
    *(TempString + size ) = ‘(‘;
    *(TempString + size + 1 ) = ‘\0‘;
    label->setString(TempString);
}
/* )按钮回调函数 */
void GameScene::menuCallbackRBracket(CCObject* pSender)
{
    CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT);
    const char * StringALready = label->getString();
    int size = strlen(StringALready);
    char * TempString = (char *)malloc(size + 2);
    TempString = strcpy(TempString, StringALready);
    *(TempString + size ) = ‘)‘;
    *(TempString + size + 1 ) = ‘\0‘;
    label->setString(TempString);
}

bool GameScene::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
    //CCLog("Began");
    return true;
}

void itoa(int i,char*string)
{
    int power,j;
    j=i;
    for(power=1;j>=10;j/=10)
        power*=10;
    for(;power>0;power/=10)
    {
        *string++=‘0‘+i/power;
        i%=power;
    }
    *string=‘\0‘;
}

void GameScene::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
    /* 获取POKER的数字 并添加到文本框 */
    CCPoint touchPoint=convertTouchToNodeSpace(pTouch);
    for (int x = 0; x < 4; x++)
    {
        CCRect temp = m_PokerSquares[x]->getTextureRect();
        if(m_PokerSquares[x]->boundingBox().CCRectContainsPoint(m_PokerSquares[x]->GetPokerRect(),touchPoint))
        {
            char * Temp = (char*)malloc(10);
            int num = m_PokerSquares[x]->getNumber();

            itoa(num, Temp);
            CCLog(Temp);

            CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT);
            const char * StringALready = label->getString();
            int size = strlen(StringALready);
            char * TempString = (char *)malloc(size + 3);
            TempString = strcpy(TempString, StringALready);
            if (num >0 && num <= 9)
            {
                *(TempString + size ) = ‘0‘ + num;
                *(TempString + size + 1 ) = ‘\0‘;
            }
            else
            {
                *(TempString + size ) = ‘0‘ + (num / 10);
                *(TempString + size + 1 ) = ‘0‘ + (num % 10);
                *(TempString + size + 2 ) = ‘\0‘;

            }

            label->setString(TempString);
        }
    }
   // CCLog("%f %f",touchPoint.x, touchPoint.y);

}

void GameScene::registerWithTouchDispatcher()
{
    /* 注册触屏事件 */
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}
时间: 2024-11-07 07:35:30

【24点游戏】cocos2dx 源码的相关文章

远古守卫/cocos2d-x 源码/塔防游戏/高仿王国保卫战

本源码高度模仿IOS游戏王国保卫战,由国外IOS商业开发教程网站raywenderlich内部工程师制作,有非常完整的关卡设计,战斗流程,长达12个关卡,各种敌兵,怪物,箭塔,炮塔一应俱全,还有魔法系统,成就系统以及后台数据库,几乎达到了可以上线的商业游戏级别.代码严格规范,提供了一整套塔防游戏实现的解决方案,如一波波怪如何走出,怪的行走路线,我方小兵行动AI,塔搜索和攻击敌人的AI,代码严格按照MVC(model, view, controller)规范,让人一目了然,复用度极高.可惜原作者只

cocos2d-x《农场模拟经营养成》游戏完整源码

cocos2d-x农场模拟经营养成游戏完整源码,cocos2d-x引擎开发,使用JSON交互,支持IOS与 Android,解压后1016MB. 很强大的游戏源码 完整游戏源码下载 http://www.yxkfw.com/thread-7170-1-1.html 版权声明:本文为博主原创文章,未经博主允许不得转载.

Cocos2d-X手游源码/iOS/Android/cocos2dx源码/AppStore/手游资源“集中营”

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源码下载:点我传送 游戏开发程序猿加班熬夜伤脑筋伤身体,花钱买技术,不如买游戏源代码. 节省开发周期和大量资金投入(工资.测试.学习),可以腾出大量时间精力和女友约会聊天,放松心情! 现在放出多套精品游戏cocos2d-x源代码. 大小游戏可以换皮上线!可学习 二次开发! 完整可编译,游戏体验安装包在网盘里放着,需要的看看. 店铺

Chrome自带恐龙小游戏的源码研究(完)

在上一篇<Chrome自带恐龙小游戏的源码研究(七)>中研究了恐龙与障碍物的碰撞检测,这一篇主要研究组成游戏的其它要素. 游戏分数记录 如图所示,分数及最高分记录显示在游戏界面的右上角,每达到100分就会出现闪烁特效,游戏第一次gameover时显示历史最高分.分数记录器由DistanceMeter构造函数实现,以下是它的全部代码: 1 DistanceMeter.dimensions = { 2 WIDTH: 10, //每个字符的宽度 3 HEIGHT: 13, //每个字符的高 4 DE

Chrome自带恐龙小游戏的源码研究(七)

在上一篇<Chrome自带恐龙小游戏的源码研究(六)>中研究了恐龙的跳跃过程,这一篇研究恐龙与障碍物之间的碰撞检测. 碰撞盒子 游戏中采用的是矩形(非旋转矩形)碰撞.这类碰撞优点是计算比较简单,缺点是对不规则物体的检测不够精确.如果不做更为精细的处理,结果会像下图: 如图所示,两个盒子虽然有重叠部分,但实际情况是恐龙和仙人掌之间并未发生碰撞.为了解决这个问题,需要建立多个碰撞盒子: 不过这样还是有问题,观察图片,恐龙和仙人掌都有四个碰撞盒子,如果每次Game Loop里都对这些盒子进行碰撞检测

Chrome自带恐龙小游戏的源码研究(五)

在上一篇<Chrome自带恐龙小游戏的源码研究(四)>中实现了障碍物的绘制及移动,从这一篇开始主要研究恐龙的绘制及一系列键盘动作的实现. 会眨眼睛的恐龙 在游戏开始前的待机界面,如果仔细观察会发现恐龙会时不时地眨眼睛.这是通过交替绘制这两个图像实现的: 可以通过一张图片来了解这个过程: 为实现图片的切换,需要一个计时器timer,并且需要知道两张图片切换的时间间隔msPerFrame.当计时器timer的时间大于切换的时间间隔msPerFrame时,将图片切换到下一张,到达最后一张时又从第一张

Chrome自带恐龙小游戏的源码研究(六)

在上一篇<Chrome自带恐龙小游戏的源码研究(五)>中实现了眨眼睛的恐龙,这一篇主要研究恐龙的跳跃. 恐龙的跳跃 游戏通过敲击键盘的Spacebar或者Up来实现恐龙的跳跃.先用一张图来表示整个跳跃的过程: 首先规定向下为正方向,即重力加速度(g)为正,起跳的速度(v)为负,恐龙距离画布上方的距离为yPos: 每一帧动画中,速度都会与重力加速度相加得到新的速度,再用新的速度与yPos相加得到新的yPos,改变恐龙的位置为新的yPos,表现出来为yPos不断减小: 当恐龙升至最高点,此时速度为

Chrome自带恐龙小游戏的源码研究(四)

在上一篇<Chrome自带恐龙小游戏的源码研究(三)>中实现了让游戏昼夜交替,这一篇主要研究如何绘制障碍物. 障碍物有两种:仙人掌和翼龙.仙人掌有大小两种类型,可以同时并列多个:翼龙按高.中.低的随机飞行高度出现,不可并行.仙人掌和地面有着相同的速度向左移动,翼龙则快一些或慢一些,因为添加了随机的速度修正.我们使用一个障碍物列表管理它们,当它们移出屏幕外时则将其从列表中移除.同时再用一个列表记录它们的类型: 1 Obstacle.obstacles = []; //存储障碍物的数组 2 Obs

Chrome自带恐龙小游戏的源码研究(二)

在上一篇<Chrome自带恐龙小游戏的源码研究(一)>中实现了地面的绘制和运动,这一篇主要研究云朵的绘制. 云朵的绘制通过Cloud构造函数完成.Cloud实现代码如下: 1 Cloud.config = { 2 HEIGHT:14, //云朵sprite的高度 3 MAX_CLOUD_GAP:400, //两朵云之间的最大间隙 4 MAX_SKY_LEVEL:30, //云朵的最大高度 5 MIN_CLOUD_GAP:100, //两朵云之间的最小间隙 6 MIN_SKY_LEVEL:71,

cocos2d-x 源码 :可以循环的CCScrollView (代码已经重构过,附使用方法)

1.准备工作 想弄懂可循环的CCscrollView,首先请阅读cocos2d-x本身的CCscrollView源码http://blog.csdn.net/u011225840/article/details/30033501(我已经添加注释,方便阅读). 2.源码展示 因为源码我想放到git上,所以注释都是用的英文,如果这部分源码有人有问题,请在评论区留言,我会逐一回答. 总体说下,CCCycleScrollview继承了CCscrollView以及CCscrollViewDelegate,