package l.y.l;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import l.y.l.R;
//战斗类
public class Fighting extends SurfaceView implements Callback, Runnable {
//屏幕的宽
private int screenWidth;
//屏幕的高
private int screenHeight;
//画布
Canvas canvas = null;
//画笔
Paint paint = null;
SurfaceHolder holder = null;
//背景图片
Bitmap backGround[] = new Bitmap[2];
//当前背景图片1的高度
int bg1 = 0;
//当前背景图片2的高度
int bg2 = 0;
//线程运行标志位
public static boolean flag = false;
//暂停标志位
public static boolean pause = false;
Thread thread;
public static Plane plane = null; //主角飞机对象
int enemyCount = 20;//敌机个数
List<Plane> enemys = new ArrayList<Plane>();//敌机组
Boss boss;
Award award;//奖励
Context context;
//触屏后飞机将要移动到的位置的X、Y坐标
int moveToX = 0;
int moveToY = 0;
int enemyFlag = 0;//敌机重置标志位
int enemyInterval = 20;//敌机出现时间间隔
boolean bombFlag = false;
public static int score = 0;//总分
public static int num;//消灭敌机的数量
public int round = 1;//关卡
Random random = new Random();
public int bossFlag = 20;//消灭bossFlag个敌机后boss才会出现
Bitmap pauseIcon = null;
Bitmap playIcon = null;
Fighting(Context context,int screenWidth, int screenHeight) {
super(context);
this.context = context;
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
holder = getHolder();
paint = new Paint();
//设置背景图片1的位置
bg1 = 0;
//设置背景图片2的位置
bg2 = -screenHeight;
holder.addCallback(this);
//初始化战斗界面
init();
//获取焦点
setFocusable(true);
}
private void init(){
getBackGround(context);
//初始化飞机图片
Bitmap[] temp = new Bitmap[3];
temp[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.my);
temp[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_l);
temp[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_2);
temp[0] = Bitmap.createScaledBitmap(temp[0], temp[0].getWidth()/2, temp[0].getHeight()/2, true);
temp[1] = Bitmap.createScaledBitmap(temp[1], temp[1].getWidth()/2, temp[1].getHeight()/2, true);
temp[2] = Bitmap.createScaledBitmap(temp[2], temp[2].getWidth()/2, temp[2].getHeight()/2, true);
//初始化boss
Bitmap[] temp1 = new Bitmap[3];
temp1[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.boss1);
temp1[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.boss2);
temp1[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.boss3_t);
boss = new Boss(context, screenWidth, screenHeight, temp1);
//初始化敌机
Bitmap[] temp2 = new Bitmap[3];
temp2[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.enemy13);
temp2[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.enemy13_t);
temp2[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.enemy14_t);
for(int i = 0; i < enemyCount; i++){
Enemy enemy = new Enemy(context, screenWidth, screenHeight, temp2);
//左右移动型
enemy.moveStyle = 1;
enemys.add(enemy);
}
//初始化飞机
plane = new Plane(context, screenWidth, screenHeight, temp);
plane.enemys = enemys;
plane.boss = boss;
plane.setTarget();//给飞机设置目标
plane.shotInterval = 3;//设置飞机射击速度
for(Plane enemy:enemys){
enemy.enemys.add(plane);
enemy.setTarget();
}
boss.enemys.add(plane);
boss.setTarget();
boss.shotStyle = 5;
boss.moveStyle = 1;
award = new Award(context, screenWidth, screenHeight);
award.state = true;
if(FinalPlaneActivity.backMusicFlag){
FinalPlaneActivity.backMusic.start();
}
pauseIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.pause);
pauseIcon = Bitmap.createScaledBitmap(pauseIcon, 30, 30, false);
playIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.play);
playIcon = Bitmap.createScaledBitmap(playIcon, 30, 30, false);
}
/**
* 获取背景图片
* @param context 当前上下文
*/
private void getBackGround(Context context){
backGround[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.background2);
backGround[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.background3);
backGround[0] = Bitmap.createScaledBitmap(backGround[0], screenWidth, screenHeight, false);
backGround[1] = Bitmap.createScaledBitmap(backGround[1], screenWidth, screenHeight, false);
}
@Override
public void run() {
// TODO Auto-generated method stub
while(flag){
if(!pause){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
draw();
}
}
}
private void draw(){
canvas = holder.lockCanvas();
drawBackGround(canvas);
//打满十个小敌机,会出现boss
if(num == bossFlag){
boss.reset();//boss重置
boss.state = 2;//刚重置时boss不显示
switch(round){
case 1:
boss.moveStyle = 1;
boss.shotStyle = 2;
boss.planePics[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.boss1);
boss.width = boss.planePics[0].getWidth();
boss.height = boss.planePics[0].getHeight();
boss.moveStyle = 0;//左右水平移动
boss.shotStyle = 1;//发射一枚子弹
boss.health = 1000;
break;
case 2:
boss.moveStyle = 1;
boss.shotStyle = 2;
boss.planePics[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.boss2);
boss.width = boss.planePics[0].getWidth();
boss.height = boss.planePics[0].getHeight();
//改变敌机的发射模式
for(Plane enemy:enemys){
int enemyMoveStyle = Math.abs(random.nextInt()%2);
enemy.moveStyle = enemyMoveStyle;
enemy.health = 20;
}
boss.health = 3000;
break;
case 3:
boss.moveStyle = 1;
boss.shotStyle = 3;
boss.planePics[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.boss3_t);
boss.width = boss.planePics[0].getWidth();
boss.height = boss.planePics[0].getHeight();
//改变敌机的发射模式
for(Plane enemy:enemys){
enemy.shotStyle = 1;
enemy.health = 30;
}
boss.health = 6000;
break;
case 4:
boss.moveStyle = 1;
boss.shotStyle = 4;
boss.planePics[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.boss4);
boss.width = boss.planePics[0].getWidth();
boss.health = boss.planePics[0].getHeight();
//改变敌机的发射模式
for(Plane enemy:enemys){
enemy.shotStyle = 1;
enemy.health = 40;
}
boss.health = 10000;
break;
case 5:
boss.moveStyle = 1;
boss.shotStyle = 5;
boss.planePics[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.boss5);
boss.width = boss.planePics[0].getWidth();
boss.height = boss.planePics[0].getHeight();
//改变敌机的发射模式
for(Plane enemy:enemys){
enemy.shotStyle = 1;
enemy.health = 50;
}
boss.health = 15000;
break;
default :round ++;
}
boss.maxHealth = boss.health;
}else if(num > bossFlag){
boss.state = 1;
if(boss.state == 1)
boss.move(canvas, paint, moveToX, moveToY);//画boss,boss死亡时会将消灭敌机输重新置0
if(boss.health <= 0){
round++;
bossFlag+=20;
num = 0;
boss.state = 2;
}
}
//战机死亡后,初始化出现位置,所有敌机爆炸,子弹消失
if(plane.health <= 0){
moveToX = screenWi
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Android游戏开发基础飞机大战 (101个子文件)
jarlist.cache 120B
proguard.cfg 1KB
Fighting.class 9KB
Plane.class 7KB
Rank.class 6KB
Bullet.class 4KB
Boss.class 4KB
Award.class 3KB
Enemy.class 3KB
FinalPlaneActivity.class 3KB
SettingActivity.class 2KB
MainActivity.class 2KB
Rank$1.class 2KB
R$drawable.class 2KB
Rank$2.class 2KB
PlaneDBHelper.class 1KB
Animation.class 1KB
FinalPlaneActivity$1.class 1KB
SettingActivity$1.class 1KB
SettingActivity$2.class 1KB
FinalPlaneActivity$2.class 1KB
R$string.class 645B
R$id.class 631B
R$layout.class 476B
R.class 471B
R$raw.class 433B
BuildConfig.class 319B
R$attr.class 301B
.classpath 475B
Fighting.java 11KB
Plane.java 8KB
Rank.java 4KB
R.java 4KB
Bullet.java 4KB
FinalPlaneActivity.java 4KB
Award.java 3KB
Boss.java 3KB
Enemy.java 2KB
SettingActivity.java 2KB
MainActivity.java 2KB
PlaneDBHelper.java 1KB
Animation.java 776B
BuildConfig.java 147B
background3.jpg 107KB
background2.jpg 107KB
planeback.jpg 27KB
back.mp3 367KB
back1.mp3 318KB
bomb.mp3 30KB
bullet.mp3 8KB
play.png 40KB
boss4.png 24KB
boss1.png 22KB
boss2.png 21KB
boss3_t.png 17KB
pause.png 12KB
my_2.png 7KB
my_l.png 7KB
my.png 5KB
enemy13.png 5KB
enemy14_t.png 5KB
enemy13_t.png 4KB
ic_launcher.png 4KB
blasts6.png 4KB
blasts7.png 4KB
blasts5.png 4KB
boss5.png 4KB
blasts8.png 4KB
blasts4.png 4KB
blasts9.png 3KB
blasts3.png 3KB
blasts10.png 3KB
ic_launcher.png 3KB
myplane.png 2KB
addhealth.png 2KB
addbomb.png 2KB
addspeed.png 2KB
addlife.png 2KB
bomb.png 2KB
addpower.png 2KB
blasts2.png 2KB
ic_launcher.png 2KB
blasts11.png 2KB
blasts1.png 1KB
bossbullet.png 1006B
myzd1.png 707B
enzd1.png 371B
blastz2.png 219B
blastz1.png 208B
blastz3.png 170B
org.eclipse.jdt.core.prefs 177B
.project 846B
project.properties 361B
mainlayout.xml 1KB
AndroidManifest.xml 1KB
AndroidManifest.xml 1KB
rank.xml 1KB
set.xml 596B
strings.xml 561B
ranklist.xml 501B
共 101 条
- 1
- 2
资源评论
- 恒水2019-12-18哪个 数据库是什么欸
- 南风未起2019-10-23还好啊啊啊啊
liuyilin9608
- 粉丝: 2
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功