注:轉(zhuǎn)載請(qǐng)注明出處:http://blog.csdn.net/u011669081/article/details/52368251
前言在收到騰訊面試邀請(qǐng)的時(shí)候颂跨,作為應(yīng)屆生還有點(diǎn)小激動(dòng),面試的崗位居然是我未接觸的游戲開發(fā)给僵,額毫捣,四面之后详拙,就出了個(gè)題目,一周之內(nèi)自學(xué)flash并且開發(fā)天天愛消除蔓同。然后就硬著頭皮干了饶辙。
設(shè)計(jì)思路
天天愛消除的游戲方式其實(shí)很簡(jiǎn)單,首先游戲剛進(jìn)入是必須生成一個(gè)隨機(jī)的環(huán)境(我們這里可以把它看成7*7的數(shù)組)斑粱,用戶操作相鄰元素的替換弃揽,當(dāng)3個(gè)以上相同元素在同行同列情況下,要消除则北,然后上行下移矿微,重復(fù)這些邏輯,直到時(shí)間耗盡尚揣,游戲結(jié)束涌矢。手機(jī)版如下截圖:
實(shí)現(xiàn)源碼思路想清除后,就是將每一步思路寫成代碼快骗。(這里是三天寫完的Demo代碼娜庇,有很多可以進(jìn)一步優(yōu)化的地方,有低效代碼的存在方篮,勿噴~)先來看隨機(jī)生成元素:
/*
第一次生成的數(shù)組應(yīng)當(dāng)是:同行同列的情況是幾乎不可以存在的
/
private function createGroup():void{
for(var i:uint = 0;i<7;i++){
for(var j:uint = 0;j<7;j++){
if(i == 0 && j < 2){//前兩位 幾乎不需要判斷
var n:uint = Math.floor(Math.random()4);
arr[i][j] = n;
}else{
var t:uint = Math.floor(Math.random()4);
var fg:Boolean = checkGroup(t,i,j);
//這里有待優(yōu)化名秀,可能存在死循環(huán)
while(fg){
var m:uint = Math.floor(Math.random()4);
fg = checkGroup(m,i,j);
}
}
}
}
}
注意:checkGroup(m,i,j);是檢測(cè)是否有可消除元素。這里確實(shí)存在風(fēng)險(xiǎn)藕溅,有待優(yōu)化匕得。然后再是控制元素:
private function sunClicked(e:MouseEvent):void {
if(!gameState){//游戲已經(jīng)結(jié)束
statusTextField.text="游戲已經(jīng)結(jié)束了~~~~~~";
return;
}
lastClickTime = repeat;
nowX = e.stageX/45;
nowY = e.stageY/45;
var sound:Sound = new Shoot();
sound.play();
if(oldX != -1 && oldY != -1){//不是第一次點(diǎn)擊,但是需要做個(gè)判斷:是否兩個(gè)點(diǎn)擊是相鄰的
if(
((nowX == oldX && Math.abs(nowY - oldY)<= 1 )
|| ((Math.abs(nowX-oldX)<=1 && nowY == oldY))
)){
//trace(" 符合");
}else{
//選擇不合規(guī)范
chooseC = 0;//重置選擇
}
}
if(chooseC == 0){
chooseC = 1;
oldX = nowX;
oldY = nowY;
if(e.currentTarget.toString().search("yellow")!=-1){
var yel:yellow=e.currentTarget as yellow;
TransitionManager.start(yel, {type:Zoom, direction:0, duration:1, easing: Bounce.easeOut});
}else if(e.currentTarget.toString().search("red")!=-1){
var re:red=e.currentTarget as red;
TransitionManager.start(re, {type:Zoom, direction:0, duration:1, easing: Bounce.easeOut});
}else if(e.currentTarget.toString().search("blue")!=-1){
var bl:blue=e.currentTarget as blue;
TransitionManager.start(bl, {type:Zoom, direction:0, duration:1, easing: Bounce.easeOut});
}else if(e.currentTarget.toString().search("blank")!=-1){
var bla:blank=e.currentTarget as blank;
TransitionManager.start(bla, {type:Zoom, direction:0, duration:1, easing: Bounce.easeOut});
}
//trace("第一個(gè)");
}else if(chooseC ==1){
chooseC = 0;
changeView(nowX,nowY,oldX,oldY);
cleanItem();
}
}
//交換元素
private function changeView(currX:int,currY:int,lastX:int,lastY:int):void{
var temp:int = arr[currX][currY];
arr[currX][currY] = arr[lastX][lastY];
arr[lastX][lastY] = temp;
}
其中sunClicked是對(duì)點(diǎn)擊元素的監(jiān)聽巾表,changeView是將7*7的數(shù)組中元素交換汁掠。移除可消除的元素:
//移除掉 可以消除的元素
private function cleanItem():void{
var fg:Boolean = false;
var now:int = -1;
//這里將數(shù)組再?gòu)?fù)制一份,因?yàn)橄旅娴呐袛鄡?nèi)有修改數(shù)組的操作攒发,如果第一次修改了數(shù)組调塌,意味著第二次的比較會(huì)受影響晋南,所以復(fù)制數(shù)組
for(var a:int = 0;a<7;a++){
for(var b:int = 0;b<7;b++){
tempArray[a][b] = arr[a][b];
}
}
for(var i:uint =0;i<7;i++){
for(var j:uint =0;j<7;j++){
now = arr[i][j];
if(i<=2){//判斷5個(gè)元素
if(now == arr[i+1][j] && now == arr[i+2][j] && now == arr[i+3][j] && now == arr[i+4][j] && now !=4){
tempArray[i][j] =4;
tempArray[i+1][j] =4;
tempArray[i+2][j] =4;
tempArray[i+3][j] =4;
tempArray[i+4][j] =4;
makes = makes + 3;
fg=true;
}
}
if(i<=3){//判斷4個(gè)元素
if(now == arr[i+1][j] && now == arr[i+2][j] && now == arr[i+3][j] && now !=4){
tempArray[i][j] =4;
tempArray[i+1][j] =4;
tempArray[i+2][j] =4;
tempArray[i+3][j] =4;
makes = makes + 2;
fg=true;
}
}
if(i<=4){//判斷3個(gè)元素
if(now == arr[i+1][j] && now == arr[i+2][j] && now !=4){
tempArray[i][j] =4;
tempArray[i+1][j] =4;
tempArray[i+2][j] =4;
makes = makes + 1;
fg=true;
}
}
if(j<=2){//判斷5個(gè)元素
if(now == arr[i][j+1] && now == arr[i][j+2] && now == arr[i][j+3] && now == arr[i][j+4] && now !=4){
tempArray[i][j] =4;
tempArray[i][j+1] =4;
tempArray[i][j+2] =4;
tempArray[i][j+3] =4;
tempArray[i][j+4] =4;
makes = makes + 3;
fg=true;
}
}
if(j<=3){//判斷4個(gè)元素
if(now == arr[i][j+1] && now == arr[i][j+2] && now == arr[i][j+3] && now !=4){
tempArray[i][j] =4;
tempArray[i][j+1] =4;
tempArray[i][j+2] =4;
tempArray[i][j+3] =4;
makes = makes + 2;
fg=true;
}
}
if(j<=4){//判斷3個(gè)元素
if(now == arr[i][j+1] && now == arr[i][j+2] && now !=4){
tempArray[i][j] =4;
tempArray[i][j+1] =4;
tempArray[i][j+2] =4;
makes = makes + 1;
fg=true;
}
}
}
}
//最后變化所得的數(shù)組都在 臨時(shí)數(shù)組里面惠猿,所以將臨時(shí)數(shù)組結(jié)果給最后的數(shù)組
for(var a:int = 0;a<7;a++){
for(var b:int = 0;b<7;b++){
arr[a][b] = tempArray[a][b];
}
}
if(fg==true){//有元素清除
//創(chuàng)建一個(gè)新的View,有閃動(dòng)效果负间,可移除
//delays();
createAniView();
var delayStart:Timer = new Timer(500, 1);//為了讓消除的時(shí)候可以有直接的消除效果偶妖,選擇使用延遲的做法,讓視覺上有個(gè)替換的過程
delayStart.addEventListener(TimerEvent.TIMER_COMPLETE, goPlay);
delayStart.start();
function goPlay(e:TimerEvent):void{
updateItem();
updateView();
//消除元素結(jié)束后政溃,需要?jiǎng)?chuàng)建新的元素加到空位
reCreateGroup();
trace("是否可以繼續(xù)消除 "+isCanClean());
while(isCanClean()){//還可以繼續(xù)消除
cleanItem();
}
}
}
makeText.text = "當(dāng)前積分:"+makes;
}
執(zhí)行掉落的方法:
//更新趾访,將數(shù)組移位,填滿空的元素董虱,上方元素掉落
private function updateItem():void{
for(var j:int = 0;j<7;j++){
var temps:Array = new Array();// 用來保存臨時(shí)的結(jié)果
var num:int = 0;
for(var i:int=0;i<7;i++){
if(arr[j][i] != 4){
temps[num] = arr[j][i];
num++;
}
}
//trace(" j = "+j+" "+temps);
for(var k:int = 0;k<7;k++){
arr[j][k] = 4;
}
var n:int = 0;
for(var m:int=(7-temps.length);m <7;m ++){
arr[j][m] = temps[n];
//arr[m][j] = temps[n];
n++;
}
}
}
運(yùn)行截圖
源碼:點(diǎn)擊下載