java飞机大战

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

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++; 
  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
package cn.feike.shoot; import java.awt.Graphics; import java.awt.image.BufferedImage; public abstract class FlyingObject { protected double x;//物体的x坐标 protected double y;//物体的y坐标 protected double width;//物体的宽 protected double heigth;//物体的高 protected BufferedImage image;//当前正在显示的图片 protected int index = 0;//图片数组下标序号,子类中使用 protected double step;//飞行物每次(1/24秒)移动的距离 protected int life;//命 protected int state;//飞行物的状态 public static final int ACTIVE=0;//活着状态 public static final int DEAD=1;//死亡状态 public static final int REMOVE=2;//回收状态 //默认构造器 public FlyingObject() { life = 1; state = ACTIVE; } //有参构造器 public FlyingObject(double width,double heigth){ this();//调用无参数的构造器,必须写在第一行. this.x = (int)(Math.random()*(480-width)); this.y = -heigth; this.width = width; this.heigth = heigth; step = Math.random()*3+0.8;//初始化step为[0.8,3.8)之间的数 } //重写toString方法 public String toString() { return x+","+y+","+width+","+heigth+","+image; } //重写paint,方便子类对象的使用 public void paint(Graphics g) { g.drawImage(image, (int)x, (int)y, null);//绘制图片 } //飞行物移动的move方法 /** * 重构了move,方法实现播放销毁动画功能 */ public void move(){ if(state == ACTIVE){ y += step; return ; } if(state == DEAD){ //从子类对象中获取下一张照片 BufferedImage img = nextImage(); if(img == null){ state = REMOVE;//没有照片则回收 }else{ image = img;//否则把子类的图片传给image } //越界则销毁 if(y>=825){ state = REMOVE; } } } /** * 子类中必须有的方法,返回下一个要播放的照片引用, * 如果返回null表示没有可播放的照片了. */ protected abstract BufferedImage nextImage(); /** * 飞行物被打了一下 */ public void hit(){ if(life>0){ life--; } if(life==0){ state = DEAD; } } /** * 碰撞检测的方法 * 检测物体的位置是否在碰撞的范围内. * (子弹是否在飞行物的碰撞范围内) */ public boolean duang(FlyingObject obj){ //this(x,y,w,h) //obj(x,y,w,h) double x1 = this.x - obj.width; double x2 = this.x + this.width; double y1 = this.y - obj.width; double y2 = this.y + this.heigth; return x1<obj.x&&obj;.x<x2&&y1;<obj.y&&obj;.y<y2; } /** 重构FlyingObject,添加了状态检查方法 */ /** 检查飞行物死了吗 */ public boolean isDead(){ return state == DEAD; } /** 检查飞行物是否活动的 */ public boolean isActive(){ return state == ACTIVE; } /** 检查飞行是否可以被删除*/ public boolean canRemove(){ return state == REMOVE; } /** 飞行物添加"去死"方法*/ public void goDead(){ if(isActive()){ state = DEAD; } } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值