这个帖子是笔记,有想用C或C++语言开发游戏的可以加这个
掌柜的创的Cocos2d-x 学习群:153412944
游戏版本:0.0.1. 20160612 base
C++代码:
//HelloWorld.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "Block.h"
USING_NS_CC;
class HelloWorld : public cocos2d:ayer
{
private:
Size visibleSize;
int linesCount;
bool showEnd;
Label * timerLabel;
Node * gameLayer;
bool timerRunning;
long startTime;
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
void addStartLine();
void addEndLine();
void addNormalLine( int lineIndex );
void moveDown();
void startTimer();
void stopTimer();
void startGame();
virtual void update( float dt );
};
#endif // __HELLOWORLD_SCENE_H__
//HelloWorld.cpp
#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "Block.h"
USING_NS_CC;
using namespace cocostudio::timeline;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
// 1. super init first
if ( !Layer::init() )
{
return false;
}
srand( time( NULL ));
visibleSize = Director::getInstance()->getVisibleSize();
gameLayer = Node::create();
addChild( gameLayer );
timerLabel = Label::create();
timerLabel->setTextColor( Color4B::BLUE );
timerLabel->setSystemFontSize( 48 );
timerLabel->setPosition( visibleSize.width / 2, visibleSize.height -100 );
timerLabel->setString( "0.000");
addChild( timerLabel );
startGame();
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan=[ this ]( Touch * t, Event * e )
{
log( "onTou" );
auto bs = Block::getBlocks();
Block *b;
for( auto it = bs->begin(); it != bs->end(); it++)
{
b = * it;
if ( b->getLineIndex() == 1 && b->getBoundingBox().containsPoint( t->getLocation()))
{
if ( b->getColor() == Color3B::BLACK )
{
if( !timerRunning )
{
this->startTimer();
}
b->setColor( Color3B::GRAY );
this->moveDown();
}
else if( b->getColor() == Color3B::GREEN)
{
this->moveDown();
this->stopTimer();
}
else
{
MessageBox( "GameOver", "失败");
}
break;
}
}
return false;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority( listener, this);
return true;
}
void HelloWorld::startGame()
{
linesCount = 0;
showEnd = false;
timerRunning = false;
addStartLine();
addNormalLine( 1 );
addNormalLine( 2 );
addNormalLine( 3 );
}
void HelloWorld::addStartLine()
{
auto b = Block::createWithArgs( Color3B::YELLOW, Size( visibleSize.width, visibleSize.height / 4 ), "", 20, Color4B::BLACK );
gameLayer->addChild( b );
b->setLineIndex( 0 );
}
void HelloWorld::addEndLine()
{
auto b = Block::createWithArgs( Color3B::GREEN, visibleSize, "Game Over", 48, Color4B::BLACK );
gameLayer->addChild( b );
b->setLineIndex( 4 );
}
void HelloWorld::addNormalLine( int lineIndex )
{
Block *b;
int blackIndex = rand()%4;
for( int i = 0; i < 4; i++ )
{
b = Block::createWithArgs( blackIndex == i ? Color3B::BLACK : Color3B::WHITE, Size( visibleSize.width / 4 - 1, visibleSize.height / 4 - 1 ), "", 20, Color4B::BLACK );
gameLayer->addChild( b );
b->setPosition(i * visibleSize.width / 4, lineIndex * visibleSize.height / 4 );
b->setLineIndex( lineIndex );
}
linesCount++;
}
void HelloWorld::moveDown()
{
if ( linesCount < 50 )
{
addNormalLine( 4 );
}
else if( !showEnd )
{
addEndLine();
showEnd = true;
}
auto bs = Block::getBlocks();
for ( auto it = bs->begin(); it != bs->end(); it++ )
{
(*it)->moveDown();
}
}
void HelloWorld::update( float dt )
{
long offset = clock() - startTime;
timerLabel->setString(StringUtils::format( "%g", ((double)offset)/ 1000));
}
void HelloWorld::startTimer()
{
if( !timerRunning )
{
scheduleUpdate();
startTime = clock();
timerRunning = true;
}
}
void HelloWorld::stopTimer()
{
if( timerRunning )
{
unscheduleUpdate();
timerRunning = false;
}
}
//Block.h
#ifndef __BLOCK_H__
#define __BLOCK_H__
#include "cocos2d.h"
USING_NS_CC;
class Block: public Sprite
{
private:
static Vector<Block*> *blocks;
int lineIndex;
public:
static Vector<Block*> * getBlocks();
static Block * createWithArgs( Color3B color, Size size, std::string label, float fontSize, Color4B textColor );
virtual bool initWithArgs( Color3B color, Size size, std::string label, float fontSize, Color4B textColor );
void removeBlock();
int getLineIndex();
void setLineIndex( int lineIndex );
void moveDown();
};
#endif
//Block.cpp
#include "Block.h"
Vector<Block*> * Block::blocks = new Vector<Block*>();
Vector<Block*> * Block::getBlocks()
{
return Block::blocks;
}
Block * Block::createWithArgs( Color3B color, Size size, std::string label, float fontSize, Color4B textColor )
{
auto b = new Block();
b->initWithArgs( color, size, label, fontSize, textColor );
b->autorelease();
blocks->pushBack( b );
return b;
}
bool Block::initWithArgs( Color3B color, Size size, std::string label, float fontSize, Color4B textColor )
{
Sprite::init();
lineIndex = 0;
setContentSize( size );
setAnchorPoint( Point::ZERO );
setTextureRect( Rect( 0, 0, size.width, size.height ));
setColor( color );
auto l = Label::create();
l->setString( label );
l->setSystemFontSize( fontSize );
l->setTextColor( textColor );
l->setPosition( size.width / 2, size.height / 2 );
addChild ( l );
return true;
}
void Block::removeBlock()
{
auto c = getColor();
log( "Remove block,color is (%d %d %d)", c.r, c.g, c.b);
removeFromParent();
blocks->eraseObject( this );
}
void Block::setLineIndex( int v )
{
this->lineIndex = v;
}
int Block::getLineIndex()
{
return this->lineIndex;
}
void Block::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->removeBlock();
}
}) ,NULL));
}
效果图:
|