Android Studio 飞机大战

1.整体实现思路

实现飞机大战的简单思路:

①要先建MySurfaceView这个类,在这个类中绘制画布,有画布的高度和宽度,还有画笔,然后在这个建好的画布上开始继续“作画”画布利用了canvas实现画笔利用paint实现

②然后将飞机大战的部分闯关背景“画”上画布,实现绘图操作,利用多线程的方法,调用runable方法并用stater启动子线程。记得解锁画布,显示到屏幕上。

③选择背景图片,建立BackGround类,利用两张相同背景图片利用if循环语句实现背景图片的移动。并在MySurfaceView中调用。

④建立Myplane和BossPlane类,利用if语句for循环onTouchEventisCollisionnoisCollisionnoisCollisioncount等实现飞机的飞行移动,并可以实现屏幕触摸,还创建hp,来实现飞机血量的减少。还要利用isCrazytimecount实现boss飞机的疯狂模式时间。并在MySurfaceView中调用。

并在MySurfaceView中调用。

⑥创建Boom类,利用clipRect裁剪图片以及totalFrame来实现爆炸现象currenFrame用来显示当前显示的第几幅画。最后记得结束爆炸。并在MySurfaceView中调用。

⑦添加结束游戏输赢的图片,利用SoundPool,switch语句实现,利用RectF完成图片的大小与屏幕吻合,还有调用GAEM_STATE和switch在MySurfaceView中调用实现图片的运用。


2.如何实现循环滚动的背景图片

利用两张相同背景图片,画好背景图片的坐标即大小,利用if语句判断实现两张图片的循环滚动。

package com.example.jinxin.myapplication;  
  
import android.graphics.Bitmap;  
import android.graphics.Canvas;  
import android.graphics.Paint;  
  
/** 
 * Created by lenovo64 
 */  
  
public class BackGround {  
    private int y1;  
    private  int y2;  
    private  Bitmap bitmap ;  
    public BackGround( Bitmap bitmap){  
        this.bitmap = bitmap;  
        y1 = 0;  
        y2 = y1 - bitmap.getHeight();  
  
    }  
  
  
  
    public void draw(Canvas canvas,Paint paint){  
        logic();  
        canvas.drawBitmap(bitmap,0,y1,paint);  
        canvas.drawBitmap(bitmap,0,y2,paint);  
  
    }  
  
        public void logic(){  
            y1+=5;  
            y2+=5;  
            if (y1>MySurfaceView.height){  
                y1 = y2 - bitmap.getHeight();//移动到第二张图片的顶部  
  
            }  
            if (y2>MySurfaceView.height){  
                y2 = y1 - bitmap.getHeight();  
  
            }  
  
        }  
  
}  

3.如何绘制飞机

建立Myplane和BossPlane类,利用if语句和for循环和onTouchEvent和isCollision和noisCollision和noisCollisioncount等实现飞机的飞行移动,并可以实现屏幕触摸,还创建hp,来实现飞机血量的减少。还要利用isCrazy,time,count实现boss飞机的疯狂模式时间。并在MySurfaceView中调用。

MyPlane:

package com.example.jinxin.myapplication;  
  
import android.graphics.Bitmap;  
import android.graphics.Canvas;  
import android.graphics.Paint;  
import android.graphics.YuvImage;  
import android.util.Log;  
import android.view.MotionEvent;  
  
public class MyPlane {  
  
    private Bitmap bitmap;  
    private int x, y;  
    private static int width, height;  
    private int fx;  
  
