Android Studio飞机大战

1.整体实现思路

这里写图片描述

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

1.添加两张或更多图片,x,y1坐标为屏幕的顶点,y2= y1-photo.getHeight();
2.进行两张图片的逻辑判断:

if (y1>=MySurfaceView.height) {
            y1=y2-photo.getHeight();
        }if (y2>=MySurfaceView.height){
            y2=y1-photo.getHeight();
        }

3.如何绘制飞机

1.定义飞机初始坐标

x=MySurfaceView.width/2-(MyPlane.getWidth()/2);
y=MySurfaceView.height-MyPlane.getHeight();

2.显示飞机canvas.drawBitmap(MyPlane,x,y,paint);
3.MotionEvent event动作监听事件,判断点击移动飞机

public void onTouch(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;
                        }
                    }
                }
            }

4.如何绘制子弹

1.绘制canvas.drawBitmap(bullet,x,y,paint);
2.switch判断:0为我方子弹,1为敌机子弹

 switch(type){
            case 0:
                y-=speed;
                if (y<0){
                    isDead=true;
                }
                break;
            case 1:
                y+=Bspeed;
                if (y>MySurfaceView.height){
                    isDead=true;
                }
                break;
        }

3.isDead()与bullet数量判断

 for (int i=0;i<mybulletVector.size();i++) {
                            if (mybulletVector.elementAt(i).isDead()) {
                                mybulletVector.remove(i);
                            }
                        }
for (int i=0;i<bossBulletVector.size();i++) {
                            if (bossBulletVector.elementAt(i).isDead()) {
                                bossBulletVector.remove(i);
                            }
                        }

5.如何判断碰撞(子弹与飞机,飞机与飞机)

1.飞机与飞机
    public boolean isCollision(BossPlane bossPlane) {
    if (noCollision) {
        return false;
    } else {
        if (x + MyPlane.getWidth() < bossPlane.getX() || x > bossPlane.getX() + bossPlane.getFrameW() || y > bossPlane.getY() + bossPlane.getFrameH() || y + MyPlane.getHeight() < bossPlane.getY()) {

        } else {
            HpCount--;
            x=MySurfaceView.width/2-(MyPlane.getWidth()/2);
            y=MySurfaceView.height-MyPlane.getHeight();
            noCollision = true;
            return true;
        }
    }
    return false;
}
2.子弹与飞机
public boolean isCollision(Mybullet bullet){
        if(noCollision){
            return false;
        }else{
            if (bullet.getX()>x&&bullet.getX()<x+MyPlane.getWidth()&&bullet.getY()>y&&bullet.getY()<y+MyPlane.getHeight()){
                if (HpCount>0){
                    HpCount--;
                    x=MySurfaceView.width/2-(MyPlane.getWidth()/2);
                    y=MySurfaceView.height-MyPlane.getHeight();
                }
                noCollision=true;
                return true;
            }
        }
        return false;
    }
3.我方飞机闪烁判断与血量相应显示
public void Appear(Canvas canvas, Paint paint){
        if (HpCount<=0){
            MySurfaceView.Game_STATE=1;
        }
            if(noCollision){
                noCollisionCount++;
                if(noCollisionCount%10==0){
                    canvas.drawBitmap(MyPlane,x,y,paint);
                }if (noCollisionCount>100){
                    noCollision=false;
                    noCollisionCount=0;
                }
           }else{
                canvas.drawBitmap(MyPlane,x,y,paint);
            }for (int i = 0;i<HpCount;i++){
                    canvas.drawBitmap(MyHp,i*MyHp.getWidth(),MySurfaceView.height-MyHp.getHeight(),paint);
        }
    }

6.如何绘制爆炸效果

1.爆炸图片剪切`
      canvas.save();
      canvas.clipRect(x,y,x+framW,y+framH);
      canvas.drawBitmap(boom,x-currentFrame*framW,y,paint);//图片一帧一帧显示
      canvas.restore();`
2.子弹碰撞到敌机时的爆炸判断
Boom boom = new Boom(BitmapFactory.decodeResource(this.getResources(),R.mipmap.boom),bossPlane.getX()+bossPlane.getFrameW()/2,bossPlane.getY(),7);
                                boomVector.add(boom);
                            }
                        }

     //判断boss飞机爆炸 删除爆炸
  for (int i =0;i<boomVector.size();i++){
      boomVector.elementAt(i).Appear(canvas,paint);
  if(boomVector.elementAt(i).isEnd()){
       boomVector.remove(i);
           }
  }

7.添加音效

运用soundpool方法
public GameSoundPool(Context context){
        this.soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC,0);
        s1 = soundPool.load(context,R.raw.shoot,0);
        s2 = soundPool.load(context,R.raw.explosion,0);
        s3 = soundPool.load(context,R.raw.explosion2,0);
    }
