#include "GameScene.h"
#include "GameOver.h"
#include "StartScene.h"
#include <ui/UIImageView.h>
#include "SimpleAudioEngine.h"
#include "cocos2d.h"
USING_NS_CC;
using namespace CocosDenshion;
Scene*GameScene::createScene(){
auto scene = Scene::create();
auto layer = GameScene::create();
scene->addChild(layer);
return scene;
}
bool GameScene::init()
{
if (!Scene::init()){
return false;
}
visibleSize = Director::getInstance()->getVisibleSize();
//游戏背景, Rect(0, 0, visibleSize.width, visibleSize.height)
auto gameBj = ui::ImageView::create("gamebg.png"); //创建背景图片视图
gameBj->setPosition(Vec2(0,0)); //设置位置
gameBj->setAnchorPoint(Vec2(0, 0)); //设置锚点位置
float uiX = gameBj->getContentSize().width; //获取背景图片的宽
float uiY = gameBj->getContentSize().height; //获取背景图片的高
gameBj->setScaleX(480 / uiX); //横轴以480个像素点进行缩放
gameBj->setScaleY(600 / uiY); //纵轴以600个像素点进行缩放
this->addChild(gameBj);
//显示分数标签
ScoreSystem();
//随机方块颜色
srand(time(NULL));
createFix();
this->schedule(schedule_selector(GameScene::boxMove), 0.5f);
//关闭音乐按钮
auto musicOn = MenuItemImage::create("menu_on.png", "menu_on.png");//开
musicOn->setTag(1);
auto musicOff = MenuItemImage::create("menu_off.png", "menu_off.png");//关
musicOff->setTag(2);
auto musicTogg = MenuItemToggle::createWithCallback(CC_CALLBACK_1(
GameScene::menuMusicTogg, this),
musicOn, musicOff, NULL);
musicTogg->setPosition(Vec2(440, 550));
Menu*mn = Menu::create(musicTogg, NULL);
mn->setPosition(Vec2::ZERO);
this->addChild(mn);
//单击事件
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this);
listener->setSwallowTouches(true);//不向下传递触摸
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
/*******************************************************************************************/
/* 音乐开关 */
/*******************************************************************************************/
void GameScene::menuMusicTogg(Ref*pSender){
auto musicTogg = (MenuItemToggle*)pSender;
if (musicTogg->getSelectedIndex() == 1){
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();//暂停播放背景音乐
}
else{
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();//继续播放背景音乐
}
}
/*******************************************************************************************/
/* 触摸事件开始,也就是手指按下时 */
/*******************************************************************************************/
bool GameScene::onTouchBegan(Touch*touch, Event*event){
Point pos = touch->getLocation();//获取单击坐标,基于3D
if (pos.y > 0 && pos.y < 128){
if (pos.x > 0 && pos.x < 120){ //--------------左移--------------//
leftMove();
}
else if (pos.x > 120 && pos.x < 240){//--------------右移--------------//
rightMove();
}
else if (pos.x > 240 && pos.x < 360){//--------------旋转--------------//
fkRotate(shape);
}
else if (pos.x > 360 && pos.x < 480){//--------------下移--------------//
fastDrop(0.01f);
}
}
return true;
}
/*******************************************************************************************/
/* 预览方块实现 */
/*******************************************************************************************/
void GameScene::PreviewBox(int box[4][2]){
int s, zx, zy;
s = 20;
zx = 410;
zy = 460;
//设置方块的颜色
Colors[0] = Color3B(255, 0, 0);//红
Colors[1] = Color3B(0, 255, 0);//绿
Colors[2] = Color3B(0, 0, 255);//蓝
Colors[3] = Color3B(255, 255, 0);//黄
Colors[4] = Color3B(255, 0, 255);//紫
Colors[5] = Color3B(0, 255, 255);//青
auto texture = TextureCache::getInstance()->getTextureForKey("box.png");//缓存图片的纹理
auto frame = SpriteFrame::createWithTexture(texture, Rect(0, 0, s - 1, s - 1));//使用纹理创建精灵帧
for (int i = 0; i < 4; i++){
auto sp = Sprite::createWithSpriteFrame(frame);//通过纹理创建精灵
sp->setPosition(Point(zx + box[i][0] * s, zy + box[i][1] * s));//根据数组设置位置
sp->setColor(Colors[preColor]);//设置方块的颜色
this->addChild(sp);//添加到场景中
sp->setTag(90 + i);//设置标签
}
}
/*******************************************************************************************/
/* 创建下落图案 */
/*******************************************************************************************/
void GameScene::createFix()
{
int ctime[7] = { 0, 1, 1, 1, 3, 3, 3 };//变换次数
auto sframe = SpriteFrame::create("box.png", Rect(0, 0, bSize - 1, bSize - 1));
//下落图案数组
int boxes[7][4][2]{
{ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } }, //"田" 字形方块坐标
{ { 1, 0 }, { 0, 0 }, { 2, 0 }, { 3, 0 } }, // "1" 字形方块坐标
{ { 1, 1 }, { 0, 0 }, { 1, 0 }, { 2, 1 } }, //"闪电"形方块坐标
{ { 1, 1 }, { 0, 1 }, { 1, 0 }, { 2, 0 } }, //"反闪电" 形方块坐标
{ { 1, 0 }, { 0, 0 }, { 2, 0 }, { 1, 1 } }, //"凸" 字形方块坐标
{ { 1, 0 }, { 0, 0 }, { 0, 1 }, { 2, 0 } }, //"反L" 形方块坐标
{ { 1, 0 }, { 0, 0 }, { 2, 0 }, { 2, 1 } } //"L" 形方块坐标
};
//设置方块的颜色
Colors[0] = Color3B(255, 0, 0);//红
Colors[1] = Color3B(0, 255, 0);//绿
Colors[2] = Color3B(0, 0, 255);//蓝
Colors[3] = Color3B(255, 255, 0);//黄
Colors[4] = Color3B(255, 0, 255);//紫
Colors[5] = Color3B(0, 255, 255);//青
//取出颜色
if (preColor == -1){
preColor = rand() % 6;
}
//取出形状
if (preShape == -1){
shape = preShape;
preShape = rand() % 7;
}
//获取形状
for (int i = 0; i < 4; i++){
Sprite*box = Sprite::createWithSpriteFrame(sframe);
box->setColor(Colors[preColor]);//设置颜色
box->setAnchorPoint(Vec2(0, 0));//设置锚点(方块下落终点)
box->setPosition(Vec2(getX(4 + boxes[preShape][i][0]), getY(13 + boxes[preShape][i][1])));//设置位置
this->addChild(box);
box->setTag(110 + i);
MoveList.pushBack(box);//6.保存到正在下落的方块数组内
}
Eliminate();//消除方块
shape = preShape;
preShape = rand() % 7;
preColor = rand() % 6;
PreviewBox(boxes[preShape]);
}
/*******************************************************************************************/
/* 数字转换坐标 */
/*******************************************************************************************/
float GameScene::getX(int x){
return 85 + x * 31;
}
float GameScene::getY(int y){
return 128 + y * 31;
}
/*******************************************************************************************/
/* 坐标转换数字 */
/*******************************************************************************************/
int GameScene::getIx(float x){
return (x - 85) / 31;
}
int GameScene::getIy(float y){
return (y - 128) / 31;
}
/*******************************************************************************************/
/* 获取最高高度 */
/*******************************************************************************************/
int GameScene::Highest(){
int h = 0;
for (int i = 0; i<StopList.size(); i++)
{
Sprite* box = StopList.at(i);
int y = getIy(box->getPositionY());
if (y > h)
h = y;
}
return h;
}
/*******************************************************************************************/
/* 快速下落 */
/**