【cocos2dx 3.2】别碰白块禅模式

时间:2022-04-08 12:10:14

注:

1.没有实现倒计时30秒,而是顺数30秒

2.打包好的apk,不能调用系统时钟,所以计时功能只有在vs上有效

3.apk地址:http://pan.baidu.com/share/link?shareid=3877337685&uk=185595768

【cocos2dx 3.2】别碰白块禅模式


调整屏幕大小:

AppDelegate.cpp:

bool AppDelegate::applicationDidFinishLaunching() {

    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) {
		//修改屏幕大小
        glview = GLView::createWithRect("MyBlock",Rect(0,0,286,512),1.0f); 
        director->setOpenGLView(glview);
    }

	glview->setDesignResolutionSize(268,512,ResolutionPolicy::EXACT_FIT);  

    director->setDisplayStats(true);

    director->setAnimationInterval(1.0 / 60);

    auto scene = HelloWorld::createScene();

    director->runWithScene(scene);

    return true;
}

卡片类:

Card.h:

#include "cocos2d.h"

USING_NS_CC;

class Card : public Sprite
{

public:
	
	static Card* createCard(Color3B bgColor,Size cardSize);
	virtual bool init(Color3B bgColor,Size cardSize);

	void setLineIndex(int index);
	int getLineIndex();

	void removeCard();

	//获取向量里的对象
	static Vector<Card*> *getCard();

	//向下移动一行
	void moveDown();

	//清空向量
	static void renewVector();

private:

	//用向量存放Card
	static Vector<Card*> *cards;

	//记录卡片所在的行
	int lineIndex;

};

Card.cpp:

#include "Card.h"

USING_NS_CC;

Vector<Card*> *Card::cards = new Vector<Card*>();

Card* Card::createCard(Color3B bgColor,Size cardSize)
{
	auto card = new Card();
	card->init(bgColor,cardSize);
	card->autorelease();

	cards->pushBack(card);

	return card;
}


bool Card::init(Color3B bgColor,Size cardSize)
{
	Sprite::init();

	lineIndex = 0;
	setContentSize(cardSize);
	setAnchorPoint(Point::ZERO);
	setTextureRect(Rect(0,0,cardSize.width,cardSize.height));
	setColor(bgColor);

	return true;
}

Vector<Card*> * Card::getCard()
{
	return cards;
}

void Card::setLineIndex(int index)
{
	this->lineIndex = index;
}

int Card::getLineIndex()
{
	return this->lineIndex;
}

void Card::removeCard(){

    removeFromParent();
	cards->eraseObject(this);
}

//向下移动一行
void Card::moveDown()
{
	this->lineIndex--;
	Size visibleSize = Director::getInstance()->getVisibleSize();

	runAction(Sequence::create(MoveTo::create(0.1f, Point(getPositionX(), lineIndex*visibleSize.height/4)),
                               CallFunc::create([this](){
        
        if (lineIndex<0) {
            this->removeCard();
        }
        
    }), NULL));

}

void Card::renewVector()
{
	cards->clear();
}

HelloWorldScene.h:

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "Card.h"

class HelloWorld : public cocos2d::Layer
{
public:
 
    static cocos2d::Scene* createScene();
 
    virtual bool init();  
  
    void menuCloseCallback(cocos2d::Ref* pSender);
    
    CREATE_FUNC(HelloWorld);

	//添加卡片
	void addStartLine();
	void addNormalLine(int lineIndex);

	void startGame();

	//向下移动一行
	void moveDown();
	
	//显示时间
	void update(float dt);

	//清空向量里的卡片
	void removeAllCard();
	
private:
	Size visibleSize;
	Point origin;

	//记录分数
	int score;

	Node *gameLayer;

	//倒计时
	long startTime;
	LabelTTF *label;
};

#endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp:

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
    
    auto layer = HelloWorld::create();

    scene->addChild(layer);

    return scene;
}

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    visibleSize = Director::getInstance()->getVisibleSize();
    origin = Director::getInstance()->getVisibleOrigin();

	//设置随机种子
	srand(time(NULL));
	
	//gameLayer层
	gameLayer = Node::create();
	addChild(gameLayer);

	score = 0;
	startTime = clock();

	startGame();

	//时间label
	label = LabelTTF::create();
	label->setString("0.000");
	label->setFontSize(30);
	label->setColor(Color3B(255,0,0));
	label->setPosition(Point(visibleSize.width/2,visibleSize.height-20));
	addChild(label);

	//开启计时器
	scheduleUpdate();

	//设置事件监听器
	auto listener = EventListenerTouchOneByOne::create();

	listener->onTouchBegan = [this](Touch *t,Event *e)
	{
	
		
		//schedule(schedule_selector(HelloWorld::Go),0.3);  
		//遍历卡片向量
		auto c = Card::getCard();
		Card *it;
		for (auto iterator = c->begin() ; iterator != c->end(); iterator++)
		{
			it = *iterator;
			if(it->getLineIndex()==1 && it->getBoundingBox().containsPoint(t->getLocation()))
			{
				//若是黑块
				if(it->getColor()==Color3B::BLACK)
				{
					 
                    it->setColor(Color3B::GRAY);
					score++;
					this->moveDown();
				}
				else
				{
					unscheduleUpdate();
					this->removeAllCard();
					Director::getInstance()->replaceScene(TransitionFade::create(1,HelloWorld::createScene()));
				}
				break;
			}
		}

		return false;
	};

	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,this);

    return true;
}

void HelloWorld::startGame()
{
	addStartLine();
	addNormalLine(1);
	addNormalLine(2);
	addNormalLine(3);
}

void HelloWorld::addNormalLine(int lineIndex)
{
	Card *card;
	
	int blackIndex = rand()%4;

	for (int i = 0; i < 4; i++)
	{
		card = Card::createCard(blackIndex==i?Color3B::BLACK:Color3B::WHITE,Size(visibleSize.width/4-1,visibleSize.height/4-1));
		card->setPosition(Point(i*visibleSize.width/4,lineIndex*visibleSize.height/4));
		card->setLineIndex(lineIndex);
		gameLayer->addChild(card);
	}
}

void HelloWorld::addStartLine()
{
	Card *c;
	for (int i = 0; i < 4; i++)
	{
		c = Card::createCard(Color3B::YELLOW,Size(visibleSize.width/4-1,visibleSize.height/4-1));
		c->setPosition(Point(i*visibleSize.width/4,0));
		c->setLineIndex(0);
		gameLayer->addChild(c);
	}

}

void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif

    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

void HelloWorld::moveDown()
{
	addNormalLine(4);

	auto bs = Card::getCard();

    for (auto i = bs->begin(); i!=bs->end(); i++) {
        (*i)->moveDown();
    }

}

void HelloWorld::update(float dt)
{
		long offset = clock()-startTime;
		if((int)offset/1000 > 29)
		{
			label->setString(StringUtils::format("Score:%d",score));
			unscheduleUpdate();	
		}
		else
		{
			label->setString(StringUtils::format("%g",((double)offset)/1000));
		}
}

void HelloWorld::removeAllCard()
{
	Card::renewVector();
}