飞机大战

一.整体实现思路

首先选好背景,然后设置背景,实现两张背景的循环,达成背景的循环滚动。
第二步,加上一个玩家飞机,首先给他赋予初始位置,然后给玩家飞机Touch,实现玩家飞机能按照手指的触摸位置来进行移动。
第三步,Boss飞机,相同的道理,只不过少了触摸移动,在背景的上方给Boss飞机进行判断,实现飞机的左右移动。
第四步,给飞机加上子弹,玩家飞机子弹和boss飞机子弹一样,先创建一个speed给它赋值,是子弹的速度,用speed++进行子弹的加速,再定义一个ISDEAD,进行子弹的判断。
第五步,给子弹加声音,在子弹加声音的时候要先进行判断,判断当子弹撞到飞机时产生的反应,飞机撞到飞机时候的反应,如玩家飞机和Boss飞机的血量都会减少,和玩家飞机会有无敌的效果,然后就是赋予碰撞时的声音。

二.如何绘制循环滚动的背景图片

首先定义图片的y,y1,Bitmap,给这三个值赋予构造方法,然后在逻辑方法中进行判断,给y和y1进行++实现背景的滚动,然后会发现两张背景不会衔接到一起,这时就要换一种判断方法,当第一张图循环结束,把第二张图放在后面继续进行滚动,第一张图立马放到第二张图后面接着循环,这样就会有一种一直在循环的感觉。
下面是背景这一部分的代码:

