Java打飞机小游戏(附完整源码)

这篇博客分享了作者使用Java编写的打飞机小游戏,旨在通过实例帮助读者理解面向对象编程。文章提供了游戏的完整代码,包括敌飞机、分数奖励、蜜蜂、子弹类、飞行物、英雄机和游戏启动主类等关键部分。虽然Java不是桌面应用的理想选择,但这个简单的游戏展示了OO编程的基本思想。作者还提到,由于篇幅原因未上传所有图片,但有兴趣的读者可以通过私信获取,同时提供了一份思维导图辅助理解面向对象编程流程。
摘要由CSDN通过智能技术生成

Paste_Image.png

写在前面

技术源于分享,所以今天抽空把自己之前用java做过的小游戏整理贴出来给大家参考学习。java确实不适合写桌面应用,这里只是通过这个游戏让大家理解oop面向对象编程的过程,纯属娱乐。代码写的很简单,也很容易理解,并且注释写的很清楚了,还有问题,自己私下去补课学习。

完整代码

敌飞机

import java.util.Random;


 敌飞机: 是飞行物,也是敌人

public class Airplane extends FlyingObject implements Enemy {
   
    private int speed = 3;  //移动步骤

    /** 初始化数据 */
    public Airplane(){
        this.image = ShootGame.airplane;
        width = image.getWidth();
        height = image.getHeight();
        y = -height;          
        Random rand = new Random();
        x = rand.nextInt(ShootGame.WIDTH - width);
    }

    /** 获取分数 */
    @Override
    public int getScore() {  
        return 5;
    }

    /** //越界处理 */
    @Override
    public     boolean outOfBounds() {   
        return y>ShootGame.HEIGHT;
    }

    /** 移动 */
    @Override
    public void step() {   
        y += speed;
    }

}

分数奖励

/** 
 * 奖励 
 */  
public interface Award {
     
    int DOUBLE_FIRE = 0;  //双倍火力  
    int LIFE = 1;   //1条命  
    /** 获得奖励类型(上面的0或1) */  
    int getType();  
}

蜜蜂

import java.util.Random;  

/** 蜜蜂 */  
public class Bee extends FlyingObject implements Award{
     
    private int xSpeed = 1;   //x坐标移动速度  
    private int ySpeed = 2;   //y坐标移动速度  
    private int awardType;    //奖励类型  

    /** 初始化数据 */  
    public Bee(){  
        this.image = ShootGame.bee;  
        width = image.getWidth();  
        height = image.getHeight();  
        y = -height;  
        Random rand = new Random();  
        x = rand.nextInt(ShootGame.WIDTH - width);  
        awardType = rand.nextInt(2);   //初始化时给奖励  
    }  

    /** 获得奖励类型 */  
    public int getType(){  
        return awardType;  
    }  

    /** 越界处理 */  
    @Override  
    public boolean outOfBounds() {  
        return y>ShootGame.HEIGHT;  
    }  

    /** 移动,可斜着飞 */  
    @Override  
    public void step() {        
        x += xSpeed;  
        y += ySpeed;  
        if(x > ShootGame.WIDTH-width){    
            xSpeed = -1;  
        }  
        if(x < 0){  
            xSpeed = 1;  
        }  
    }  
}

子弹类:是飞行物体

/** 
 * 子弹类:是飞行物 
 */  
public class Bullet extends FlyingObject {
     
    private int speed = 3;  //移动的速度  

    /** 初始化数据 */  
    public Bullet(int x,int y){  
        this.x = x;  
        this.y = y;  
        this.image = ShootGame.bullet;  
    }  

    /** 移动 */  
    @Override  
    public void step(){     
        y-=speed;  
    }  

    /** 越界处理 */  
    @Override  
    public boolean outOfBounds() {  
        return y<-height;  
    }  

}

敌人的分数

/** 
 * 敌人,可以有分数 
 */  
public interface Enemy {
     
    /** 敌人的分数  */  
    int getScore();  
}

飞行物(敌机,蜜蜂,子弹,英雄机)

import java.awt.image.BufferedImage;  

/** 
 * 飞行物(敌机,蜜蜂,子弹,英雄机) 
 */  
public abstract class FlyingObject {
     
    protected int x;    //x坐标  
    protected int y;    //y坐标  
    protected int width;    //宽  
    protected int height;   //高  
    protected BufferedImage image;   //图片  

    public int getX() {  
        return x;  
    }  

    public void setX(int x) {  
        this.x = x;  
    }  

    public int getY() {  
        return y;  
    }  

    public 
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值