【实例简介】android 弹球游戏制作
【实例截图】
【核心代码】
package wyf.ytl; //声明包语句
public class BallGoThread extends Thread {
GameView father;
boolean flag = true;
public BallGoThread(GameView father) {
this.father = father;
}
public void run() {
while (flag) {
switch (father.direction) {
case 0:
// 右上
// 当在向右上角移动的时候x坐标加,y坐标减
father.ballx = father.ballx father.ballSpan;
father.bally = father.bally - father.ballSpan;
//当碰到右边的墙壁的时候,向左下飘
if (father.ballx >= father.screenWidth - father.ballSize) {
father.direction = 3;
} else if (father.bally <= 0) {
// 碰到上壁,产生反射向右下飘
father.direction = 1;
}
break;
case 3:
// 向左上角飘,x坐标逐渐减,y坐标逐渐减
father.ballx = father.ballx - father.ballSpan;
father.bally = father.bally - father.ballSpan;
if (father.ballx <= 0) {// 碰到左壁
father.direction = 0;
} else if (father.bally <= 0) {// 碰到上壁
father.direction = 2;
}
break;
case 1:
// 向右下角飘
father.ballx = father.ballx father.ballSpan;
father.bally = father.bally father.ballSpan;
if (father.bally >= father.screenHeight - father.bannerHeight
- father.bottomSpance - father.ballSize) {// 碰到下壁
checkCollision(1);
} else if (father.ballx >= father.screenWidth - father.ballSize) {// 碰到右壁
father.direction = 2;
}
break;
case 2:
// 向左下角飘
father.ballx = father.ballx - father.ballSpan;
father.bally = father.bally father.ballSpan;
if (father.bally >= father.screenHeight - father.bannerHeight
- father.bottomSpance - father.ballSize) {// 碰到下壁
checkCollision(2);
} else if (father.ballx <= 0) {// 碰到左壁
father.direction = 1;
}
break;
}
try {
Thread.sleep(80);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void checkCollision(int direction) {
if (father.ballx >= father.bannerX - father.ballSize
&& father.ballx <= father.bannerX father.bannerWidth) {// 碰到板
switch (direction) {
case 1:
father.direction = 0;
break;
case 2:
father.direction = 3;
break;
}
} else {// 没有碰到板
// 游戏失败
father.tt.flag = false;
father.bgt.flag = false;
father.status = 2;
}
}
}