從零開始的泡泡龍游戲

來玩泡泡龍

花了一小時,做了準備工作

0_1525269456973_QQ圖片20180502215537.jpg

發射泡泡

按下屏幕可以發射泡泡,泡泡碰撞後,粘在上面

0_1525327635185_Screenshot_20180503-140700.jpg

泡泡粘貼的位置

0_1525334610207_QQ截圖20180503160252.png

根據角度計算

0_1525335750992_QQ截圖20180503161857.png

var radian = Math.atan2(pointB.y - pointA.y, pointB.x - pointA.x )

var angle = 180 / Math.PI * radian
console.log("angle=", angle)
if(angle>=45 && angle<=90){
    //右下角
}
else if(angle>90 && angle<=135){
    //左下角
}
else if(angle<45 && angle>=-45){
    //右
}
else if(angle<-45 && angle >= -90){
    //右上角
}
else if(angle<-90 && angle >= -135){
    //左上角
}
else{
    //左
}

消除計算

每個發射球周圍有六個球,依次計算他們是否跟發射球顏色相同,如果相同,再次遞歸,直到找不到相同顏色球

function checkSameTypeSize(newI, newJ, tempType){
        console.log("checkSameTypeSize ", newI, newJ, tempType)
        var hasSize = 0
        var flag = newI%2
        var len = flag==0?9:10
        var otherLen = flag==1?9:10

        var tempPoint = new Array

        //左側泡泡
        if(newJ-1>=0){
            if(level.arrays[newI][newJ-1] == tempType){
                tempPoint.push(Qt.point(newI, newJ-1))
            }
        }

        //右側泡泡
        if(newJ+1<=len-1){
            if(level.arrays[newI][newJ+1] == tempType){
                tempPoint.push(Qt.point(newI, newJ+1))
            }
        }
        //上側
        if(newI-1>=0){

            if(flag==0){
                //上層是偶數行
                if(newJ>=0){
                    //上左側泡泡
                    //上左側 存在
                    if(level.arrays[newI-1][newJ] == tempType){
                        tempPoint.push(Qt.point(newI-1, newJ))
                    }
                }
                 //上右側泡泡
                if(newJ+1<=otherLen-1){
                    //存在
                    if(level.arrays[newI-1][newJ+1] == tempType){
                        tempPoint.push(Qt.point(newI-1, newJ+1))
                    }
                }
            }
            else{
                //上層是奇數行
                if(newJ-1>=0){
                    //上左側泡泡
                    //上左側 存在
                    if(level.arrays[newI-1][newJ-1] == tempType){
                        tempPoint.push(Qt.point(newI-1, newJ-1))
                    }
                }
                 //上右側泡泡
                if(newJ<=otherLen-1){
                    //存在
                    if(level.arrays[newI-1][newJ] == tempType){
                        tempPoint.push(Qt.point(newI-1, newJ))
                    }
                }
            }

        }

        //下側
        if(newI+1<level.arrays){
            if(flag==0){
                //下層是偶數行
                if(newJ>=0){
                    //下左側泡泡
                    //下左側 存在
                    if(level.arrays[newI+1][newJ] == tempType){
                        tempPoint.push(Qt.point(newI+1, newJ))
                    }
                }
                 //下右側泡泡
                if(newJ+1<=otherLen-1){
                    //存在
                    if(level.arrays[newI+1][newJ+1] == tempType){
                        tempPoint.push(Qt.point(newI+1, newJ+1))
                    }
                }
            }
            else{
                //下層是奇數行
                if(newJ-1>=0){
                    //下左側泡泡
                    //下左側 存在
                    if(level.arrays[newI+1][newJ-1] == tempType){
                        tempPoint.push(Qt.point(newI+1, newJ-1))
                    }
                }
                 //上右側泡泡
                if(newJ<=otherLen-1){
                    //存在
                    if(level.arrays[newI+1][newJ] == tempType){
                        tempPoint.push(Qt.point(newI+1, newJ))
                    }
                }
            }
        }

        var tempArrayFunc = new Array

        for(var p=0 in tempPoint){
            var pp = tempPoint[p]
            if(tempArray[pp.x][pp.y]==1){
                //已經採集過這個點
            }
            else{
                tempArray[pp.x][pp.y] = 1
                tempArrayCaiji++
                tempArrayFunc.push(Qt.point(pp.x, pp.y))
            }
        }

        //遞歸採集
        for(var p=0 in tempArrayFunc){
            var pp = tempArrayFunc[p]
            console.log("============pp", pp)
            checkSameTypeSize(pp.x, pp.y, tempType)
        }

    }

粘貼位置優化

六個方向每個角度爲60

邊界碰撞優化

碰撞邊界後不超出邊界,強制設置x,y座標

function checkScreen(x, y){
        var w = Screen.width
        var h = Screen.height
        var radian = Math.PI/180 * moveAngle

        var point = Qt.point(x,y)

        if(x<=2/* && moveAngle<-90*/){
            moveAngle = -(180+moveAngle)
            point.x = 2
            point.y = (fireBall.x-2)*Math.tan(radian) + fireBall.y
        }
        else if(x+ballRadius*2+2>=w /*&& moveAngle>-90*/){
            moveAngle = 180-moveAngle
            point.x = w-ballRadius*2-2
            point.y = (w-fireBall.x-ballRadius*2-2)*Math.tan(radian) + fireBall.y
        }

        return point
    }

球與球碰撞優化

碰撞前不移動發射球,檢測到預碰撞後,下次座標直接設置爲正確的點位置

分數

//計分
score += ballScore

泡泡破碎

0_1525404206965_ball1.gif

破碎優化

0_1525411221012_ball3.gif

菜單與關卡

0_1525420516148_Screenshot_20180504-155437.jpg

0_1525420525652_Screenshot_20180504-155442.jpg

未完待續

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章