java飞机大战

本文介绍了一款使用Java编写的飞机大战游戏,通过分析游戏元素,包括Jframe、Jpanel、不同类型的飞机和子弹等,展现了面向对象编程的应用。作者详细讲解了如何利用BufferedImage加载和显示图片,并提供了游戏的源代码和运行效果。文章最后提到了游戏中的继承、封装、抽象以及接口实现等核心概念。

我们在写飞机大战时,先思考一下飞机大战中本身有哪些元素

1.本身元素有哪些

  1. 第一点就是需要用到Jframe和Jpanel来画出窗口和画出图片
  2. 然后设计出六个类,分别是天空类、飞机类、子弹类、(小敌机、大敌机、奖励机)类,设计BackGround背景超类让如上六大类来继承,给BackGround设计两种构造方法,来进行分别调用,图片加载设计为Images类,还有游戏窗口的World类,初学,博主较懒,所以把图片也都放在了src里边,同包中加载图片
  3. 在超类中写出move方法,然后在六大类中引用重写
  4. 主要元素为,三种敌机在窗口上方随机x轴刷新,我方战机发射子弹,敌方大飞机发射子弹,获取命数,获取战力值解锁更高级的子弹,检测游戏开始、结束、暂停状态,天空移动,获取得分,击毁敌机,敌机爆炸,得到奖励等等

2.里边运用到了什么?

1)面向对象:学会继承、封装、抽象、实现接口 (重点为熟练掌握了重写与重载);

2)学会如何用BufferedImage来读取图片,以及怎样才能让这些图片在窗口中显示出来

下边为飞机大战源代码(附运行效果)

backGround 超类

package flyChicken;

import org.omg.CORBA.PUBLIC_MEMBER;

import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Random;

// 背景板对象
public abstract class backGround {

    public static final int LIVE = 0;
    public static final int DEAD = 1;
    public static final int REMOVE = 2;
    protected int state = LIVE;

    protected int width;  //宽
    protected int height;//高
    protected int x;   //x坐标
    protected int y;    //y坐标
    protected int speed;//移动速度

    /** 三种飞机 */
    public backGround(int width,int height){
        this.width = width;
        this.height = height;
        Random rand = new Random();
        x = rand.nextInt(World.WIDTH-70+1);
        y = -height;
        speed = rand.nextInt(3)+1;
    }

    /* 写给英雄机,子弹 */
    public backGround(int width,int height,int x,int y,int speed){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    /* 飞机移动 */
    public abstract void move();

    /* 获取对象的图片 */
    public abstract BufferedImage getImage();


    /* 判断对象是否活着 */
    public boolean isLive(){
        return state==LIVE;
    }

    /* 判断对象时候死了 */
    public boolean isDead(){
        return state==DEAD;
    }

    /** 判断对象是否被删除了 */
    public boolean isRemove(){
        return state==REMOVE;
    }


    /** 检测越界 */
    public boolean isOutOfBounds(){
        return this.y >= World.HEIGHT;
    }

    /** 检测碰撞 */
    public boolean isHit(backGround other){
        //假设this指敌机    other指子弹
        int x1 = this.x- other.width;
        int x2 = this.x + this.width;
        int y1 = this.y - other.height;
        int y2 = this.y + this.height;
        int x = other.x;
        int y = other.y;

        return x>=x1 && x<=x2    &&    y>=y1 && y<=y2;
    }
    /** 检测死亡 */
    public void goDead(){
        state = REMOVE;
    }
    
}

World 整个游戏世界

package flyChicken;

import org.omg.CORBA.PUBLIC_MEMBER;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;

import java.util.Random;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import javax.swing.JButton;

//整个游戏世界
public class World extends JPanel{

    public static final int WIDTH =400;
    public static final int HEIGHT = 700;

    public static final int START = 0;     //启动状态
    public static final int RUNNING = 1;   //运行状态
    public static final int PAUSE = 2;     //暂停状态
    public static final int GAME_OVER = 3; //游戏结束状态
    private int state = START; //当前状态(默认为启动状态)

    private Sky sky = new Sky();
    private BattleFlyChicken flys = new BattleFlyChicken();
    private backGround[] enemys = {};
    private Bullet[] bullets= {};
    private EnemyBullet[] enemybuttles = {};

    /** 随机生成敌机 */
    public backGround nextbgd(){
        Random rand = new Random();
        int type = rand.nextInt(20);
        if (type<12){
            return new AirPlane();
        }else if (type<17){
            return new BigAirPlane();
        }else{
            return new Bee();
        }

    }

    /** 敌人入场 */
    private int bgdEnterIndex = 0;
    public void bgdEnterAction(){
        bgdEnterIndex++;
        if (bgdEnterIndex%20==0){
            backGround bgd = nextbgd();
            enemys = Arrays.copyOf(enemys,enemys.length+1);
            enemys[enemys.length-1] = bgd;
        }
    }

    private int enemybulletIndex = 0;
    /** 敌机子弹入场 */
    public void enemybuttlesEnterAction(){
        enemybulletIndex++; //10毫秒走一次
        if (enemybulletIndex
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值