整体实现思路

绘制循环滚动的背景图片创建BackGround类
绘制飞机和子弹.创建Myplane和Bullet类
在Myplane中构造isCollision绘制飞机与boss飞机的碰撞,飞机与子弹的碰撞
绘制爆炸效果创建boom类
添加音效创建GameSoundpool添加飞机发射子弹的音效和爆炸音效

绘制循环滚动的背景图片

建一个Background类,用draw方法和logic方法使两张图片紧密连接在一起,当第一张图片小于MySurfaceView高度时另一张图片接着循环

[java] view plain copy
1.package com.example.a29148.myapplication;
2.
3.import android.graphics.Bitmap;
4.import android.graphics.Canvas;
5.import android.graphics.Paint;
6.
7.public class BackGround {
8.    private int y1;
9.    private  int y2;
10.    private Bitmap bitmap;
11.    public BackGround(Bitmap bitmap){
12.        this.bitmap = bitmap;
13.        y1 = 0;
14.        y2 = y1-bitmap.getHeight();
15.    }
16.    public void draw(Canvas canvas){
17.
18.        logic();
19.        Paint paint = new Paint();
20.        canvas.drawBitmap(bitmap,0,y1,paint);
21.        canvas.drawBitmap(bitmap,0,y2,paint);
22.    }
23.    public void logic(){
24.        y1+=5;
25.        y2+=5;
26.        if(y1>=MySurfaceView.Height){
27.            y1=y2-bitmap.getHeight();
28.        }
29.        if(y2>=MySurfaceView.Height){
30.            y2=y1-bitmap.getHeight();
31.        }
32.    }
33.}  

绘制飞机和子弹.

绘制飞机:创建Myplane,draw方法并在MySurfaceView传递参数,在后面还定义了飞机的血量,还定义了一个布尔型的noCollision使手指可以拖动飞机,并判断飞机是否被boss飞机发射的子弹击中和被boss飞机的疯狂模式撞到减少血量;当飞机的血量小于等于零时,判定游戏失败

[java] view plain copy
1.package com.example.a29148.myapplication;
2.
3.import android.graphics.Bitmap;
4.import android.graphics.Canvas;
5.import android.graphics.Paint;
6.
7.public class BackGround {
8.    private int y1;
9.    private  int y2;
10.    private Bitmap bitmap;
11.    public BackGround(Bitmap bitmap){
12.        this.bitmap = bitmap;
13.        y1 = 0;
14.        y2 = y1-bitmap.getHeight();
15.    }
16.    public void draw(Canvas canvas){
17.
18.        logic();
19.        Paint paint = new Paint();
20.        canvas.drawBitmap(bitmap,0,y1,paint);
21.        canvas.drawBitmap(bitmap,0,y2,paint);
22.    }
23.    public void logic(){
24.        y1+=5;
25.        y2+=5;
26.        if(y1>=MySurfaceView.Height){
27.            y1=y2-bitmap.getHeight();
28.        }
29.        if(y2>=MySurfaceView.Height){
30.            y2=y1-bitmap.getHeight();
31.        }
32.    }
33.}  

绘制子弹:

创建一个Bulle类,定义speed方法给子弹飞行速度赋值,用switch语句把子弹分为玩家子弹和boss子弹,并定义一个布尔型方法isOut,移除飞出屏幕的子弹

[java] view plain copy
1.package com.example.a29148.myapplication;
2.
3.        import android.graphics.Bitmap;
4.               import android.graphics.Canvas;
5.               import android.graphics.Paint;
6.
7.public class Bullet {
8.    private Bitmap bitmap;
9.    private int x,y;
10.    private int speed=20;
11.    private boolean isOut;
12.    private int type;
13.    public Bullet(Bitmap bitmap,int x,int y,int type) {
14.        this.bitmap = bitmap;
15.        this.x = x;
16.        this.y = y;
17.        this.type = type;
18.    }
19.    public void draw(Canvas canvas, Paint paint){
20.        canvas.drawBitmap(bitmap,x,y,paint);
21.        lg();
22.    }
23.    private void lg(){
24.
25.        switch (type){
26.            //玩家子弹
27.            case 0:
28.                y-=speed;
29.                if(y<0){
30.                    isOut = true;
31.                }
32.                break;
33.            case 1:
34.                //Boss子弹
35.                y+=speed;
36.                if (y<0){
37.                    isOut = true;
38.                }
39.
40.                break;
41.            default:
42.                break;
43.
44.        }
45.
46.
47.
48.    }
49.
50.    public boolean isOut() {
51.        return isOut;
52.    }
53.
54.    public Bitmap getBitmap() {
55.        return bitmap;
56.    }
57.
58.    public int getX() {
59.        return x;
60.    }
61.
62.    public int getY() {
63.        return y;
64.    }
65.
66.    public void setOut(boolean out) {
67.        isOut = out;
68.    }
69.}  