并在相应位置插入
添加子弹时增加音效
if (count % 10 == 0) {
   gameSoundPool.playSound(1);
   Mybullet mybullet = new Mybullet(BitmapFactory.decodeResource(this.getResources(), R.mipmap.mybullet),myPlane.getX()+(myPlane.getWidth()/2)-15,myPlane.getY(),0);
   mybulletVector.add(mybullet);
 }
子弹碰撞到敌机时增加音效
 if (bossPlane.isCollision(mybulletVector.elementAt(i))){
   gameSoundPool.playSound(2);
   mybulletVector.remove(i);
    Boom boom = new Boom(BitmapFactory.decodeResource(this.getResources(),R.mipmap.boom),bossPlane.getX()+bossPlane.getFrameW()/2,bossPlane.getY(),7);
    boomVector.add(boom);
  }
我方飞机被击中闪烁时增加音效
 if(myPlane.isCollision(bossBulletVector.elementAt(i))){//boss子弹碰撞我的飞机
   gameSoundPool.playSound(3);
   bossBulletVector.remove(i);
  }

8.哪些地方运用封装,继承,多态,方法重栽,接口?

1.封装:1.)图片的坐标x,y和宽度,高度用private修饰。2.)Boolean方法的判断用public修饰。

2.继承:

public class MySurfaceView extends SurfaceView {
public MySurfaceView(Context context) {
        super(context);
        gameSoundPool =new GameSoundPool(context);
        init();
    }
 }

3.多态:1.)我方子弹击中敌机碰撞 2.)敌机子弹击中我方飞机

我方子弹击中敌机碰撞
public boolean isCollision(Mybullet bullet){
            if (bullet.getX()>x&&bullet.getX()+bullet.bullet.getWidth()<x+getFrameW()&&bullet.getY()>y&&bullet.getY()+bullet.bullet.getHeight()<y+bossPlane.getHeight()){
                 if (hp>0){
                     hp--;
                 }else{
                     MySurfaceView.Game_STATE=1;
                 }
                return true;
            }
        return false;
        }
敌机子弹击中我方飞机
 public boolean isCollision(Mybullet bullet){
        if(noCollision){
            return false;
        }else{
            if (bullet.getX()>x&&bullet.getX()<x+MyPlane.getWidth()&&bullet.getY()>y&&bullet.getY()<y+MyPlane.getHeight()){
                if (HpCount>0){
                    HpCount--;
                    x=MySurfaceView.width/2-(MyPlane.getWidth()/2);
                    y=MySurfaceView.height-MyPlane.getHeight();
                }
                noCollision=true;
                return true;
            }
        }
        return false;
    }

4.方法重载 1.)敌方飞机子弹击中我方飞机 2.)敌方飞机与我方飞机碰撞

敌方飞机子弹击中我方飞机
public boolean isCollision(Mybullet bullet){
        if(noCollision){
            return false;
        }else{
            if (bullet.getX()>x&&bullet.getX()<x+MyPlane.getWidth()&&bullet.getY()>y&&bullet.getY()<y+MyPlane.getHeight()){
                if (HpCount>0){
                    HpCount--;
                    x=MySurfaceView.width/2-(MyPlane.getWidth()/2);
                    y=MySurfaceView.height-MyPlane.getHeight();
                }
                noCollision=true;
                return true;
            }
        }
        return false;
    }
敌方飞机与我方飞机碰撞(运用相反的逻辑:当敌方飞机与我方飞机在不碰撞的情况下)
public boolean isCollision(BossPlane bossPlane) {
    if (noCollision) {
        return false;
    } else {
        if (x + MyPlane.getWidth() < bossPlane.getX() || x > bossPlane.getX() + bossPlane.getFrameW() || y > bossPlane.getY() + bossPlane.getFrameH() || y + MyPlane.getHeight() < bossPlane.getY()) {

        } else {
            HpCount--;
            x=MySurfaceView.width/2-(MyPlane.getWidth()/2);
            y=MySurfaceView.height-MyPlane.getHeight();
            noCollision = true;
            return true;
        }
    }
    return false;
}

5.接口:实现Runnable方法

public class MySurfaceView  implements SurfaceHolder.Callback,Runnable {
public void run() {           

                                 } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }

9.我的收获与感悟:

实训课从本身对java基础并不是太熟悉,经过3个星期的java学到了很多,包括基础概念,简单方法应用,参数调用。比起原先进步了不少。
最后一个星期学习了AndroidStudio,从一开始听到要做一个飞机大战的游戏感觉挺有意思并且也是第一次接触,新的软件新的项目。到今天整个飞机大战的基础差不多学完了,创建背景,飞机,敌机,子弹。创建飞机闪烁,爆炸,移动飞机。创建特效,音效。虽然都是些基础但学到了不少,接下来就是个人发挥,改善,升华的时间。下次接触的时候可能是大二的这个时候,和江哥在学习上的接触差不多就到这了,平日里一呼喊就会过来帮助我们解决我们自身解决不了的问题,可能只是转个弯就能出来,但自己觉得什么也看不懂。
很感谢我们的实训老师,江哥。

  • 5
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答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进行开发,体验游戏开发的魅力。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值