码迷,mamicode.com
首页 > 其他好文 > 详细

翻白块代码

时间:2016-05-12 19:51:55      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

在AppDelegate.cpp文件中

pEGLView->setDesignResolutionSize(800, 480, kResolutionExactFit);

说明本次设计屏幕大小,以及设置自适应屏幕大小

LayerStart.cpp主要是添加游戏开始界面,以及点击菜单后能跳到相应的scene中

#include "LayerStart.h"
#include "LayerGame.h"
CCScene * LayerStart::ccScene()
{
    CCScene * ccsence = CCScene::create();
    LayerStart * layerstart = LayerStart::create();
    ccsence->addChild(layerstart);
    return ccsence;
}
bool LayerStart::init()
{
    CCLayer::init();
    CCMenuItem* easyItem = CCMenuItemFont::create("easy", this,
        menu_selector(LayerStart::easy));
    CCMenuItem * generalItem = CCMenuItemFont::create("general", this,
        menu_selector(LayerStart::general));
    CCMenuItem * hardItem = CCMenuItemFont::create("hard", this, 
        menu_selector(LayerStart::hard));
    CCMenuItem * quitItem = CCMenuItemFont::create("quit", this, 
        menu_selector(LayerStart::quit));
    CCMenu * ccmenu = CCMenu::create(easyItem, generalItem, hardItem, quitItem, NULL);
    //ccmenu->alignItemsHorizontallyWithPadding(40);
    ccmenu->alignItemsVerticallyWithPadding(20);
    addChild(ccmenu);

    return true;
}
void LayerStart::easy(CCObject * obj)
{
    CCScene * cc = LayerGame::scene(EASY);
    CCDirector::sharedDirector()->replaceScene(cc);
}
void LayerStart::general(CCObject * obj)
{
    CCScene * cc = LayerGame::scene(GENERAL);
    CCDirector::sharedDirector()->replaceScene(cc);
}
void LayerStart::hard(CCObject * obj)
{
    CCScene * cc = LayerGame::scene(HARD);
    CCDirector::sharedDirector()->replaceScene(cc);
}
void LayerStart::quit(CCObject * obj)
{
    exit(1);
}

cardItem主要是绘制card单元

#include "CardItem.h"

CardItem * CardItem::create(int dex)
{
    CardItem * cardItem = new CardItem();
    if (cardItem && cardItem->init(dex))
    {
        cardItem->autorelease();
    }
    else
    {
        delete cardItem;
        cardItem = NULL;
    }
    return cardItem;

}
bool CardItem::init(int dex)
{
    CCSprite::init();

    setdex(dex);//把白块的代码存下来

    setContentSize(CCSizeMake(80, 80));//精灵大小为80.80
    setAnchorPoint(ccp(0, 0));

    CCString * ccstring = CCString::createWithFormat("%d", dex);//背景的数字
    ttf = CCLabelTTF::create(ccstring->getCString(), "Courier New", 20);
    ttf->setAnchorPoint(ccp(0,0));//设置锚点为0,0
    ttf->setPosition(ccp(40, 40));
    addChild(ttf);

    bg = CCSprite::create();
    bg->setTextureRect(CCRectMake(0, 0, 75, 75));//子精灵大小为75*75为了避免相邻的精灵连在一起
    bg->setAnchorPoint(ccp(0, 0));//设置锚点为0,0
    bg->setColor(ccc3(255, 0, 0));

    addChild(bg);

    return true;

}
void CardItem::showwhite()//设置白块可见,数字不可见
{
    ttf->setVisible(false);
    bg->setVisible(true);
}
void CardItem::showText()//白块不可见
{
    ttf->setVisible(true);
    bg->setVisible(false);
}
#include "LayerGame.h"
#include "CardItem.h"
#include "time.h"
#include "stdlib.h"
#include "LayerOver.h"
#include "SimpleAudioEngine.h"//包含这个头文件是因为后面要使用播放音乐,音乐的默认格式为wav
using namespace CocosDenshion; //同上
LayerGame *LayerGame::create(int degree)
{
    LayerGame* pRet = new LayerGame();
    if (pRet&&pRet->init(degree))
    {
        pRet->autorelease();
    }
    else
    {
        delete pRet;
        pRet = NULL;
    }
    return pRet;

}

bool LayerGame::init(int degree)//degree等级
{
    CCLayer::init();
    setdegree(degree);
    settouchPoint(0);
    //CardItem * cardItem = CardItem::create(3);
    //cardItem->setPosition(ccp(10, 10));
    //addChild(cardItem);
    getAllPoint();   //获得一共所有能够绘制小方块的个数,并且把点保存下来
    getAvailablePoint();   //获得随机方块的位置
    addCardItem();       //绘制方块
    setTouchEnabled(true);
    setTouchMode(kCCTouchesOneByOne);//触摸打开

    return true;
}

bool LayerGame::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)//点击处理事件
{
    for (int i = 0; i < allcards->count();i++)
    {
        if (((CardItem*)allcards->objectAtIndex(i))->boundingBox()
            .containsPoint(pTouch->getLocation()))
        {
            showAllBg();
            if (((CardItem*)allcards->objectAtIndex(i)) ->getdex()==gettouchPoint())
            {
                SimpleAudioEngine::sharedEngine()->playEffect("click.wav");
                ((CardItem*)allcards->objectAtIndex(i))->removeFromParentAndCleanup(true);
                allcards->removeObjectAtIndex(i);
                if (allcards->count()==0)
                {
                    CCScene * ccscene = LayerOver::ccscene(true);
                    CCDirector::sharedDirector()->replaceScene(ccscene);
                    CCLOG("VETORY");
                }
            }
            else
            {
                CCLog("no");
                CCScene * ccscene = LayerOver::ccscene(false);
                CCDirector::sharedDirector()->replaceScene(ccscene);
            }
            _touchPoint++;
        }
    }


    return true;
}

void LayerGame::showAllBg()
{
    CCObject *obj;
    CCARRAY_FOREACH(allcards, obj)
    {
        ((CardItem *)obj)->showwhite();

    }

}

void LayerGame::addCardItem()//根据等级添加item
{
    allcards = CCArray::create();
    allcards->retain();//内存托管
    for (int i = 0; i < getdegree();i++)
    {
        CardItem * card = CardItem::create(i);
        card->setPosition(allPoint->getControlPointAtIndex(vec[i]));
        addChild(card);
        card->showText();
        allcards->addObject(card);
    }
}

CCScene * LayerGame::scene(int degree)
{
    CCScene * ccscene = CCScene::create();
    LayerGame * laygame = LayerGame::create(degree);
    ccscene->addChild(laygame);
    return ccscene;
}
void LayerGame::getAllPoint()//得到能够绘制方格的点的坐标
{
    allPoint = CCPointArray::create(60);
    allPoint->retain();//内存托管,防止泄漏
    for (int i = 0; i < 6;++i)
    {
        for (int j = 0; j < 10;++j)
        {
            allPoint->addControlPoint(ccp(j * 80, i* 80));//方格大小
        }
    }
}

//产生degree个方格的随机位置
void  LayerGame::getAvailablePoint()
{
    srand(time(NULL));
    while (1)
    {
        int randNumber = rand() % 60;
        vector<int>::iterator ite = vec.begin();//遍历vector
        for (; ite != vec.end();++ite)
        {
            if (randNumber == *ite)
            {
                break;
            }
        }
        if (ite == vec.end())
        {
            vec.push_back(randNumber);
            if (getdegree()==vec.size())
            {
                break;
            }
        }
    }

}

翻白块代码

标签:

原文地址:http://blog.csdn.net/u012920673/article/details/51352269

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!