JAVA小白的第二个项目—飞机大战分步骤理解流程

项目简单简介

飞机大战是集成这一个月实训以来的成果,其中包括了所学的封装,继承,多态,方法的重载等多个知识点构成的一款简单的小游戏。经过一个礼拜的时间将此项目完成,由多个模块组成。

这个代码我们一共需要创建7个类

一.如何实现游戏背景图滚动播放

这个游戏首先的第一个问题就是呈现出飞机正在向前飞行的效果,也就是如何将图片一张接着一张无缝的滚动播放
在MySurfacView的画布上将背景图片上传
我们先创建Blackground类,并创建logic方法

package com.example.administrator.myapplication9;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

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+=10;
        y2+=10;
        if (y1>=MySurfaceView.height){
            y1=y2-bitmap.getHeight();//移动到第二张图片的顶部
        }
        if (y2>=MySurfaceView.height){
            y2=y1-bitmap.getHeight();
        }
    }
}

如何绘制飞机

我们需要创建一只画笔,锁定画布
并在MySurfaceView中创建run方法

  public void run() {
        gameSoundPool.playSound(3);

        Paint paint = new Paint();
        BackGround backGround = new BackGround(BitmapFactory.decodeResource(getResources(), R.mipmap.img_bg_level_3));
        plane = new Myplane(BitmapFactory.decodeResource(getResources(), R.mipmap.myplane),BitmapFactory.decodeResource(getResources(), R.mipmap.myhp));
        BossPlane bossPlane = new BossPlane(BitmapFactory.decodeResource(getResources(), R.mipmap.bossplane));

这样就将敌机和我方机器绘制在了画布上

如何绘制子弹

绘制子弹和绘制飞机的方法类似,创建一个子弹(bullet)类,在MySurfaceView中需要实例化子弹对象,然后将子弹的图片上传,我们还需要创建我方子弹,敌方子弹的数组eg: private VectorbulletVector = new Vector <>();像这样的一个数组用来存放子弹,调用bullet类,在bullet类中创造logic方法,用if语句进行判断,分别进行判断,并给玩家子弹和敌方子弹进行设置

这是先创建一个bullet类,子弹类,并在里面创建方法,设置子弹的运行
这里写package com.example.administrator.myapplication9;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class Bullet {
   
    private Bitmap bitmap;
    private int x, y;
    private int speed = 10;
    private boolean isDead;
    private int type;

    public Bullet(Bitmap bitmap, int x, int y,int type) {
        this.bitmap = bitmap;
        this.x = x;
        this.y = y;
        this.type = type;

    }

    public Bullet() {

    }

    public void draw(Canvas canvas, Paint paint) {
        canvas.drawBitmap(bitmap, x, y, paint);
        logic();
    }

    public void 
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值