搜索
查看: 1634|回复: 9
打印 上一主题 下一主题

【cocos2d-x】别踩白块

[复制链接]
跳转到指定楼层
楼主
发表于 2016-6-12 05:49:35 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
这个帖子是笔记,有想用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));
}

效果图:






QQ截图20160612054458.png (104.81 KB, 下载次数: 5)

QQ截图20160612054458.png
推荐
发表于 2016-8-5 21:09:18 | 只看该作者
yanph2003 发表于 2016-8-5 12:36
竟然是标准WIN32...
没用EasyX。
厉害。

错错错,使用的是 cocos2d-x 游戏引擎
沙发
发表于 2016-7-5 11:17:10 | 只看该作者
楼主,你用的是什么C++编译器?给我一个下载链接好吗。

点评

http://www.ahalei.com/thread-10055-1-1.html  发表于 2018-2-1 21:18
板凳
发表于 2016-7-20 16:11:00 | 只看该作者
对呀!!!

点评

http://www.ahalei.com/thread-10055-1-1.html  发表于 2018-2-1 21:19
对对对  发表于 2016-9-11 14:38
adc
对什么呀  发表于 2016-7-23 19:06
地板
发表于 2016-8-4 22:36:16 | 只看该作者
羡慕、嫉妒、恨~~
5#
发表于 2016-8-5 12:36:54 | 只看该作者
竟然是标准WIN32...
没用EasyX。
厉害。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表