飞机大战java_Java飞机大战

这是一个使用Java编写的飞机大战游戏,包括FlyingObject抽象类、敌机Airplane、小蜜蜂Bee等类,实现了敌机和小蜜蜂的移动、碰撞检测以及英雄机的动态效果等功能。游戏通过定时器不断刷新重画,支持鼠标事件控制英雄机移动和发射子弹,具有分数和生命值显示,当英雄机生命值归零时游戏结束。
摘要由CSDN通过智能技术生成

FlyingObject,作为飞行物的父类,这里的飞行物指的就是敌机,小蜜蜂,子弹,英雄机

package com.tarena.shoot;

import java.awt.image.BufferedImage;

//飞行物类

public abstract class FlyingObject {

protected BufferedImage image; //图片

protected int width; //宽

protected int height; //高

protected int x; //坐标

protected int y;

// 飞行物走一步

public abstract void step();

//检查是否出界,,返回true表示已越界

public abstract boolean outOfBounds();

public boolean shootBy(Bullet b) {

return false;

};

}

敌机类,图片取自ShootGame的入口类(在下面),打掉一个敌机得五分,敌机只能向下走

图片在主类 ShootGame中静态引入了

package com.tarena.shoot;

import java.util.Random;

//敌机

public class Airplane extends FlyingObject implements Enemy{

private int speed = 2; //走的步数

public Airplane() { // 全部都在构造方法中初始化数据

image = ShootGame.airplane;

width = image.getWidth(); //获取图片的宽

height = image.getHeight(); //获取图片的高

Random rand = new Random(); //随机数对象

x = rand.nextInt(ShootGame.WIDTH-this.width+1);

y = -this.height;

}

public int getScore() {

return 5; //打掉一个敌机得5分

}

// 重写

public void step() {

y+=speed;

}

// 重写是否越界函数---越界删除该对象

public boolean outOfBounds() {

if(y>ShootGame.HEIGHT) {

return true;

}else {

return false;

}

}

// 敌人被子弹射击----也就是检测图片与图片之间检测碰撞

public boolean shootBy(Bullet bullet) {

int x1 = this.x;

int x2 = this.x + this.width;

int y1 = this.y;

int y2 = this.y + this.height;

// x在 x1 和 x2 之间,y在y1和y2之间,既是碰撞了

if(bullet.xx1 && bullet.yy1) {

return true;

}else {

return false;

}

}

}

Bee,小蜜蜂类,也是敌人的一种,和上面敌机不同的是,小蜜蜂还会横着走,碰到边界反弹,打掉一个小蜜蜂还会获得奖励,也就是继承的接口 Award

package com.tarena.shoot;

import java.util.Random;

public class Bee extends FlyingObject implements Award{

private int xSpeed = 1; //X坐标速度

private int ySpeed = 2; //Y坐标速度

private int awardType; //奖励的类型 0/1

public Bee() {

image = ShootGame.bee;

width = image.getWidth();

height = image.getHeight();

Random rand = new Random();

x = rand.nextInt(ShootGame.WIDTH - this.width+1);

y = -this.height;

awardType = rand.nextInt(2); //0到1之间

int xSymbol = rand.nextInt(2);

if(xSymbol == 0) {

xSpeed = -xSpeed;

}

}

public int getType() {

return awardType; //返回奖励类型 0/1 在ShootGame 中根据0、1判断奖励的类型分别调用方法

}

// 重写

public void step() {

y+=ySpeed;

x+=xSpeed;

if(x>ShootGame.WIDTH-this.width || x<=0) {

xSpeed = -xSpeed;

}

}

// 重写是否越界函数

public boolean outOfBounds() {

if(y>ShootGame.HEIGHT) {

return true;

}else {

return false;

}

}

// 敌人被子弹射击

public boolean shootBy(Bullet bullet) {

int x1 = this.x;

int x2 = this.x + this.width;

int y1 = this.y;

int y2 = this.y + this.height;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值