package wyf.ytl;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/**
*
* 主游戏界面
*
*/
public class GameView extends SurfaceView implements SurfaceHolder.Callback{
int selectMap = 1;//第几关
PlaneActivity activity;
private TutorialThread thread;//刷帧的线程
GameViewBackGroundThread gameThread;//背景滚动线程
KeyThread keyThread;//键盘监听线程
MoveThread moveThread;//移动物体的线程
ExplodeThread explodeThread;//爆炸换帧的线程
int backGroundIX = 0;//核心图的x坐标
int i = 0;//核心图的索引
int cloudX = 470;//云彩的X坐标
Bitmap battleback;//背景的大图元
Bitmap[] battlebacks = new Bitmap[ConstantUtil.pictureCount];//装分割以后的图片
Bitmap cloud;//云彩
Bitmap enemyPlane1;//敌机1
Bitmap enemyPlane2;//敌机2
Bitmap enemyPlane3;//敌机3
Bitmap enemyPlane4;//敌机4
Bitmap[] number = new Bitmap[10];//数字数组
int[] explodesID = new int[]{//爆炸的所有帧
R.drawable.explode1,
R.drawable.explode2,
R.drawable.explode3,
R.drawable.explode4,
R.drawable.explode5,
R.drawable.explode6,
};
Bitmap[] explodes = new Bitmap[explodesID.length];//爆炸的数组
Bitmap hullBackground;//显示生命的背景图片
Bitmap hull;//生命的图片
Bitmap life;//血块的图片
Bitmap changebullet;
int status = 1;//游戏的状态1表示游戏中,2表示游戏失败即我方飞机没有了生命
Paint paint;//画笔
Plane plane = new Plane(50, 140, 1, 0, ConstantUtil.life, this);//初始化我方飞机
ArrayList<Bullet> badBollets = new ArrayList<Bullet>();//敌方飞机发出的子弹
ArrayList<Bullet> goodBollets = new ArrayList<Bullet>();//我方飞机发出的子弹
ArrayList<Explode> explodeList = new ArrayList<Explode>();//爆炸
ArrayList<ChangeBullet> changeBollets = new ArrayList<ChangeBullet>();//吃了改变枪的物体
ArrayList<EnemyPlane> enemyPlanes;//敌方的飞机
ArrayList<Life> lifes;//存放血块
SoundPool soundPool;//声音
HashMap<Integer, Integer> soundPoolMap;
MediaPlayer mMediaPlayer;
public GameView(PlaneActivity activity) {//构造器
super(activity);
this.activity = activity;//activity的引用
initSounds();
mMediaPlayer = MediaPlayer.create(activity, R.raw.gamestart);
mMediaPlayer.setLooping(true);
getHolder().addCallback(this);//注册接口
this.thread = new TutorialThread(getHolder(), this);//初始化刷帧线程
this.gameThread = new GameViewBackGroundThread(this);//初始化背景滚动线程
this.keyThread = new KeyThread(activity);
this.moveThread = new MoveThread(this);
this.explodeThread = new ExplodeThread(this);
if(activity.processView != null){
activity.processView.process += 20;
}
if(this.selectMap == 1){
enemyPlanes = Maps.getFirst();//取第一关的敌机
lifes = Maps.getFirstLife();//取第一关的的血块
changeBollets = Maps.getFirstBollet();//取第一关吃了改变枪的物体列表
}
initBitmap();//初始化所有图片
if(activity.isSound){
mMediaPlayer.start();
}
if(activity.processView != null){
activity.processView.process += 20;
}
}
public void initSounds(){
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(1, soundPool.load(getContext(), R.raw.bulletsound1, 1));
soundPoolMap.put(2, soundPool.load(getContext(), R.raw.explode, 1));
if(activity.processView != null){
activity.processView.process += 20;
}
soundPoolMap.put(3, soundPool.load(getContext(), R.raw.dead, 1));
if(activity.processView != null){
activity.processView.process += 20;
}
}
public void playSound(int sound, int loop) {
AudioManager mgr = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;
soundPool.play(soundPoolMap.get(sound), volume, volume, 1, loop, 1f);
}
public void initBitmap(){//初始化所有图片
if(activity.processView != null){
activity.processView.process += 20;
}
paint = new Paint();
paint.setColor(Color.BLACK);
battleback = BitmapFactory.decodeResource(getResources(), R.drawable.battleback);//大背景图片
cloud = BitmapFactory.decodeResource(getResources(), R.drawable.cloud);//云彩
for(int i=0; i<battlebacks.length; i++){//切成小图片
battlebacks[i] = Bitmap.createBitmap(battleback, ConstantUtil.pictureWidth*i, 0, ConstantUtil.pictureWidth, ConstantUtil.pictureHeight);
}
battleback = null;//释放掉大图
enemyPlane1 = BitmapFactory.decodeResource(getResources(), R.drawable.plane4);//敌机1的图片
enemyPlane2 = BitmapFactory.decodeResource(getResources(), R.drawable.plane5);//敌机2的图片
enemyPlane3 = BitmapFactory.decodeResource(getResources(), R.drawable.plane6);//敌机3的图片
enemyPlane4 = BitmapFactory.decodeResource(getResources(), R.drawable.plane7);//敌机4的图片
hullBackground = BitmapFactory.decodeResource(getResources(),R.drawable.hullbackground);
hull = BitmapFactory.decodeResource(getResources(), R.drawable.hull);
life = BitmapFactory.decodeResource(getResources(), R.drawable.life);
changebullet = BitmapFactory.decodeResource(getResources(), R.drawable.changebullet);
number[0] = BitmapFactory.decodeResource(getResources(), R.drawable.number0);
number[1] = BitmapFactory.decodeResource(getResources(), R.drawable.number1);
number[2] = BitmapFactory.decodeResource(getResources(), R.drawable.number2);
number[3] = BitmapFactory.decodeResource(getResources(), R.drawable.number3);
number[4] = BitmapFactory.decodeResource(getResources(), R.drawable.number4);
number[5] = BitmapFactory.decodeResource(getResources(), R.drawable.number5);
number[6] = BitmapFactory.decodeResource(getResources(), R.drawable.number6);
number[7] = BitmapFactory.decodeResource(getResources(), R.drawable.number7);
number[8] = BitmapFactory.decodeResource(getResources(), R.drawable.number8);
number[9] = BitmapFactory.decodeResource(getResources(), R.drawable.number9);
for(int i=0; i<explodes.length; i++){//初始化爆炸图片
explodes[i] = BitmapFactory.decodeResource(getResources(), explodesID[i]);
}
for(EnemyPlane ep : enemyPlanes){//为敌机初始化图片
if(ep.type == 1){
ep.bitmap = enemyPlane1;
}
else if(ep.type == 2){
ep.bitmap = enemyPlane2;
}
else if(ep.type == 3){
ep.bitmap = enemyPlane3;
}
else if(ep.type == 4){
ep.bitmap = enemyPlane4;
}
}
for(Life l : lifes){//为血块初始化图片
l.bitmap = life;
}
for(ChangeBullet cb : changeBollets){//为吃了改变枪的物体初始化图片
cb.bitmap = changebullet;
}
}
public void onDraw(Canvas canvas){//自己写的绘制方法,并非重写的
//画的内容是z轴的,后画的会覆盖前面画的
int backGroundIX=this.backGroundIX;
int i=this.i;
int cloudX = this.cloudX;
//解决i左侧的问题
if(backGroundIX>0){
int n=(backGroundIX/ConstantUtil.pictureWidth)+((backGroundIX%ConstantUtil.pictureWidth==0)?0:1);//计算i左面有几幅图
for(int j=1;j<=n;j++){
canvas.drawBitmap(
battlebacks[(i-j+ConstantUtil.pictureCount)%ConstantUtil.pictureCount],
backGroundIX-ConstantUtil.pictureWidth*j,
ConstantUtil.top,
paint
);
}
}
//解决i自己
canvas.drawBitmap
没有合适的资源?快使用搜索试试~ 我知道了~
小程序源码 太空保卫战源码.rar
共226个文件
png:112个
class:72个
java:21个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 44 浏览量
2023-03-20
07:12:37
上传
评论
收藏 2.53MB RAR 举报
温馨提示
免责声明:资料部分来源于合法的互联网渠道收集和整理,部分自己学习积累成果,供大家学习参考与交流。收取的费用仅用于收集和整理资料耗费时间的酬劳。 本人尊重原创作者或出版方,资料版权归原作者或出版方所有,本人不对所涉及的版权问题或内容负法律责任。如有侵权,请举报或通知本人删除。
资源推荐
资源详情
资源评论
收起资源包目录
小程序源码 太空保卫战源码.rar (226个子文件)
resources.ap_ 706KB
Plane.apk 731KB
GameView.class 9KB
GameView.class 9KB
WelcomeView.class 7KB
WelcomeView.class 7KB
Plane.class 5KB
Plane.class 5KB
Maps.class 5KB
Maps.class 5KB
MoveThread.class 4KB
MoveThread.class 4KB
EnemyPlane.class 4KB
EnemyPlane.class 4KB
ProcessView.class 3KB
ProcessView.class 3KB
WinView.class 3KB
WinView.class 3KB
HelpView.class 3KB
FailView.class 3KB
HelpView.class 3KB
FailView.class 3KB
PlaneActivity.class 2KB
PlaneActivity.class 2KB
KeyThread.class 2KB
KeyThread.class 2KB
PlaneActivity$1.class 2KB
PlaneActivity$1.class 2KB
R$drawable.class 2KB
R$drawable.class 2KB
GameViewBackGroundThread.class 2KB
GameViewBackGroundThread.class 2KB
Bullet.class 2KB
Bullet.class 2KB
ProcessView$TutorialThread.class 2KB
ProcessView$TutorialThread.class 2KB
ExplodeThread.class 1KB
ExplodeThread.class 1KB
WelcomeView$TutorialThread.class 1KB
WelcomeView$TutorialThread.class 1KB
GameView$TutorialThread.class 1KB
GameView$TutorialThread.class 1KB
FailView$TutorialThread.class 1KB
FailView$TutorialThread.class 1KB
HelpView$TutorialThread.class 1KB
HelpView$TutorialThread.class 1KB
WinView$TutorialThread.class 1KB
WinView$TutorialThread.class 1KB
ChangeBullet.class 1KB
ChangeBullet.class 1KB
Life.class 1KB
Life.class 1KB
WelcomeViewThread.class 1KB
WelcomeViewThread.class 1KB
Explode.class 1KB
Explode.class 1KB
ConstantUtil.class 913B
ConstantUtil.class 913B
PlaneActivity$1$2.class 904B
PlaneActivity$1$2.class 904B
PlaneActivity$1$1.class 895B
PlaneActivity$1$1.class 895B
PlaneActivity$2.class 720B
PlaneActivity$2.class 720B
R$raw.class 484B
R$raw.class 484B
R.class 456B
R.class 456B
R$string.class 394B
R$string.class 394B
R$layout.class 361B
R$layout.class 361B
R$attr.class 307B
R$attr.class 307B
.classpath 288B
classes.dex 45KB
GameView.java 13KB
WelcomeView.java 10KB
Maps.java 6KB
PlaneActivity.java 5KB
Plane.java 5KB
ProcessView.java 5KB
MoveThread.java 5KB
EnemyPlane.java 4KB
R.java 4KB
FailView.java 4KB
WinView.java 4KB
HelpView.java 4KB
KeyThread.java 3KB
Bullet.java 2KB
GameViewBackGroundThread.java 2KB
ChangeBullet.java 1KB
Life.java 1KB
WelcomeViewThread.java 1KB
ConstantUtil.java 1KB
ExplodeThread.java 1KB
Explode.java 819B
battleback.jpg 35KB
background2.jpg 17KB
bg.jpg 12KB
共 226 条
- 1
- 2
- 3
资源评论
荣华富贵8
- 粉丝: 223
- 资源: 7653
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 注塑技术员试题及答案.doc
- 自学考试房地产开发和经营重点.doc
- 江苏镇江市2018年中考语文试题答案和解析.doc
- 精神病学试题与答案.doc
- 教育行动研究报告的写作.doc
- 老年人常见疾病的护理知识.doc
- 考试后激励学生的话.doc
- 廉洁文化主题教育课教学案.doc
- 贫困家庭申请书范文(精选多篇).doc
- 培训机构教学计划.doc
- 全新版大学英语综合教程3contentquestions答案.doc
- 全科医师转岗培训理论考试题和正确答案.doc
- 全国居民健康素养知识问卷80题及答案.doc
- 服装公司薪酬福利管理手册.docx
- 服装薪酬体系-KPI绩效考核指标.xls
- 各岗位KPI绩效考核指标——服装生产企业.xls
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功