//
// GameScene.cpp
// 2048
//
// Created by 宋橙 on 14-6-16.
//
//
#include "GameScene.h"
#include "Station.h"
#include "Box.h"
#include "MyTools.h"
#include "OverScene.h"
#include "HomeScene.h"
USING_NS_CC;
#define userDefault UserDefault::getInstance()
#define DURATION 0.1f
#define WIN_NUMBER 2048
Scene* Game::createScene()
{
auto scene = Scene::create();
auto layer = Game::create();
scene->addChild(layer);
return scene;
}
bool Game::init()
{
if (!Layer::init()) {
return false;
}
//随机数种子
srand((int)time(NULL));
//初始化各项数据
initData();
return true;
}
//清除上次游戏数据,用于重新开始
void Game::clearData()
{
userDefault->setIntegerForKey("lastScore", 0);
for (int line = 0; line < 4; line++) {
for (int column = 0; column < 4; column++) {
auto key = String::createWithFormat("tile_%i%i", line, column)->getCString();
userDefault->setIntegerForKey(key, 0);
}
}
userDefault->flush();
}
#pragma mark - 初始化
//初始化各项数据
void Game::initData()
{
number = 0;
firstMove = true;
isWin = false;
//加载背景
loadBackground();
//显示得分
showScore();
//加载菜单
loadMenu();
//加载站点
stations = Map<std::string, Node *>(16);
loadStations();
//添加数盒
boxes = Vector<Node *>(16);
loadBox();
}
//加载背景
void Game::loadBackground()
{
auto background = Sprite::createWithSpriteFrameName("bg_blank.png");
background->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT);
addChild(background);
auto icon = Sprite::createWithSpriteFrameName("bg_icon.png");
icon->setPosition(130, 825);
addChild(icon);
auto scoreLast = Sprite::createWithSpriteFrameName("bg_score_last.png");
scoreLast->setPosition(340, 865);
addChild(scoreLast);
auto scoreMax = Sprite::createWithSpriteFrameName("bg_score_max.png");
scoreMax->setPosition(530, 865);
addChild(scoreMax);
auto table = Sprite::createWithSpriteFrameName("bg_table.png");
table->setPosition(320, 410);
addChild(table);
}
//加载菜单
void Game::loadMenu()
{
//菜单按钮
auto menuSprite = Sprite::createWithSpriteFrameName("btn_menu.png");
auto menuSpriteItem = MenuItemSprite::create(menuSprite, menuSprite, CC_CALLBACK_1(Game::menuCallback, this));
menuSpriteItem->setPosition(Point(20, 285));
menuSpriteItem->setTag(GameMenuTag);
//排行榜按钮
auto billboradSprite = Sprite::createWithSpriteFrameName("btn_billboard.png");
auto billboradSpriteItem = MenuItemSprite::create(billboradSprite, billboradSprite, CC_CALLBACK_1(Game::menuCallback, this));
billboradSpriteItem->setPosition(Point(210, 285));
billboradSpriteItem->setTag(GameBillboradTag);
auto menu = Menu::create(menuSpriteItem, billboradSpriteItem, NULL);
this->addChild(menu);
}
//获胜加载
void Game::loadWin()
{
_eventDispatcher->removeAllEventListeners();
auto mask = Sprite::createWithSpriteFrameName("bg_mask.png");
mask->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT);
addChild(mask, 2, 100);
auto win = Sprite::createWithSpriteFrameName("bg_win.png");
win->setScale(1.2f);
win->setPosition(320, 410);
addChild(win, 2, 101);
//再试一次按钮
auto restartSprite = Sprite::createWithSpriteFrameName("btn_restart_win.png");
restartSprite->setScale(1.2f);
auto restareSpriteItem = MenuItemSprite::create(restartSprite, restartSprite, CC_CALLBACK_1(Game::menuCallback, this));
restareSpriteItem->setPosition(Point(-80, -200));
restareSpriteItem->setTag(GameRestartTag);
//分享按钮
auto shareSprite = Sprite::createWithSpriteFrameName("btn_share_win.png");
shareSprite->setScale(1.2f);
auto shareSpriteItem = MenuItemSprite::create(shareSprite, shareSprite, CC_CALLBACK_1(Game::menuCallback, this));
shareSpriteItem->setPosition(Point(80, -200));
shareSpriteItem->setTag(GameShareTag);
auto menu = Menu::create(restareSpriteItem, shareSpriteItem, NULL);
this->addChild(menu, 2, 102);
}
#pragma mark - 得分
//显示得分
void Game::showScore()
{
//最近得分
lastScore = userDefault->getIntegerForKey("lastScore");
lastScoreLabel = Label::createWithSystemFont(String::createWithFormat("%d", lastScore)->getCString(), "HelveticaNeue-Bold", 50);
lastScoreLabel->setPosition(Point(340, 850));
lastScoreLabel->setColor(Color3B(255, 255, 255));
addChild(lastScoreLabel);
//最大得分
maxScore = userDefault->getIntegerForKey("maxScore", lastScore);
auto maxScoreLabel = Label::createWithSystemFont(String::createWithFormat("%d", maxScore)->getCString(), "HelveticaNeue-Bold", 50);
maxScoreLabel->setPosition(Point(530, 850));
maxScoreLabel->setColor(Color3B(255, 255, 255));
addChild(maxScoreLabel);
}
#pragma mark - 站点
//加载站点
void Game::loadStations()
{
auto stationPoints = FileUtils::getInstance()->getValueVectorFromFile("stationPoints.plist");
for (int i = 0; i < stationPoints.size(); i++) {
auto columns = stationPoints[i].asValueVector();
for (int j = 0; j < columns.size(); j++) {
auto point = columns[j].asValueMap();
auto key = String::createWithFormat("%i%i", i, j)->getCString();
auto position = Point(point["x"].asFloat(), point["y"].asFloat());
stations.insert(key, Station::stationWithPosition(position, i, j));
}
}
}
//获取空站点
Vector<Node *> Game::getEmptyStations()
{
auto emptyStations = Vector<Node *>(16);
for (auto iter = stations.begin(); iter != stations.end(); iter++) {
auto station = (Station *)(iter->second);
if (station->box == NULL){
emptyStations.pushBack(station);
}
}
return emptyStations;
}
//通过行列号获取站点
Station * Game::getStation(int line, int column)
{
auto key = String::createWithFormat("%i%i", line, column)->getCString();
auto station = (Station *)stations.at(key);
return station;
}
//获取下一个站点
Station * Game::getNextStation(Station *station, WhereGo whereGo)
{
auto line = station->line;
auto column = station->column;
switch (whereGo) {
case GO_UP:
line -= 1;
if (line < 0) return NULL;
break;
case GO_DOWN:
line += 1;
if (line > 3) return NULL;
break;
case GO_LEFT:
column -= 1;
if (column < 0) return NULL;
break;
case GO_RIGHT:
column += 1;
if (column > 3) return NULL;
break;
default:
return NULL;
break;
}
return getStation(line, column);
}
#pragma mark - 数盒
//随机添加一个数盒
void Game::addBox()
{
//获取空站点
auto emptyStations = getEmptyStations();
if (emptyStations.size() == 0) {
gameOver();
return;
}
//随机选择一个空站点
int index = rand() % emptyStations.size();
auto station = (Station *)emptyStations.at(index);
//随机一个数盒
int number = pow(2, rand()%2 + 1);
auto box = Box::boxWithStation(station, number);
station->box = box;
boxes.pushBack(box);
box->setScale(0.0f);
addChild(box);
box->runAction(ScaleTo::create(DURATION, 1.0f));
//记录数据
recordData();
//当就剩一个可用站点时添加数盒后的判断
if (!isWin && emptyStations.size() == 1) {
gameOver();
}
}
//加载记录的数盒
void Game::loadBox()
{
int count = 0;
//继续
for (auto iter = stations.begin(); iter != stations.end(); iter++) {
auto station = (Station *)(iter->second);
auto key = String::createWithFormat("tile_%i%
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
学习cocos2d-x 3.0自己写的游戏2048,自己的算法,除了网络部分,其他功能全部实现。 素材是用手机截屏ps出来的。 自己还没有玩赢过,所以按照从网上找的一个获胜界面做出来的。 修改GameScene.cpp第20行中WIN_NUMBER的数值你可以不用玩到2048就能赢。 源码已上传,欢迎大家下载源码进行测试交流。
资源推荐
资源详情
资源评论
收起资源包目录
2048_20140623_1.zip (24个子文件)
Resources
images.png 309KB
move.wav 12KB
merge.wav 35KB
images.plist 18KB
stationPoints.plist 2KB
.DS_Store 6KB
Classes
AppDelegate.h 947B
Scenes
HomeScene.h 732B
GameScene.h 2KB
InstructorScene.h 752B
HomeScene.cpp 5KB
OverScene.cpp 3KB
GameScene.cpp 17KB
InstructorScene.cpp 4KB
.DS_Store 6KB
OverScene.h 609B
AppDelegate.cpp 2KB
Nodes
Station.cpp 591B
Box.h 464B
Station.h 518B
Box.cpp 677B
Tools
MyTools.cpp 707B
MyTools.h 364B
.DS_Store 6KB
共 24 条
- 1
资源评论
- Mike_Xu_2014-07-22很不错 很收益
- SHIYULF2014-10-15下载下来学习,借鉴下还行。
- lyre_ll2014-08-29学习了,很不错,简单
song5198038_1
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功