基于Android的瘋狂足球遊戲源代碼。
相關檔案下載下傳在Linux公社的1号FTP伺服器裡,下載下傳位址:
使用者名:www.linuxidc.com
密碼:www.muu.cc
在 2011年LinuxIDC.com\10月\10月\基于Android的瘋狂足球遊戲源代碼
示例:
package wyf.wpf; //聲明包語句
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
public class Ball extends Thread{
int x; //足球中心的x坐标
int y; //足球中心的y坐标
int direction=-1; //足球的運動方向,從0到15順時針代表從向上開始的16個方向,寫書的時候畫個圖貼上去
int velocity=20; //足球的運動速率
int maxVelocity = 20; //最大運動速率
int minVelocity = 5; //最小運動速率
int ballSize = 10; //足球大小
Matrix matrix; //Matrix對象,用來實作足球圖檔的翻轉效果
Bitmap bmpBall; //足球的圖檔
GameView father; //FieldView對象引用
float acceleration=-0.10f; //足球在無人撞擊時速度會逐漸衰減
boolean isStarted; //比賽是否開始
boolean isPlaying; //比賽是否正在進行
float sin675=0.92f; //特定角度正弦值,用于計算移動的像素個數
float sin225=0.38f; //特定角度正弦值,用于計算移動的像素個數
float sin45=0.7f; //特定角度正弦值,用于計算移動的像素個數
int sleepSpan = 50; //休眠時間
float changeOdd = 0.6f; //變向的幾率
int lastKicker; //最近的這一腳是誰踢的,0代表自己,8代表AI
public Ball(GameView father){
super.setName("##-Ball"); //設定線程名字,用于調試用
this.father = father;
Resources r = father.getContext().getResources(); //擷取Resources對象
bmpBall = BitmapFactory.decodeResource(r, R.drawable.ball);//設定圖檔
matrix = new Matrix();
isStarted = true; //設定循環變量
isPlaying = true; //
}
//線程的任務方法
public void run(){
while(isStarted){
while(isPlaying){
//移動足球
move();
//碰撞檢測
checkCollision();
//休眠一下
try{
Thread.sleep(sleepSpan);
}
catch(Exception e){
e.printStackTrace();
}
}
try{
Thread.sleep(500);
}
catch(Exception e){ e.printStackTrace();
}
}
}
//繪制足球圖檔
public void drawSelf(Canvas canvas){
Bitmap bmp=null;
if(isPlaying){
matrix.postRotate(5); //将足球圖檔旋轉一定角度,生成新的Bitmap對象
bmp = Bitmap.createBitmap(bmpBall, 0, 0, ballSize, ballSize, matrix, true);
}
else{
bmp = bmpBall; //如果沒有在遊戲中,則不作旋轉變換
}
canvas.drawBitmap(bmp, x-ballSize/2, y-ballSize/2, null);
}
//移動足球
public void move(){
switch(direction){
case 0: //方向向上
y -= velocity; //移動
decreamentVelocity(); //衰減速度
break;
case 1: //上偏右22.5度
x += (int)(velocity*sin225); //移動
y -= (int)(velocity*sin675);
decreamentVelocity(); //衰減速度
break;
case 2: //上偏右45度
x += (int)(velocity*sin45); //移動
y -= (int)(velocity*sin45);
decreamentVelocity(); //衰減速度
break;
case 3: //上偏右67.5度
x += (int)(velocity*sin225); //移動
y -= (int)(velocity*sin675);
decreamentVelocity(); //衰減速度
break;
case 4: //方向向右
x += velocity; //移動
decreamentVelocity(); //衰減速度
break;
case 5: //右偏下22.5度
x += (int)(velocity*sin675); //移動
y += (int)(velocity*sin225);
decreamentVelocity(); //衰減速度
break;
case 6: //右偏下45度
x += (int)(velocity*sin45); //移動
y += (int)(velocity*sin45);
decreamentVelocity(); //衰減速度
break;
case 7: //右偏下67.5度
x += (int)(velocity*sin225); //移動
y += (int)(velocity*sin675);
decreamentVelocity(); //衰減速度
break;
case 8: //方向向下
y += velocity; //移動
decreamentVelocity(); //衰減速度
break;
case 9: //下偏左22.5度
x -= (int)(velocity*sin225); //移動
y += (int)(velocity*sin675);
decreamentVelocity(); //衰減速度
break;
case 10: //下偏左45度
x -= (int)(velocity*sin45); //移動
y += (int)(velocity*sin45);
decreamentVelocity(); //衰減速度
break;
case 11: //下偏左67.5度
x -= (int)(velocity*sin675); //移動
y += (int)(velocity*sin225);
decreamentVelocity(); //衰減速度
break;
case 12: //方向向左
x -= velocity; //移動
decreamentVelocity(); //衰減速度
break;
case 13: //左偏上22.5度
x -= (int)(velocity*sin675); //移動
y -= (int)(velocity*sin225);
decreamentVelocity(); //衰減速度
break;
case 14: //左偏上45度
x -= (int)(velocity*sin45); //移動
y -= (int)(velocity*sin45);
decreamentVelocity(); //衰減速度
break;
case 15: //左偏上67.5度
x -= (int)(velocity*sin225); //移動
y -= (int)(velocity*sin675);
decreamentVelocity(); //衰減速度
break;
default:
break;
}
}
public void decreamentVelocity(){
velocity = (int)(velocity*(1+acceleration)); //衰減速���
if(velocity
velocity = minVelocity;
}
}
//總的碰撞檢測方法,其中包含多個檢測子項,分别為其設立一個方法
public void checkCollision(){
checkForBorders(); //檢查是否出邊界
checkForAIPlayers(); //檢查是否碰到AI
checkForUserPlayers(); //檢查是否碰到玩家
checkIfScoreAGoal(); //檢查是否進球啦
checkForBonus(); //檢查是否碰到Bonus
}
public void checkForBorders(){
int d = direction;
//左右是不是出邊界了
if(x <= father.fieldLeft){
//撞了左邊界
if(d>8 && d<16 && d!=12){ //如果不是正撞到左邊界
if(Math.random() < changeOdd){ //一定機率機率沿正确反射路線變向
direction = 16 - direction;
}
else{ //一定機率随機變向
direction = (direction>12?1:5) + (int)(Math.random()*100)%3;
}
}
else if(d == 12){ //如果是正撞到左邊界
if(Math.random() < 0.4){ //注意這個機率要小,因為正撞上去希望随機變向的機率大一些
direction = 4;
}
else{
direction = (Math.random() > 0.5?3:5);
}
}
}
else if(x > father.fieldRight){
//撞到右邊界
if(d >0 && d<8 && d!=4){
if(Math.random() < changeOdd){ //按正常反射路線變向
direction = 16-direction;
}
else{ //一定幾率随機變向
direction = (direction>4?9:13) + (int)(Math.random()*100)%3;
}
}
else if(d == 4){ //如果是正撞到右邊界
if(Math.random() < 0.4){
direction = 12;
}
else{
direction = (Math.random()>0.5?11:13);
}
}
}
d = direction;
//判斷是否撞到上邊界
if(y < father.fieldUp){
//不是正撞
if(d>0 && d<4 || d>12&&d<16){
if(Math.random() < changeOdd){ //一定幾率沿正确反射路線變向
direction = (d>12?24:8) - d;
}
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);
}
}
}
}
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; //記錄最後一腳誰踢的
}
}
}
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;
}
}
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;
}
}
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;
}
}
}
}
}
