第四节 游戏界面的绘制
其实这一节和第一节差不多,主要就是在GameLayer类中添加一些按钮。
注意先include一下上一节的SnakeHead.h
打开GameLayer.h 往里面加入
成员:
Sprite* background;
MenuItemImage* butup;
MenuItemImage* butdown;
MenuItemImage* butleft;
MenuItemImage* butright;
MenuItemImage* pausebutton;
MenuItemImage* playbutton;
Label* gameover;
Size visiblesize;
Menu* menu;
简单解释一下,第一个成员是游戏界面的背景,第二到第四个成员是控制上下左右方向的按钮,第五个和第六个成员是控制游戏暂停和游戏恢复的按钮,第七个成员是用来显示“GameOver!”这句话的,第八个成员是屏幕可视大小,最后一个成员嘛,你懂的。
继续往GameLayer.h里添加成员函数
void SetApperance()
void SetDirection(Ref* psender,Direction direc);
void PauseGame();
void StartGame();
在GameLayer.cpp中实现 SetApperance()函数。代码如下
void GameLayer::SetApperance(){
visiblesize = Director::getInstance()->getVisibleSize();
background = Sprite::create("BackGround.png");
background->setPosition(Point(visiblesize.width/2,visiblesize.height/2));
this->addChild(background,0); //this is the background
butup = MenuItemImage::create("Button.png","Button2.png","Button.png",CC_CALLBACK_1(GameLayer::SetDirection,this,up));
Size bs = butup->getContentSize();
butup->setPosition(Point(visiblesize.width-3*bs.width+bs.width/2,3*bs.height+bs.height/2));
butdown = MenuItemImage::create("Button.png","Button2.png","Button.png",CC_CALLBACK_1(GameLayer::SetDirection,this,down));
butdown ->setPosition(Point(visiblesize.width-3*bs.width+bs.width/2,bs.height+bs.height/2));
butleft = MenuItemImage::create("Button.png","Button2.png","Button.png",CC_CALLBACK_1(GameLayer::SetDirection,this,left));
butleft ->setPosition(Point(visiblesize.width-4*bs.width+bs.width/2,2*bs.height+bs.height/2));
butright = MenuItemImage::create("Button.png","Button2.png","Button.png",CC_CALLBACK_1(GameLayer::SetDirection,this,right));
butright ->setPosition(Point(visiblesize.width-2*bs.width+bs.width/2,2*bs.height+bs.height/2));
pausebutton = MenuItemImage::create("PauseButton.png","PauseButton.png",CC_CALLBACK_0(GameLayer::PauseGame,this));
pausebutton->setPosition(Point(visiblesize.width - pausebutton->getContentSize().width,visiblesize.height - pausebutton->getContentSize().height/2));
playbutton = MenuItemImage::create("rePlayButton1.png","rePlayButton2.png",CC_CALLBACK_0(GameLayer::StartGame, this));
playbutton->setPosition(Point(visiblesize.width/2,visiblesize.height/2));
playbutton->setVisible(false);
menu = Menu::create(butup,butdown,butleft,butright,pausebutton,playbutton,NULL);
menu->setPosition(Point::ZERO);
this->addChild(menu,2);
TTFConfig ttfconfig("Marker Felt.ttf",40);
gameover = Label::createWithTTF(ttfconfig, " Game Over !",TextHAlignment::CENTER);
gameover->setPosition(Point(visiblesize.width/2,visiblesize.height/2+gameover->getContentSize().height));
gameover->setVisible(false);
gameover->setColor(Color3B::BLACK);
this->addChild(gameover,2);
}
```
背景和按钮的设置我就不再提了,第一节里面都讲过了,稍微提一下就是第一节里的CC_CALLBACK_0 变成了CC_CALLBACK_1,因为我们要往回调函数SetDirection里面传一个Direction类型的参数。SetDirection函数先放一放,我们先来看如何创建一个label,首先我们要初始化一个ttfconfig结构体,结构体中有许多的成员,我们暂时只初始化前2个成员,分别是ttf文件的位置和字体的大小。配置完ttfconfig结构体后,我们开始创建label,利用createWithTTF函数来创建,前3个参数分别是ttfconfig结构体,你要输入的字符串,以及文字的对其方式,我们这里设置为居中。然后我们设置label的位置和颜色。
注意,这里我们先要将这个label设置为不可见,你可不想玩游戏的时候屏幕上一直有个 GameOver!吧,同样,恢复游戏的按钮也要设置为不可见。
最后用addChild函数添加成员。
好了现在我们已经完成了SetApperance函数了,在init函数中添加SetApperance函数吧!
接下来,我们开始实现SetDirection这个函数,注意如果用CC_CALLBACK1往回调函数里传参数的话,在定义回调函数的时候,要在函数参数中加入Ref* 的一个成员
代码如下
void GameLayer::SetDirection(Ref* psender,Direction direc){
Direction headdir = head->getDirec();
switch (direc) {
case up:
if(headdir==left||headdir==right){
head->setDirec(up);
}
break;
case down:
if(headdir==left||headdir==right){
head->setDirec(down);
}
break;
case left:
if(headdir==up||headdir==down){
head->setDirec(left);
}
break;
case right:
if(headdir==up||headdir==down){
head->setDirec(right);
}
break;
default:
break;
}
}
```
在这个函数中我们先获取蛇头的方向,然后根据蛇头的方向判断是否可以更改方向,因为当蛇在上下运动的时候,想改变方向的话只能变成左右,同理当蛇在左右运动的时候,只能改成上下运动。简而言之就是不能改成与当前蛇运动相反的方向。
第四节结束