Cocos2d-x游戏实战项目开发:猜数字游戏

本文介绍了一个使用Cocos2d-x框架开发的简易猜数字游戏项目,包括UI设计、随机数生成、用户输入处理及游戏逻辑实现等关键环节。

此次项目开发跟以往一样,总共分四个模块:
1.UI设计
2.生成随机数
3.用户交互
4.游戏判断

UI设计:
通过代码设计出一定的界面出来,我在代码中用了一个方法实现,即是加了一个bulidUI()函数进来,代码如下,

void HelloWorld::bulidUI()
{
    label = Label::create("please input a number random 0 -100 ", "Courier", 10);
    label->setPosition(visibleSize.width / 2, visibleSize.height - label->getContentSize().height);
    addChild(label);

    textTTF = TextFieldTTF::textFieldWithPlaceHolder("INPUT YOUR NAME","courier",25);
    textTTF->setPosition(label->getPositionX(), label->getPositionY() - 50);
    addChild(textTTF);

    klick = Label::create();
    klick->setString("submit");
    klick->setSystemFontSize(25);
    klick->setPosition(label->getPositionX(), textTTF->getPositionY() - 50);
    addChild(klick);

    message = Label::create();
    message->setSystemFontSize(25);
    message->setPosition(klick->getPositionX(),klick->getPositionY()-50);
    addChild(message);

}

这里写图片描述
生成随机数
代码非常简单,两行而已:

 srand(time(NULL));
     theronNUM = rand()%100;

用户交互和游戏判断
我把用户交互和游戏判断放在了一个方法里,即listener();

void HelloWorld::addListener()
{
    auto director = Director::getInstance();
    auto handle = [this](Touch *t, Event *e)
    {
        auto target = e->getCurrentTarget();
        auto point = t->getLocation();
        if (target->getBoundingBox().containsPoint(point))
        {
            if (target == textTTF)
            {
                textTTF->attachWithIME();
            }
            if (target == klick)//游戏判断
            {
                textTTF->detachWithIME();
                auto inputValue = __String::create(textTTF->getString())->intValue();
                if (inputValue > theronNUM)
                {
                    message->setString("your number is bigger");
                }
                else if (inputValue < theronNUM)
                {
                    message->setString("your number is smaller");
                }
                else
                {
                    message->setString("your number is right");
                }
            }
        }
        return false;
    };
    auto listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = handle;
    director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,textTTF);

    auto submitBtnklick = EventListenerTouchOneByOne::create();
    submitBtnklick->onTouchBegan = handle;
    director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(submitBtnklick, klick);

}

这里写图片描述

这里写图片描述

总的代码如下:
HelloWorldScene.h文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();

    virtual bool init();
    void bulidUI();
    // a selector callback
    void menuCloseCallback(cocos2d::Ref* pSender);
    void addListener();

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
private:
    cocos2d::Size visibleSize;
    cocos2d::Label *label, *klick,*message;
    cocos2d::TextFieldTTF *textTTF;
    int theronNUM;
};

#endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp文件:

#include "HelloWorldScene.h"
# include "ImageScene.h"

USING_NS_CC;

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;
    }

     visibleSize = Director::getInstance()->getVisibleSize();
     srand(time(NULL));
     theronNUM = rand()%100;
     bulidUI();
     addListener();

    return true;

}; 


void HelloWorld::bulidUI()
{
    label = Label::create("please input a number random 0 -100 ", "Courier", 10);
    label->setPosition(visibleSize.width / 2, visibleSize.height - label->getContentSize().height);
    addChild(label);

    textTTF = TextFieldTTF::textFieldWithPlaceHolder("INPUT YOUR NAME","courier",25);
    textTTF->setPosition(label->getPositionX(), label->getPositionY() - 50);
    addChild(textTTF);

    klick = Label::create();
    klick->setString("submit");
    klick->setSystemFontSize(25);
    klick->setPosition(label->getPositionX(), textTTF->getPositionY() - 50);
    addChild(klick);

    message = Label::create();
    message->setSystemFontSize(25);
    message->setPosition(klick->getPositionX(),klick->getPositionY()-50);
    addChild(message);

}

void HelloWorld::addListener()
{
    auto director = Director::getInstance();
    auto handle = [this](Touch *t, Event *e)
    {
        auto target = e->getCurrentTarget();
        auto point = t->getLocation();
        if (target->getBoundingBox().containsPoint(point))
        {
            if (target == textTTF)
            {
                textTTF->attachWithIME();
            }
            if (target == klick)//游戏判断
            {
                textTTF->detachWithIME();
                auto inputValue = __String::create(textTTF->getString())->intValue();
                if (inputValue > theronNUM)
                {
                    message->setString("your number is bigger");
                }
                else if (inputValue < theronNUM)
                {
                    message->setString("your number is smaller");
                }
                else
                {
                    message->setString("your number is right");
                }
            }
        }
        return false;
    };
    auto listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = handle;
    director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,textTTF);

    auto submitBtnklick = EventListenerTouchOneByOne::create();
    submitBtnklick->onTouchBegan = handle;
    director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(submitBtnklick, klick);

}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值