/*** 背景*/import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;public class BackGroud {private int y1;private int y2;private  Bitmap bitmap;public BackGroud(Bitmap bitmap){this.bitmap = bitmap;y1 = 0;y2 = y1-bitmap.getHeight();}public void draw(Canvas canvas){logic();Paint paint = new Paint();canvas.drawBitmap(bitmap,0, y1,paint);canvas.drawBitmap(bitmap,0, y2,paint);}public void logic(){ //逻辑方法y1+=10;y2+=10;if(y1>=bitmap.getHeight()){y1 = y2-bitmap.getHeight(); //移动在第二张上面}if(y2>=bitmap.getHeight()){y2=y1-bitmap.getHeight();}}}

三.如何绘制飞机

首先给飞机定义一个初始位置,在屏幕的底面中间,给飞机X,Y值定义成屏幕的X最下方以及Y的中间,但是飞机并不在屏幕中间,因为这里的XY是飞机和屏幕的左上角的位置,这时的飞机在中间偏右这个位置,所以必须要减去半个飞机的宽度(这里是飞机的宽度,而不是X的一半)才能到达正中心,然后就是给飞机定义一个初始血量Hp为3,其次就是无敌状态,然后用Canvas的方法调用到飞机的图片,最后再在主类中调用Myplane中的draw。
代码如下:

/**
*玩家飞机
*/
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;public class Myplane {private Bitmap bitmap;private Bitmap bitmapHp;private int x, y;private int width, height;private boolean noCollision;private int noCollisionCount;private int Hp = 3;public Myplane(Bitmap bitmap, Bitmap bitmapHp) {this.bitmapHp = bitmapHp;this.bitmap = bitmap;x = MySurfaceView.width / 2 - bitmap.getWidth() / 2;//中间y = MySurfaceView.height - bitmap.getHeight();width = bitmap.getWidth();height = bitmap.getHeight();}public void draw(Canvas canvas, Paint paint) {if(Hp<=0){MySurfaceView.Game_start = 3;}if (noCollision) {noCollisionCount++;if (noCollisionCount % 10 == 0) {canvas.drawBitmap(bitmap, x, y, paint);//闪烁:无敌状态}if (noCollisionCount > 100) { //无敌时间noCollision = false;noCollisionCount = 0;}} else {canvas.drawBitmap(bitmap, x, y, paint);//非无敌状态}for (int i = 0; i < Hp; i++) {canvas.drawBitmap(bitmapHp, i * bitmapHp.getWidth(), MySurfaceView.height - bitmapHp.getHeight(), paint);//HP位置}}public void touchEvent(MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_MOVE) {float ex = event.getX();float ey = event.getY();if (ex > x && ex < x + width && ey > y && ey < y + height) {x = (int) (ex - width / 2);y = (int) (ey - height / 2);if (y < 0) {y = 0;}if (y + height > MySurfaceView.height) {y = MySurfaceView.height - height;}}}}//我方飞机与boss子弹public boolean isCollision(Bullet bullet) {if (noCollision) {return false;}if (bullet.getX() > x && bullet.getX() < x + width && bullet.getY() > y && bullet.getY() < y + height) {noCollision = true;if (Hp > 0) {Hp--;}return true;}return false;}//我方飞机与bosspublic boolean Attack(Bossplane bossplane) {if (noCollision) {return false;}if(y<bossplane.getY()+bossplane.getFrameH()&&y+height>bossplane.getY() ){if (x < bossplane.getX() && x+width > bossplane.getX()) {//我方战机左边碰撞noCollision = true;if (Hp > 0) {Hp--;}return true;}if (x > bossplane.getX() && x+width < bossplane.getX() + bossplane.getFrameW()) {//我方飞机中间碰撞noCollision = true;if (Hp > 0) {Hp--;}return true;}if (x < bossplane.getX() && x+width > bossplane.getX()+bossplane.getFrameW()) {//我方飞机右边碰撞noCollision = true;if (Hp > 0) {Hp--;}return true;}}return false;}public int getX() { return x; }public int getY() { return y; }public int getWidth() {return width;}public int getHeight() {return height;}
}

四.如何绘制子弹

三要素与构造方法和之前一样,就是多了一个speed,给子弹赋予速度,还有子弹消失,否则,子弹打出去消失不了。
其中主要代码为:

public void logic() {switch (type){//玩家子弹case 0:y -= speed;if (y < 0) {isDead = true;}break;//boos子弹case 1:y += speed;if (y < 0) {isDead = true;}break;}

当然不可能只有这么多,这只是部分代码,最后的代码会在最后公布。

五.如何判断碰撞

1.飞机与子弹碰撞

主要类MySurfaceView,这个类主要是要先实现继承和接口,然后碰撞主要的内容就是判断,当飞机的坐标和子弹的坐标重合的时候,玩家飞机的血量就会减少,来体现玩家飞机已经碰撞到了子弹,这需要好几个判断,首先Boss子弹和玩家飞机碰撞时的判断,当子弹的右边碰上飞机的左边并子弹的左边小于飞机的左边,子弹的右边小于飞机的右边,当子弹的右边大于飞机的左边并子弹的左边小于飞机的右边这个时候就是飞机与子弹的碰撞。
飞机与子弹的碰撞代码如下:

//我方飞机与boss子弹public boolean isCollision(Bullet bullet) {if (noCollision) {return false;}if (bullet.getX() > x && bullet.getX() < x + width && bullet.getY() > y && bullet.getY() < y + height) {noCollision = true;if (Hp > 0) {Hp--;}return true;}return false;}

2.玩家飞机与Boss飞机碰撞

然后玩家飞机和Boss飞机的碰撞,判断的条件与子弹碰撞飞机一样。
飞机碰撞飞机代码如下:

//我方飞机与bosspublic boolean Attack(Bossplane bossplane) {if (noCollision) {return false;}if(y<bossplane.getY()+bossplane.getFrameH()&&y+height>bossplane.getY() ){if (x < bossplane.getX() && x+width > bossplane.getX()) {//我方战机左边碰撞noCollision = true;if (Hp > 0) {Hp--;}return true;}if (x > bossplane.getX() && x+width < bossplane.getX() + bossplane.getFrameW()) {//我方飞机中间碰撞noCollision = true;if (Hp > 0) {Hp--;}return true;}if (x < bossplane.getX() && x+width > bossplane.getX()+bossplane.getFrameW()) {//我方飞机右边碰撞noCollision = true;if (Hp > 0) {Hp--;}return true;}}return false;}

6.如何绘制爆炸效果

爆炸效果是用到了图片的裁剪就是Canvas的裁剪,在裁剪之前要写上save和restore方法,裁剪主要是通过裁剪图片宽度的W,高度一样所以可以不用裁剪,用W来除以总个数。
代码如下:

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;public class Boom {private Bitmap bitmap;private int x,y;private int totalFrame;private int currentFrame;private int frameW,frameH;private  boolean isEnd;public Boom(Bitmap bitmap, int x, int y, int totalFrame) {this.bitmap = bitmap;this.x = x;this.y = y;this.totalFrame = totalFrame;frameW = bitmap.getWidth()/totalFrame;frameH = bitmap.getHeight();}public void draw(Canvas canvas, Paint paint){canvas.save();canvas.clipRect(x,y,x+frameW,y+frameH);canvas.drawBitmap(bitmap,x-currentFrame*frameW,y,paint);canvas.restore();logic();}public void  logic(){if(currentFrame<totalFrame){currentFrame++;}else{isEnd = true;}}public boolean isEnd() {return isEnd;}
}

7.如何添加音效

在飞机中音效用到了新的调用:this.soundPool,先实例化,选择音乐有优先级别。然后用一个s来实现它,方便后面的调用,其中this.soundPool方法的括号里有六个位置,第一个是上面的s,第二个和第三个是左右声道,第四个是优先级别,第五个直接输入1,第六个是速率,你播放的音乐的速率。
以下是音乐的代码:

package com.example.lenovo.myapplication;import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;public class GameSoundPool {private SoundPool soundPool;private int s1;private int s2;public GameSoundPool(Context context){this.soundPool = new SoundPool(2,AudioManager.STREAM_MUSIC,0);this.soundPool = new SoundPool(1,AudioManager.STREAM_MUSIC,0);s1 = soundPool.load(context,R.raw.shoot,1);s2 = soundPool.load(context,R.raw.button,1);}public void playSound(){soundPool.play(s1,1,1,1,1,1.0f);soundPool.play(s2,1,1,1,1,1.0f);}
}

8.哪些地方用到封装,继承,多态,方法重载,接口等

1.继承和接口:

继承和接口就MySurfaceView中实现,这个类要继承SurfaceView,还要实现SurfaceHolder.Callback,Runnable的接口。

2.方法重载

在飞机和子弹,飞机与飞机的碰撞中定义了同一个类名,然而参数列表不一样,调用的时候,就按照里面的参数来进行传参。

3.多态

4.封装

9.我的收获和感悟

飞机大战学习中很累,打代码时有时打错要整行的删,但当运行出来的时候还是很有成就感的

Android 飞机大战相关推荐

  1. android飞机大战

    Android飞机大战 简述 android游戏继承surfaceView开发,实现Callback接口,Callback接口作用是回调. 实现Callback接口后,要实现三个抽象方法:surfac ...

  2. Android 飞机大战详解与感悟

    一.飞机大战的整体思路: 飞机大战的主要使用的方法大纲: 1.概略: Android的飞机大战用的是SurfaceView()来写,那么首先我们要继承SurfaceView这个类,然后我们还需要用多线 ...

  3. android飞机大战功能,安卓飞机大战(二) SurfaceView实现自制背景

    用SurfaceView写一个自制的背景图,并且可以移动,加上安卓飞机大战(一)中的BackgroundManager类,可以直接使用 GameView代码: public class GameVie ...

  4. Android 飞机大战 一

    飞机大战所需要的图片 飞机大战所需要的音效 把图片粘贴到res里面的mipmap里 把音乐粘贴到res里的raw里   (raw要新建)

  5. android飞机大战实验过程报告,飞机大战实验报告.docx

    <飞机大战实验报告.docx>由会员分享,可在线阅读,更多相关<飞机大战实验报告.docx(12页珍藏版)>请在装配图网上搜索. 1.飞机大战实验报告专业:网络工程132班学号 ...

  6. android飞机大战实验过程报告,飞机大战实验报告.doc

    . . 飞机大战实验报告 专业:网络工程132班 学号:139074298 姓名:孙 仁 强 计算机科学与技术学院 二零一六年十二月 一.软件运行所需要的软硬件环境? 本系统是以Windows系统为操 ...

  7. android飞机大战项目描述,Android studio 飞机大战项目思路和代码

    整体思路 先背景 绘制玩家飞机,玩家飞机随触摸点移动 绘制boss飞机,自动移动 绘制子弹,分别从玩家飞机和boss飞机发射 绘制爆装效果,由子弹触发 绘制血量,玩家血量为0触发死亡画面,boss血量 ...

  8. android飞机大战功能,Android飞机大战

    点击进行安装下载:https://www.pgyer.com/gUk0 快来玩一下 MainActivity package com.example.administrator.aircraftwar ...

  9. android 飞机大战背景图片,安卓飞机大战(一) 背景移动

    在制作游戏时,背景可以移动,原理就是两张图片的循环移动. package com.example.backgroundtest; import com.example.hundouluo.R; imp ...

最新文章

  1. pandas内置数据集_pandas内置数据集_Pandas中的示例数据集
  2. 论文阅读笔记四十:Deformable ConvNets v2: More Deformable, Better Results(CVPR2018)
  3. thinkphp5.1嵌套关联预载入的写法
  4. 一种基于超体素结合粒子群与模糊聚类实现点云分割的优化算法
  5. 递归调用方法时栈内存是如何变化的?(使用内存图演示递归调用过程)
  6. python 类变量(属性)和实例变量(属性
  7. Linux学习之C语言的进程与线程编程
  8. java程序中单方法接口通常是,Java基础知识整理
  9. 7-9 用天平找小球
  10. 安装 xadmin报错 Command “python setup.py egg_info“ failed with error code 1 in C:\Users\ADMINI~1\AppDat
  11. installShield_script学习
  12. JS数字转大写 (会计最爱)
  13. 走近棒球运动·科罗拉多落基队·MLB棒球创造营
  14. cordova 实现第三方登录及分享,qq,微信,微博
  15. Mongodb的集群
  16. Maven failed to download xxx.jar
  17. 使用sofa-common-tools自定义日志
  18. JAVA 图片地址路径转换 Base64 工具类
  19. 软考高级 真题 2012年下半年 信息系统项目管理师 综合知识
  20. 常用校验注解@NotEmpty,@NotBlank,@NotNull,@Valid,@Validated用法区别,以及搭配 BindingResult使用,嵌套验证等《使用|CSDN创作打卡》

热门文章

  1. 童年经典回忆 | 从零开始带大家用Python撸一个魔塔小游戏呀(2)
  2. Ubantu服务器创建多个用户,多个用户互不干扰
  3. 结构光资料 | 分享几个结构光方向研究者的主页
  4. 首届“中科杯”全国软件设计大赛拉开帷幕
  5. PHP模块加载失败:PHP Warning: Module ‘modulename‘ already loaded in Unknown on line 0
  6. 面向虚拟现实更新旧版 Unity* 游戏
  7. 软件设计模式之单例模式-----身份证号码---打印池
  8. 计算机excel混合引用,Excel公式填充:绝对引用混合引用要分清
  9. 智能社官网顶部导航实现demo
  10. Windows命令行复制文件