判断碰撞:

在Myplane类和bossplane定义一个isCollision方法并定义布尔型noCollision变量,判断子弹的位置是否落在飞机所在的xy轴位置,如果是就移除子弹并减少血量

[java] view plain copy
1.public boolean isCollision(BossPlane bossPlane) {
2.        if (noCollision) {
3.            return false;
4.        } else {
5.
6.            if (bossPlane.getY() + bossPlane.getH() > y && bossPlane.getY() + bossPlane.getH() < y + height) {
7.                if (x < bossPlane.getX() && x + width > bossPlane.getX()) {
8.                    noCollision = true;
9.                    if (hp >= 0) {
10.                        hp--;
11.                    }
12.                    return true;
13.                }
14.                if (x>bossPlane.getX()&&x+width<bossPlane.getX()+bossPlane.getX()){
15.                    noCollision = true;
16.                    if (hp > 0) {
17.                        hp--;
18.                    }
19.                    return true;
20.                }
21.                if(x<bossPlane.getX()&&x+width>bossPlane.getX()+bossPlane.getW()){
22.                    noCollision = true;
23.                    if (hp > 0) {
24.                        hp--;
25.                    }
26.                    return true;
27.                }
28.
29.
30.            }
31.            return false;
32.        }
33.    }  

绘制爆炸效果:

创建一个Boom类,定义两个int型变量crrentFrame和totalFrame,和一个布尔型变量isOut并想MySurfaceView里面传递参数用canvas.clipRect剪切图片,在用canvas.clipRect时要用canvas.save和canvas.restore当currentFrame小于totalFrame时产生爆炸效果

[java] view plain copy
1.package com.example.a29148.myapplication;
2.
3.        import android.graphics.Bitmap;
4.        import android.graphics.Canvas;
5.        import android.graphics.Paint;
6.
7.public class Boom {
8.    private Bitmap bitmap;
9.    private int x,y;
10.    private int currentFrame;//当前显示的第几副画面
11.    private int totalFrame;
12.    private int frameW,frameH;
13.    private boolean isOut;
14.
15.    public Boom(Bitmap bitmap, int x, int y, int totalFrame) {
16.        this.bitmap = bitmap;
17.        this.x = x;
18.        this.y = y;
19.        this.totalFrame = totalFrame;
20.        frameH = bitmap.getHeight();
21.        frameW = bitmap.getWidth()/totalFrame;
22.    }
23.    public void draw(Canvas canvas, Paint paint){
24.        canvas.save();
25.        canvas.clipRect(x,y,x+frameW,y+frameH);
26.        canvas.drawBitmap(bitmap,x-currentFrame*frameW,y,paint);
27.        canvas.restore();
28.        lg();
29.    }
30.    public void lg(){
31.        if(currentFrame<totalFrame){
32.            currentFrame++;
33.        }else {
34.            isOut = true;
35.        }
36.    }
37.
38.    public boolean isOut() {
39.        return isOut;
40.    }
41.}

添加音效:

创建一个GameSoundpool类,创建一个palySound方法在里面使用switch语句实现音效的快慢并传参到MySurfaceView类中


[java] view plain copy
1.package com;
2.
3.import android.content.Context;
4.import android.media.AudioManager;
5.import android.media.SoundPool;
6.
7.import com.example.a29148.myapplication.R;
8.
9.public class GameSoundpool {
10.    private SoundPool soundPool;
11.    private int s1;
12.    private int s2;
13.    public GameSoundpool(Context context) {
14.        this.soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC,0);
15.        s1 = soundPool.load(context, R.raw.shoot,1);
16.        s2 = soundPool.load(context, R.raw.explosion2,1);
17.    }
18.    public void playSound(int s){
19.        switch (s){
20.            case 1:
21.                soundPool.play(s1,1,1,0,1,1.0f);
22.                break;
23.            case 2:
24.                soundPool.play(s2,1,1,0,1,1.0f);
25.                break;
26.        }
27.
28.
29.    }
30.}  

用到的方法:

封装:飞机的x和y坐标,boss的坐标MySurfaceView中的canvas等都是用到了封装

继承:MySurfaceView继承了SurfaceView

