來玩泡泡龍
花了一小時,做了準備工作
發射泡泡
按下屏幕可以發射泡泡,泡泡碰撞後,粘在上面
泡泡粘貼的位置
根據角度計算
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
泡泡破碎
破碎優化
菜單與關卡
未完待續