}
else{ //一定几率随机变向
direction = (d>12?9:5) + (int)(Math.random()*100)%3;
}
}
else if(d == 0){ //正撞到上边界
if(Math.random() < 0.4){ //一定几率沿正确反射路线返回
direction = 8;
}
else{
direction = (Math.random() < 0.5?7:9); //一定几率随机变向
}
}
}
//判断是否撞到下边界
else if(y > father.fieldDown){
//不是正撞
if(d >4 && d<12 && d!=8){
if(Math.random() < changeOdd){ //按正常反射路线变向
direction = (d>8?24:8) - d;
}
else{ //随机变向
direction = (d>8?13:1) +(int)(Math.random()*100)%3;
}
}
else if(d == 8){ //正撞到下边界
if(Math.random() < 0.4){ //正常变向
direction = 0;
}
else{ //随机变向
direction = (Math.random()>0.5?1:15);
}
}
}
}
/*
* 此方法检测是否碰到AI运动员,如果碰到,则调用handleCollision方法处理碰撞,
* 同时播放声音设置足球新速率和设置lastKicker
*/
public void checkForAIPlayers(){
int r = (this.ballSize + father.playerSize)/2;
for(Player p:father.alAIPlayer){
if((p.x - this.x)*(p.x - this.x) + (p.y - this.y)*(p.y - this.y) <= r*r){ //发生碰撞
handleCollision(this,p); //处理碰撞
if(father.father.wantSound && father.father.mpKick!=null){ //播放声音
try { //用try/catch语句包装
father.father.mpKick.start();
} catch (Exception e) {}
}
velocity = p.power;
lastKicker = 8; //记录最后一脚是谁踢的
}
}
}
/*
* 此方法检测是否碰到了玩家 的足球运动员,
*/
public void checkForUserPlayers(){
int r = (this.ballSize + father.playerSize)/2;
for(Player p:father.alMyPlayer){
if((p.x - this.x)*(p.x - this.x) + (p.y - this.y)*(p.y - this.y) <= r*r){ //发生碰撞
handleCollision(this,p); //处理碰撞
if(father.father.wantSound && father.father.mpKick!=null){ //播放声音
try {
father.father.mpKick.start();
} catch (Exception e) {}
}
velocity = p.power; //被 赋予新速度
lastKicker = 0; //记录最后一脚谁踢的
}
}
}
/*
* 此方法处理足球和运动员之间的碰撞,AI和玩家的处理方式是一样的,
* 首先查看Player对象的movingDirection,再综合Player对象的
* attackDirection,确定方向范围,类似直角坐标系中的4个象限,
* 然后在方向范围中随机产生一个,这样产生的方向有惯性在里面,看着
* 比较真实。需要注意的是如果足球和运动员碰撞时运动员静止不动,
* 那么可选的方向就是1或15(进攻方向朝上)、7或9(进攻方行朝下)
*/
public void handleCollision(Ball ball,Player p){
switch(p.movingDirection){
case 12: //移动方向向左
if(p.attackDirection == 0){ //攻击方向向上
ball.direction = 13 + (int)(Math.random()*100)%3; //取13,14,15中一个
}
else{ //攻击方向向下
ball.direction = 9 + (int)(Math.random()*100)%3; //取9,10,11中一个
}
break;
case 4: //移动方向向右
if(p.attackDirection == 0){ //攻击方向向上
ball.direction = 1 + (int)(Math.random()*100)%3; //取1,2,3中一个
}
else{ //攻击方向向下
ball.direction = 5 + (int)(Math.random()*100)%3; //取5,6,7中一个
}
break;
default: //没有移动
if(p.attackDirection == 0){ //攻击方向向上
ball.direction = 15 + (int)(Math.random()*100)%3; //取1,2,3中一个
if(ball.direction > 15){
ball.direction = ball.direction % 16;
}
}
else{ //攻击方向向下
ball.direction = 7 + (int)(Math.random()*100)%3; //取7,8,9中一个
}
break;
}
}
/*
* 此方法用于检测是否进球,如是,则相应球队得分加1,然后判断游戏是否结束(游戏规则是谁先进够8个谁就赢)
*/
public void checkIfScoreAGoal(){
if(this.y <= father.fieldUp && this.x > father.AIGoalLeft && this.x < father.AIGoalRight){
//上方球门进球,即玩家
isPlaying = false;
father.scores[0]++;
father.checkIfLevelUp();
}
else if(this.y >= father.fieldDown && this.x > father.myGoalLeft && this.x < father.myGoalRight){
//AI进球
isPlaying = false;
father.scores[1]++;
father.checkIfLevelUp();
}
}
//升级方法
public void levelUp(){
this.minVelocity +=3;
if(minVelocity > 20){
minVelocity = 20;
}
}
/*
* 该方法判断是否碰到了Bonus,如果碰到,对相应的Bonus进行操作
* 改变其状态,调用其方法修改游戏参数等等,并播放声音
*/
public void checkForBonus(){
if(father.balLive.size() != 0){
for(Bonus b:father.balLive){
if((b.x - x)*(b.x - x) + (b.y-y)*(b.y-y) <= (b.bonusSize/2+ballSize/2)*(b.bonusSize/2+ballSize/2)
&& b.status == Bonus.LIVE){
b.status = Bonus.EFFECTIVE;
father.balLive.remove(b);
b.setTarget(this.lastKicker);
b.doJob();
b.setTimeout(Bonus.EFFECT_SPAN);
if(father.father.wantSound){
if(b instanceof IceBonus){ //是冰冻小球
try {
father.father.mpIce.start();
} catch (Exception e) {}
}
else if( b instanceof LargerGoalBonus){ //是扩大球门的
try {
father.father.mpLargerGoal.start();
} catch (Exception e) {}
}
}
break;
}
}
}
}
5.3游戏界面
public class FootballActivity extends Activity {
View current; // 记录当前View
GameView gv; // GameView对象
WelcomeView welcome; // 欢迎界面
LoadingView lv; // 进度条加载界面
int keyState = 0; // xxxx00为不动,xxxx10为向左,xxxx01为向右
PlayerMoveThread pmt; // 移动球员位置的线程
boolean wantSound = true; // 是否播放声音标志位
int[] layoutArray; // 表示球员球场站位的数组
MediaPlayer mpWelcomeMusic; // 游戏开始前的欢迎音乐
MediaPlayer mpKick; // 踢球音效
MediaPlayer mpCheerForWin; // 赢了的音乐
MediaPlayer mpCheerForLose; // 输了的音乐
MediaPlayer mpCheerForGoal; // 进球后的音乐
MediaPlayer mpIce; // 撞到冰山后的音乐
MediaPlayer mpLargerGoal; // 撞到打开球门后的音乐
Rect[] rectPlus; // 代表增加球员按钮的矩形框
Rect[] rectMinus; // 代表减少球员按钮的矩形框
Rect rectSound; // 是否播放声音按钮的矩形框
Rect rectStart; // 开始按钮的矩形框
Rect rectQuit; // 退出按钮的矩形框
Rect rectGallery; // 表示Gallery的矩形框
int[] imageIDs = { // 存放8个俱乐部的图片ID
R.drawable.club_1, R.drawable.club_2, R.drawable.club_3, R.drawable.club_4,
R.drawable.club_5, R.drawable.club_6, R.drawable.club_7,
R.drawable.club_8 };
int clubID = imageIDs[0]; // 记录用户选择的俱乐部的ID
public void onCreate(Bundle savedInstanceState) { // 重写onCreate方法
super.onCreate(savedInstanceState);
initWelcomeSound(this)
上一篇:
基于android手机通讯录的毕业论文
下一篇:
试论APP广告在手机中的应用