方法重载:Myplane中的碰撞类中存在两碰撞就是方法的重载,一个是飞机和子弹的碰撞,一个是飞机和boss飞机的碰撞

[java] view plain copy
1.public boolean isCollision(Bullet bullet) {
2.       if (noCollision) {
3.           return false;
4.
5.
6.       } else {
7.           if (bullet.getX() > x && bullet.getX() < x + width && bullet.getY() > y && bullet.getY() < y + height) {
8.               Log.e("AAA", "isCollision: .................................");
9.               noCollision = true;
10.               if (hp > 0) {
11.                   hp--;
12.               }
13.               return true;
14.           }
15.       }
16.       return false;
17.   }
18.
19.   public boolean isCollision(BossPlane bossPlane) {
20.       if (noCollision) {
21.           return false;
22.       } else {
23.
24.           if (bossPlane.getY() + bossPlane.getH() > y && bossPlane.getY() + bossPlane.getH() < y + height) {
25.               if (x < bossPlane.getX() && x + width > bossPlane.getX()) {
26.                   noCollision = true;
27.                   if (hp >= 0) {
28.                       hp--;
29.                   }
30.                   return true;
31.               }
32.               if (x>bossPlane.getX()&&x+width<bossPlane.getX()+bossPlane.getX()){
33.                   noCollision = true;
34.                   if (hp > 0) {
35.                       hp--;
36.                   }
37.                   return true;
38.               }
39.               if(x<bossPlane.getX()&&x+width>bossPlane.getX()+bossPlane.getW()){
40.                   noCollision = true;
41.                   if (hp > 0) {
42.                       hp--;
43.                   }
44.                   return true;
45.               }
46.
47.
48.           }
49.           return false;
50.       }
51.   }

接口:MySurfaceView中的SurfaceHolder.Callback和Rnnable都是接口


[java] view plain copy
1.public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
2.
3.    private SurfaceHolder surfaceHolder;
4.    private Canvas canvas;//绘制图形的画布
5.    private boolean isDrawing = true;
6.    public static int Height;
7.    public static int Wide;
8.    private MyPlane plane;
9.    private Vector<Bullet> bulletVector = new Vector<>();//Vector是线程安全的,ArrayList是非线程安全的
10.    private Vector<Bullet> boosBulletVector = new Vector<>();
11.    private Vector<Boom>boomVector = new Vector<>();
12.    private int count;
13.    private GameSoundpool gameSoundpool;
14.    public static int Game_STATE=3;  

收获与感悟

在这实训的一个月是这个学期中最充实的一个月(虽然会很累,比如绞尽脑汁却想不出代码时),在这个月中从最基础的Java基础数据类型类和对象到封装多态继承File文件类IO流再到Android studio,对于我这个在上实训课之前只会写“public class Test{}”的人来说能够上这个实训课真是莫大的荣幸!第一二周我们每天学习基础知识了第二天早上在复习前面学的知识点,让我这个纯小白也有了对学习Java的信心。
这个学习开学时在第一次上Java课的时候我就想着什么时候我们才能写出一个像样的代码,可是在学了一段时间之后我感觉我好像被那个不是这块料。老师每天讲的课我都是晕头转向,每次写完代码都有很多红线在嘲讽我,那段时间过得很迷。再知道接下来一个月的实训课的消息时,我也感受到过绝望。我很怕这一个月和之前一样看着代码发呆,那种想学却不知道如何学的感觉给我很大的挫败感,让人无地自容的感觉。直到上实训课的第一天我带着惶恐不安和意思希望来到了教室,第一节课老师告诉我们只要你想学我便竭尽全力的的教你,在第一周老师讲的最基础的知识却用最慢的节奏。这一周我们不断的学习复习,随着学的知识积累,使我对这门课又有了信心。
第二周我们做了一个图书管理系统的项目,从中学习ArrayList、scanner以及复习前所学的基础知识,使我们对学到的知识进一步掌握和使用,最后两周老师是让我们用Android studio做一个飞机大战小游戏,从如何安装及使用Android studio开始一点一点慢慢学习每天写一点每一步对我来说都是有些难因为英语很差只有靠着翻译才能勉强维持生活。每天写的代码我觉得写起来比以前轻松了一些,通过老师的讲解和同学的帮助今天终于把这个飞机大战游戏写完了。在我的手机上运行出来的时候,真的很兴奋。这是我最开始学习Java的第一个目标今天终于实现了。
这在实训之前感觉已经没有希望的事情终于实现了,我觉得在这个月中我最大的收获不仅仅是知识;这个月我学到最有要的东西便是信心,对自己的信心。只要艰苦的劳动和正确的方法我一定可以做到!