    private boolean noCollision;  
    private int noCollisionCount;//碰撞计数器  
    private Bitmap bitmapHp;  
    private int hp = 3;  
  
  
    public MyPlane(Bitmap bitmap, Bitmap bitmapHp) {  
        this.bitmap = bitmap;  
        x = MySurfaceView.width / 2 - bitmap.getWidth() / 2;  
        y = MySurfaceView.height - bitmap.getHeight();  
        width = bitmap.getWidth();  
        height = bitmap.getHeight();  
        this.bitmapHp = bitmapHp;  
  
  
    }  
  
  
    public void draw(Canvas canvas, Paint paint) {  
        if (hp<=0){  
            MySurfaceView.GAME_STATE =3;  
  
        }  
  
        if (noCollision) {  
            noCollisionCount++;  
            if (noCollisionCount % 10 == 0) {  
                Log.e("&&&&&", "****");  
                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);  
  
        }  
  
    }  
  
  
    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;  
                }  
            }  
            if (x < 0) {  
                x = 0;  
            }  
            if (x + width > MySurfaceView.width) {  
                y = MySurfaceView.width - width;  
            }  
  
        }  
  
    }  
  
    public boolean isCollision(Bullet bullet) {  
        if (noCollision) {  
            return false;  
        } else {  
            if (bullet.getX() > x && bullet.getX() < x + width && bullet.getY() > y && bullet.getY() < y + height) {  
                Log.e("***", "------------------");  
                noCollision = true;  
                if (hp > 0) {  
                    hp--;  
                }  
                return true;  
            }  
  
  
        }  
        return false;  
    }  
  
    public boolean isCollision(BossPlane bossPlane) {  
        if (noCollision) {  
            return false;  
        } else {  
            if (bossPlane.getY() + bossPlane.getFrameH() > y && bossPlane.getY() + bossPlane.getFrameH() < y + height) {  
  
                if (x < bossPlane.getX() && x + width > bossPlane.getX()) {  
                    Log.e("AAAAAAAAAa", "isCollision: ...................................");  
                    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;  
  
  
    }  
  
}  

BossPlane:

package com.example.jinxin.myapplication;  
  
import android.graphics.Bitmap;  
import android.graphics.Canvas;  
import android.graphics.Paint;  
import android.util.Log;  
  
public class BossPlane {  
    private Bitmap bitmap;  
    private int x, y;  
    private int frameW, frameH;  
    private int speed = 10;  
    private int count;//计数器  
    private int time = 100;//疯狂模式间隔时间  
    private boolean isCrazy;  
    private int crazySpeed = 50;  
  
  
    private int bossHp = 10;  
  
  
    public BossPlane(Bitmap bitmap) {  
        this.bitmap = bitmap;  
        this.frameW = bitmap.getWidth() / 10;  
        this.frameH = bitmap.getHeight();  
        x = MySurfaceView.width / 2 - frameW / 2;  
  
  
    }  
  
  
    public void draw(Canvas canvas, Paint paint) {  
        canvas.save();  
        canvas.clipRect(x, y, x + frameW, y + frameH);  
        canvas.drawBitmap(bitmap, x, y, paint);  
        canvas.restore();  
        logic();  
    }  
  
    public void logic() {  
        count++;  
        //疯狂模式  
        if (isCrazy) {  
            y = y + crazySpeed;  
            crazySpeed--;  
            if (y == 0) {  
                isCrazy = false;  
                crazySpeed = 50;  
  
            }  
  
        } else {  
            if (count % time == 0) {  
                isCrazy = true;  
  
            }  
            x = x + speed;  
            if (x > MySurfaceView.width - frameW) {  
                speed = -speed;  
  
            }  
            if (x < 0) {  
                speed = -speed;  
  
            }  
        }  
  
    }  
  
  
  
  
  
  
  

   public boolean isCollision(Bullet bullet) {  
        if (bullet.getX()>x&&bullet.getX()+bullet.getBitmap().getWidth()<x+frameW&&bullet.getY()>y&&bullet.getY()<y+frameH){  
            bossHp--;  
  
            bullet.setDead(true);//设置子弹状态,碰撞后修改子弹状态为dead,从数组中移除  
            if (bossHp<0){  
                MySurfaceView.GAME_STATE =2;  
            }  
            return true;  
  
  
        }  
  
        return false;  
    }  
  
    public int getX() {  
        return x;  
    }  
  
    public int getY() {  
        return y;  
    }  
  
    public int getFrameW() {  
        return frameW;  
    }  
  
    public int getFrameH() {  
        return frameH;  
    }  
  
}  
4.如何绘制子弹

创建子弹Bullet的类,利用get set方法,以及if语句实现子弹的移动速度和击中飞机范围。最后要移除子弹,减少内存负荷。

  1. package com.example.jinxin.myapplication;  
  2.   
  3. import android.graphics.Bitmap;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Paint;  
  6.   
  7. public class Bullet {  
  8.   
  9.         private Bitmap bitmap;  
  10.         private int x,y;  
  11.         private int speed =10;  
  12.         public boolean isDead;  
  13.         private int type;  
  14.   
  15.   
  16.         public Bullet(Bitmap bitmap,int x,int y,int type){  
  17.             this.bitmap = bitmap;  
  18.             this.x = x;  
  19.             this.y = y;  
  20.             this.type = type;  
  21.   
  22.   
  23.   
  24.         }  
  25.         public void draw(Canvas canvas, Paint paint){  
  26.             canvas.drawBitmap(bitmap,x,y,paint);  
  27.             logic();  
  28.   
  29.   
  30.         }  
  31.         public void logic(){  
  32.             switch (type){  
  33.                 case 0:  
  34.                         y-=speed;  
  35.                         if (y<0){  
  36.                             isDead = true;  
  37.                         }  
  38.                     break;  
  39.   
  40.                 case 1:  
  41.                     y+=speed+5;  
  42.                     if (y>MySurfaceView.height){  
  43.                         isDead = true;  
  44.                     }  
  45.   
  46.                     break;  
  47.   
  48.             }  
  49.   
  50.         }  
  51.   
  52.   
  53.     public int getX() {  
  54.         return x;  
  55.     }  
  56.   
  57.     public int getY() {  
  58.         return y;  
  59.     }  
  60.   
  61.     public boolean isDead() {  
  62.         return isDead;  
  63.   
  64.   
  65.     }  
  66.   
  67.     public void setDead(boolean dead) {  
  68.         isDead = dead;  
  69.     }  
  70.   
  71.     public Bitmap getBitmap() {  
  72.         return bitmap;  
  73.     }  
  74. }  

5.如何判断碰

飞机与子弹
public boolean isCollision(Bullet bullet) {  
        if (noCollision) {  
            return false;  
        } else {  
            if (bullet.getX() > x && bullet.getX() < x + width && bullet.getY() > y && bullet.getY() < y + height) {  
                Log.e("***", "------------------");  
                noCollision = true;  
                if (hp > 0) {  
                    hp--;  
                }  
                return true;  
            }  
  
  
        }  
        return false;  
    }  

飞机与飞机的

public boolean isCollision(BossPlane bossPlane) {  
        if (noCollision) {  
            return false;  
        } else {  
            if (bossPlane.getY() + bossPlane.getFrameH() > y && bossPlane.getY() + bossPlane.getFrameH() < y + height) {  
  
                if (x < bossPlane.getX() && x + width > bossPlane.getX()) {  
                    Log.e("AAAAAAAAAa", "isCollision: ...................................");  
                    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.如何绘制爆炸效果:

创建Boom类,利用clipRect裁剪图片以及totalFrame来实现爆炸现象,currenFrame用来显示当前显示的第几幅画。最后记得结束爆炸。

package com.example.jinxin.myapplication;  
  
import android.graphics.Bitmap;  
import android.graphics.Canvas;  
import android.graphics.Paint;  
  
public class Boom {  
    private int totalFrame;  
    private Bitmap bitmap;  
    private int x,y;  
    private int currenFrame;//当前显示的第几幅画面  
    private int frameW;  
    private int 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-currenFrame*frameW,y,paint);  
        canvas.restore();  
        logic();  
  
  
  
    }  
    public void logic(){  
        if (currenFrame<totalFrame){  
            currenFrame++;  
        }else {  
            isEnd = true;  
        }  
    }  
    public boolean isEnd(){  
        return isEnd;  
    }  
  
}  

7.如何添加音效:

利用MediaPlayer来实现

[java]  view plain   copy
  1. MediaPlayer mediaPlayer = null;  
  2.         mediaPlayer = MediaPlayer.create(getContext(),R.raw.bgm_zhuxuanlv);  
  3.         mediaPlayer.setLooping(true);  
  4.         mediaPlayer.start();  
  5.         Paint paint = new Paint();  

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

运用到封装的地方为(代码):

封装就是把对象的信息和内部逻辑的结构隐藏起来。在MySurfaceView,Bullet,MyPlane,BossPlane,Boom,GameSounPool几个类中都用到了,

MySurfaceView

[java]  view plain   copy
  1. public static int height;  
  2.    private SurfaceHolder surfaceHolder;  
  3.    private Canvas canvas;//绘制图形的画布  
  4.    private boolean isDrawing = true;//标志位  
  5.    public static int width;  
  6.    private MyPlane plane;  
  7.    //Vector是线程安全的,ArrayList是非线程安全的  
  8.    private Vector<Bullet> bulletVector = new Vector<>();  
  9.    private Vector<Bullet> bossbulletVector = new Vector<>();  
  10.    private Vector<Boom>boomVector = new Vector<>();  
  11.    private int count;  
  12.    private BossPlane bossPlane;  
  13.    private GameSoundPool gameSoundPool;  
  14.    public static int GAME_STATE = 4;  
  15.    private MediaPlayer mediaPlayer;  
BossPlane
[java]  view plain   copy
  1. private Bitmap bitmap;  
  2.     private int x, y;  
  3.     private int frameW, frameH;  
  4.     private int speed = 10;  
  5.     private int count;//计数器  
  6.     private int time = 100;//疯狂模式间隔时间  
  7.     private boolean isCrazy;  
  8.     private int crazySpeed = 50;  
MyPlane
[java]  view plain   copy
  1. private Bitmap bitmap;  
  2.    private int x, y;  
  3.    private static int width, height;  
  4.    private int fx;  
  5.   
  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Android Studio 飞机大战是一款基于 Android Studio 开发的飞行射击游戏。玩家需要控制飞机躲避敌机的攻击并射击敌机,最终击败 Boss。这款游戏具有简单易懂的操作和精美的画面,适合各个年龄段的玩家。 ### 回答2: Android Studio飞机大战是一款基于Android平台开发的游戏应用程序。这款游戏使用了Android Studio开发工具,并利用Java语言编写。玩家扮演一名飞行员,在游戏中操控一架战斗机,与敌机展开战斗。 在游戏中,玩家可以使用屏幕上的触摸操作来控制战斗机的移动,同时按下屏幕进行射击。其操作简单直观,适合各个年龄段的玩家。游戏场景设置在空中,玩家需要躲避敌机的攻击,并尽可能多地消灭敌机。玩家可以通过击落敌机来积分,同时还可以获得道具和奖励,提升自己的战斗力。 这款游戏不仅具有良好的游戏性,还有精美的图形和音效。通过Android Studio平台的强大功能,游戏开发者可以设计出精美的游戏场景、真实的音效以及流畅的游戏操作,为玩家带来更好的游戏体验。 在开发过程中,开发者需要使用Android Studio提供的各种工具和资源进行开发。如Android Studio提供了所见即所得的可视化界面设计,开发者可以方便地布局游戏界面,设置按钮、背景等元素。同时,Android Studio还提供了强大的调试功能,开发者可以随时检查游戏代码的执行情况,及时修复bug。 总之,Android Studio飞机大战是一款基于Android平台的游戏应用程序,通过精美的图形和音效,简单直观的操作方式,给玩家带来了极具乐趣的游戏体验。如对游戏开发有兴趣的人员,可以使用Android Studio进行开发,体验游戏开发的魅力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值