Android studio飞机大战游戏分析-月末总结相关推荐

  1. 基于Android的飞机大战游戏的设计与实现

    在2007年11月5日谷歌公司发布了一款全新的面向智能移动端设备的操作系统,这就是Android.经历了几年市场的洗礼,Android凭借其优异的性能占据了大部分智能手机市场.根据最新的调查显示,An ...

  2. Android Studio 飞机大战

    1.整体实现思路 实现飞机大战的简单思路: ①要先建MySurfaceView这个类,在这个类中绘制画布,有画布的高度和宽度,还有画笔,然后在这个建好的画布上开始继续"作画"画布利 ...

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

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

  4. 鸿蒙开发实例 | ArkUI JS飞机大战游戏开发

    本篇介绍使用ArkUI JS框架开发一款基于鸿蒙操作系统的飞机大战游戏.这款飞机大战游戏是在普通单机游戏的基础上添加了鸿蒙操作系统分布式支持,使游戏可以同时使用多台鸿蒙操作系统设备,为游戏玩家提供分布 ...

  5. 微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js)

    微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞 ...

  6. python飞机大战概要设计_飞机大战游戏开发文档(Android版)

    飞机大战游戏 开发文档 (Android版) 课程名称:飞机大战游戏 课程类型:Android游戏编程精彩内容,尽在百度攻略:https://gl.baidu.com 姓名:苏均灿 学号:131342 ...

  7. 微信5.0 Android版飞机大战破解无敌模式手记

    微信5.0 Android版飞机大战破解无敌模式手记 转载: http://www.blogjava.net/zh-weir/archive/2013/08/14/402821.html 微信5.0 ...

  8. 基于pygame实现的飞机大战游戏

    目录 1.引言 1.1 背景 1.2 意义 1.3 功能 2.系统结构 2.1 整体框架 2.2 精灵与精灵组 2.3 功能介绍 2.3.1 玩家飞机 2.3.2 敌机类型和关卡设定 2.3.3 敌机 ...

  9. Python实验,用pygame做飞机大战游戏设计

    飞机大战游戏设计 摘 要:根据课程要求,以及面向对象程序设计的编程思想,在Windows操作系统环境下,运用PyCharm编译程序,以Python语言为开发语言,最终实现飞机大战游戏相应的游戏操作功能 ...

最新文章

  1. secureCRT 实现windows和linux文件互传
  2. 可能是把Docker的概念讲的最清楚的一篇文章
  3. 【机器学习】干货!机器学习中 5 种必知必会的回归算法!
  4. pack php 详解_函数pack的使用详解
  5. StringBuffer 案例
  6. repeater 时间格式化
  7. BLE-NRF51822教程1-常用概念
  8. 【Matlab】根据图生成带权邻接矩阵,并求出最短路径
  9. 信息学奥赛C++语言:十位能被个位和百位之和整除
  10. android学习的java,android学习之java常识
  11. 网站二次开发如何防止别人打包源码_企业网站制作前需注意的几个事项
  12. 就算给穷人一百万,他们也很难赚到一块钱利润
  13. Python高级知识点学习(一)
  14. Linux经常使用到的操作
  15. 2020Java程序设计基础(华东交通大学)章节测试免费满分答案
  16. 最新中国上市公司市值500强(2021年)
  17. 计算机计算器logo,计算器品牌哪个比较好(卡西欧stylish计算器入手体验)
  18. 数码管点亮中几个常见三极管基极导通状态
  19. 微信转盘抽奖前端源码(二):12个奖品,指针初始指向奖品中间
  20. 如何判断域名是否被墙?域名被墙怎么解决?

热门文章

  1. 【C++】公积金贷款计算器
  2. P1567统计天数-C++编程解析-数组
  3. 实战:Traefik 高级配置3-2022.1.23
  4. 第一次将项目push到gitlab
  5. [原]解密Airbnb 自助BI神器:Superset 颠覆 Tableau
  6. macM1下PD虚拟机中ubuntu安装git过程中apt-get update失败、E: 部分索引文件下载失败等问题
  7. [Vijos]P1788 第k大
  8. 反向放大器为何要使用同相增益(也称作噪声增益),来计算带宽
  9. 台式计算机怎么加一个硬盘,台式机加装一个机械硬盘图解 但建议直接在windows下...
  10. NLP系列笔记:通俗理解LDA主题模型