JAVA基础知识完成的增强版仙侠主题飞机大战
程序员文章站
2022-06-27 16:18:36
#使用JAVA基础知识做的一个增强版飞机大战,仙侠主题,增设关卡BOSS等欢迎使用你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。新的改变我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:全新的界面设计 ,将会带来全新的写作体验;在创作中心设置你喜爱的代码高亮样式,Markdown...
使用JAVA基础知识做的一个增强版飞机大战,仙侠主题,增设关卡BOSS等
第一次在CSDN写博客 不足之处请大家多多指教
先放一下效果图
以下是部分代码介绍
首先定义播放音乐和图片的工具类,都是使用静态代码块来加载资源
package Game;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
/** 处理图片的工具类*/
public class Images {
public static BufferedImage[] robot0;//支援机器人出现前
public static BufferedImage[] robot;//支援机器人
public static BufferedImage[] superaward;//超级奖励
public static BufferedImage[] superaward2;
public static BufferedImage paihang;//排行榜
public static BufferedImage tongguan;//通关
public static BufferedImage[] award;
public static BufferedImage[] awardtype;
public static BufferedImage[] protect;
public static BufferedImage[] bskill;//BOSS技能图
public static BufferedImage[] bskill2;//BOSS技能图
public static BufferedImage[] bskill3;//BOSS技能图
public static BufferedImage skill;//技能图
public static BufferedImage[] bosstest5;
public static BufferedImage[] bosstest4;
public static BufferedImage[] bosstest3;
public static BufferedImage[] bosstest2;
public static BufferedImage[] bosstest2f;
public static BufferedImage[] bairplanetest5;//大敌机五号
public static BufferedImage[] bairplanetest4;//大敌机四号
public static BufferedImage[] bairplanetest3;//大敌机三号
public static BufferedImage[] bairplanetest2;//大敌机二号
public static BufferedImage[] bairplanetest;//大敌机一号
public static BufferedImage[] airplanetest;//小敌机一号
public static BufferedImage[] airplanetest2;//小敌机二号
public static BufferedImage[] airplanetest3;//小敌机三号
public static BufferedImage[] airplanetest4;//小敌机4号
public static BufferedImage[] airplanetest5;//小敌机5号
public static BufferedImage[] beetest;//蜜蜂一号
public static BufferedImage[] beetest2;//蜜蜂二号
public static BufferedImage[] beetest3;//蜜蜂三号
public static BufferedImage[] beetest4;//蜜蜂三号
public static BufferedImage[] test;//BOSS机一号
public static BufferedImage[] test2;
public static BufferedImage[] ebullet;//敌方子弹图片
public static BufferedImage helphero; //僚机图片
public static BufferedImage sky[]; //天空图片数组
public static BufferedImage bullet[]; //子弹图片
public static BufferedImage[] heros;
public static BufferedImage airs[]; //敌机图片
public static BufferedImage[] boom;//爆炸数组
public static BufferedImage bairs[];
public static BufferedImage bees[];
//public static BufferedImage[] boss;//boss机数组
public static BufferedImage start;//开始状态的图片
//public static BufferedImage pause;//开始状态的图片
public static BufferedImage gameover;//开始状态的图片
/**静态代码块中进行初始赋值*/
static{
bosstest5 = new BufferedImage[41];
bosstest4 = new BufferedImage[33];
bosstest3 = new BufferedImage[16];
bosstest2 = new BufferedImage[9];
bosstest2f = new BufferedImage[9];
bairplanetest = new BufferedImage[4];
bairplanetest2 = new BufferedImage[6];
bairplanetest3 = new BufferedImage[8];
bairplanetest4 = new BufferedImage[6];
bairplanetest5 = new BufferedImage[14];
airplanetest = new BufferedImage[6];
airplanetest3 = new BufferedImage[6];
airplanetest4 = new BufferedImage[7];
airplanetest5 = new BufferedImage[8];
bullet = new BufferedImage[2];
robot = new BufferedImage[4];
robot0 = new BufferedImage[7];
bskill = new BufferedImage[5];
bskill2 = new BufferedImage[8];
bskill3 = new BufferedImage[8];
protect = new BufferedImage[11];
airplanetest2 = new BufferedImage[13];
beetest = new BufferedImage[6];
beetest2 = new BufferedImage[6];
beetest3 = new BufferedImage[6];
beetest4 = new BufferedImage[6];
test = new BufferedImage[6];
test2 = new BufferedImage[6];
bairs = new BufferedImage[5];
bees = new BufferedImage[5];
airs = new BufferedImage[5];
ebullet = new BufferedImage[2];
skill = readImage("fire.png");
paihang = readImage("排行榜.jpg");
tongguan = readImage("通关.png");
ebullet[0] = readImage("月光.png");
ebullet[1] = readImage("600.png");
superaward = new BufferedImage[10];
superaward2 = new BufferedImage[10];
award = new BufferedImage[17];
awardtype = new BufferedImage[2];
awardtype[0] = readImage("生命.png");
awardtype[1] = readImage("子弹奖励.jpg");
// boss = new BufferedImage[5];
bullet[0] = readImage("bullet.png");
bullet[1] = readImage("bullet2.png");
helphero = readImage("hero.png");
boom = new BufferedImage[6];
heros = new BufferedImage[12];
sky = new BufferedImage[5];
// boss[0] = readImage("602.png");
// boss[1] = readImage("ep13.png");
// boss[2] = readImage("ep15.png");
// boss[3] = readImage("601.png");
// boss[4] = readImage("ep14.png");
// airs[0] = readImage("airplane.png");
// airs[1] = readImage("ep01.png");
// airs[2] = readImage("ep02.png");
// airs[3] = readImage("ep03.png");
// airs[4] = readImage("ep05.png");
// bairs[0] = readImage("bigairplane.png");
// bairs[1] = readImage("ep10.png");
// bairs[2] = readImage("ep11.png");
// bairs[3] = readImage("ep12.png");
// bairs[4] = readImage("ep12.png");
// bees[0] = readImage("bee.png");
// bees[1] = readImage("ep06.png");
// bees[2] = readImage("ep07.png");
// bees[3] = readImage("ep08.png");
// bees[4] = readImage("ep09.png");
start = readImage("start.jpg");
// pause = readImage("pause.png");
gameover = readImage("失败.png");
/** 爆炸效果图片数组 */
for(int i=0;i<boom.length;i++){
boom[i] = readImage("boom ("+(i+1)+").png");
}
for (int i = 1; i < sky.length+1; i++) {
sky[i-1] = readImage("bg"+i+".jpg");
}
/**
* 加载英雄机图片
*/
for (int i = 0; i < heros.length; i++) {
heros[i] = readImage("hero ("+(i+1)+").png");
}
for (int i = 0; i < protect.length; i++) {
protect[i] = readImage("护盾 ("+(i+1)+").png");
}
/**
* 加载正向超级奖励
*/
for (int i = 0; i < superaward.length; i++) {
superaward[i] = readImage("super ("+(i+1)+").png");
superaward2[i] = readImage("super2 ("+(i+1)+").png");
}
for (int i = 0; i < robot.length; i++) {
robot[i] = readImage("机器人 ("+(i+1)+").png");
}
/**
* 加载超级奖励2号
*/
for (int i = 0; i < award.length; i++) {
award[i] = readImage("bee2 ("+(i+1)+").png");
}
/**
* 加载大小一号敌机
*/
for (int i = 0; i < beetest.length; i++) {
beetest[i] = readImage("bee ("+(i+1)+").png");
beetest2[i] = readImage("bee3 ("+(i+1)+").png");
beetest3[i] = readImage("bee4 ("+(i+1)+").png");
beetest4[i] = readImage("bee5 ("+(i+1)+").png");
airplanetest[i] = readImage("e1 ("+(i+1)+").png");
airplanetest3[i] = readImage("e3 ("+(i+1)+").png");
bairplanetest2[i] = readImage("En2 ("+(i+1)+").png");
bairplanetest4[i] = readImage("En4 ("+(i+1)+").png");
}
/**
* 加载小敌机4号
*/
for (int i = 0; i < airplanetest4.length; i++) {
airplanetest4[i] = readImage("e4 ("+(i+1)+").png");
}
for (int i = 0; i < bskill.length; i++) {
bskill[i] = readImage("sk"+i+".png");
}
for (int i = 0; i < bairplanetest.length; i++) {
bairplanetest[i] = readImage("En1 ("+(i+1)+").png");
}
/**
* 加载大敌机五号
*/
for (int i = 0; i < bairplanetest5.length; i++) {
bairplanetest5[i] = readImage("En5 ("+(i+1)+").png");
}
/**
* 加载小敌机二号
*/
for (int i = 0; i < airplanetest2.length; i++) {
airplanetest2[i] = readImage("e2 ("+(i+1)+").png");
}
/**
* 加载大敌机三号
*/
for (int i = 0; i < bairplanetest3.length; i++) {
bairplanetest3[i] = readImage("En3 ("+(i+1)+").png");
airplanetest5[i] = readImage("e5 ("+(i+1)+").png");
}
/**
* 加载BOSS1
*/
for (int i = 0; i < test.length; i++) {
test[i] = readImage("test正"+i+".png");
test2[i] = readImage("test反"+i+".png");
}
/**
* 加载BOSS2
*/
for (int i = 0; i < bosstest2.length; i++) {
bosstest2[i] = readImage("boss2 ("+(i+1)+").png");
bosstest2f[i] = readImage("boss2反 ("+(i+1)+").png");
}
for (int i = 0; i < bosstest3.length; i++) {
bosstest3[i] = readImage("boss3 ("+(i+1)+").png");
}
for (int i = 0; i < bosstest5.length; i++) {
bosstest5[i] = readImage("boss5 ("+(i+1)+").png");
}
for (int i = 0; i < bosstest4.length; i++) {
bosstest4[i] = readImage("boss4 ("+(i+1)+").png");
}
/**
* 加载机器人
*/
for (int i = 0; i < robot0.length; i++) {
robot0[i] = readImage("机器出现 ("+(i+1)+").png");
}
/**
* 加载BOSS精灵王技能
*/
for (int i = 0; i < bskill2.length; i++) {
bskill2[i] = readImage("闪电 ("+(i+1)+").png");
bskill3[i] = readImage("飓风 ("+(i+1)+").png");
}
}
/**读取图片的方法*/
public static BufferedImage readImage(String fileName){
try{
BufferedImage img = ImageIO.read(Flys.class.getResource(fileName));
return img;
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException();
}
}
}
package Game;
import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
public class Music {
public static AudioClip ac ;
public static AudioClip bg ;//背景音乐
public static AudioClip dj ;
public static AudioClip fs ;
public static AudioClip ly ;
public static AudioClip jf ;
public static AudioClip my ;
public static AudioClip gd ;
static{
try {
ac = getMusic("src/鹰啼.wav");
bg = getMusic("C:\\Users\\chengcheng\\Desktop\\迪迦奥特曼.wav");
dj = getMusic("src/电击.wav");
fs = getMusic("src/子弹命中.wav");
ly = getMusic("src/龙吟.wav");
jf = getMusic("src/飓风.wav");
my = getMusic("src/冥月.wav");
gd = getMusic("src/光电.wav");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static AudioClip getMusic(String path) throws Exception {
@SuppressWarnings("all")
AudioClip ac = Applet.newAudioClip(new File(path).toURI().toURL());
return ac;
}
}
接着是所有飞行物的父类,存放共有的属性和行为
package Game;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
public abstract class Flys {
int hp = 1;
public static int k = 0;
public static final int LIFE = 0;//代表活着的状态
public static final int DEAD = 1;//代表死亡爆炸状态
public static final int REMOVE = 2;//代表移除状态
int state = LIFE;//默认当前状态为LIFE
protected int high;
protected int width;
protected int x;
protected int y;
BufferedImage img;
/** 专门为英雄机,天空,子弹提供的构造方法*/
public Flys( int x, int y,int width,int high) {
super();
this.high = high;
this.width = width;
this.x = x;
this.y = y;
}
/**专门为大小敌机和小蜜蜂提供的构造 */
public Flys(int high, int width) {
super();
Random r = new Random();
this.high = high;
this.width = width;
x = r.nextInt(World.WIDTH-width);
y = -high;
}
/**因为每个对象获取图片的方式不一致,所以父类中设计获取图片的抽象方法*/
public abstract BufferedImage getImage();
public void paint(Graphics g){
g.drawImage(getImage(),x,y,null);
}
public abstract void step();
public boolean isLife(){
return state==LIFE;//状态为life返回true
}
public boolean isRemove(){
return state==REMOVE;//状态为remove返回true
}
public boolean isDead(){
return state==DEAD;//状态为dead返回true
}
public boolean hit(Flys other){
int x1 = this.x-other.width;
int x2 = this.x+this.width;
int y1 = this.y-other.high;
int y2 = this.y+this.high;
int x = other.x;
int y = other.y;
return x>=x1 && x<=x2 && y>=y1 && y<=y2;
}
public void goDead(){
state = DEAD;
}
//检测是否越界的方法
public boolean isBound(){
return y>=World.HIGH;
}
public int Hp(){
hp--;
return hp;
}
}
接下来是敌机类,分为大小敌机和奖励机三个,基本思路一致,使用不同的图片相互切换实现图片的动态展示
在这里贴出大敌机类的代码
package Game;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class BigAirplane extends Flys implements EnemyGetScore{
public BigAirplane() {
super(160, 160);
if(Flys.k==4){
width = 220;
high = 220;
}
hp = 6+2*Flys.k;
}
public void step(){
y += 6+Flys.k;
}
@Override
public void paint(Graphics g) {
g.drawImage(getImage(), x, y, width,high,null);
}
int change = 0;
private int index = 0;
int index1 = 0;
int index2 = 0;
int index3 = 0;
int index4 = 0;
/** 重写画大敌机的方法 */
@Override
public BufferedImage getImage() {
change++;
if(change%20==0){
index2++;
index1++;
index3++;
index4++;
index++;
}
if(isLife()){
if(Flys.k==0)
return Images.bairplanetest[index%4];
else if(Flys.k==1)
return Images.bairplanetest2[index2%6];
else if(Flys.k==2)
return Images.bairplanetest3[index3%8];
else if(Flys.k==3)
return Images.bairplanetest4[index2%6];
else
return Images.bairplanetest5[index4%14];
}else if(isDead()) {
img = Images.boom[index%6];
if(index%6==0){
//index=0;
state = REMOVE;
}
return img;
}
return null;
}
@Override
public int getscore() {
return 3*Flys.k;
}
@Override
public int Hp() {
hp--;
return hp;
}
}
以下是英雄机的代码,
package Game;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
public class Hero extends Flys{
int bx = this.width/4;
int by = 20;
int life = 30;
int fire=1;
boolean protect = false;//英雄机的受保护状态
boolean help = false;
boolean skill = true;
public Hero() {
super(380,800,150,200);
}
public void moveMouse(int x,int y){
this.x = x-this.width/2;
this.y = y-this.high/2;
}
int index = 0;
int count = 0;
@Override
public BufferedImage getImage() {
if(isLife()){
count++;
if(count%30==0)
index++;
if(!protect)
return Images.heros[index%Images.heros.length];
else
return Images.protect[index%11];
}else if(isDead()){
return Images.boom[index%6];
}
return null;
}
//英雄机发射子弹的方法
public LinkedList<Bullet> shoot() {
if(fire>=4){
LinkedList<Bullet> five = new LinkedList<Bullet>();
five.add(new Bullet(x+bx-5,y+by,0));
five.add(new Bullet(x+2*bx+5,y+by,0));
five.add( new Bullet(x+bx+20,y+by,0));
five.add(new Bullet(x+3*bx+5,y+by,2));
five.add(new Bullet(x-bx-5,y+by,1));
if(help) {
Bullet buhelp = new Bullet(x-150,y-100);
five.add(buhelp);
Bullet buhelp2 = new Bullet(x+55,y-100);
five.add(buhelp2);
}
return five;
}else if(fire==3){
LinkedList<Bullet> three = new LinkedList<Bullet>();
three.add(new Bullet(x+bx-5,y+by,0));
three.add(new Bullet(x+2*bx+5,y+by,0));
three.add( new Bullet(x+bx+20,y+by,0));
if(help) {
Bullet buhelp = new Bullet(x-150,y);
three.add(buhelp);
Bullet buhelp2 = new Bullet(x+55,y);
three.add(buhelp2);
}
return three;
}else if(fire==2){
LinkedList<Bullet> two = new LinkedList<Bullet>();
two.add(new Bullet(x+bx-5,y+by,0));
two.add(new Bullet(x+2*bx+5,y+by,0));
if(help) {
Bullet buhelp = new Bullet(x-150,y);
two.add(buhelp);
Bullet buhelp2 = new Bullet(x+55,y);
two.add(buhelp2);
}
return two;
}else{
LinkedList<Bullet> one = new LinkedList<Bullet>();
one.add(new Bullet(x+bx+20,y-by,0));
if(help) {
Bullet buhelp = new Bullet(x-150,y-100);
one.add(buhelp);
Bullet buhelp2 = new Bullet(x+55,y-100);
one.add(buhelp2);
}
return one;
}
}
/**
* 英雄机增加生命
*/
public void addLife(){
life++;
}
/**
* 英雄机减少生命
*/
public void subLife(){
life--;
}
/**
* 获取生命
* @return
*/
public int getLife(){
return life;
}
/**
* 英雄机增加火力
*/
public void addFire(){
fire++;
}
/**
* 清空活力值
*/
public void clearFire(){
fire = 1;
}
@Override
public void step() {
}
}
然后是BOSS机的类
package Game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
/**
* BOSS机类
* @author JSD
*
*/
public class BossAirplane extends Flys{
int hp = 150+50*Flys.k;
boolean flag = true;
int xspeed = 1;
int yspeed = 1;
public BossAirplane(){
super(10,0,350,300);
state = LIFE;
if(Flys.k==1){
x = 600;
}else if(Flys.k==2){
x = 500;
y = 0;
width = 400;
high = 400;
}else if(Flys.k==4||Flys.k==3)
x = 50;
}
@Override
public void paint(Graphics g) {
g.setColor(Color.YELLOW);
if(Flys.k==0)
g.drawString("火龙王", 50, 100);
else if(Flys.k==1)
g.drawString("瘟疫巫师", 50, 100);
else if(Flys.k==2)
g.drawString("精灵王-伊卡贝尔", 50, 100);
else if(Flys.k==3)
g.drawString("光之城主", 50, 100);
else
g.drawString("尼古拉斯", 50, 100);
g.drawImage(getImage(), x, y, width,high, null);
}
int index=0;
int index1 = 0;
int index2 = 0;
int index3 = 0;
int index4 = 0;
int index5 = 0;
int change = 0;
@Override
public BufferedImage getImage() {
change++;
if(change%35==0){
index++;
index1++;
index2++;
index3++;
index4++;
index5++;
}
if(isLife()){
if(Flys.k==0&&flag)
return Images.test2[index1%6];
else if(Flys.k==0&&!flag)
return Images.test[index1%6];
else if(Flys.k==1&&!flag)
return Images.bosstest2[index2%9];
else if(Flys.k==1&&flag)
return Images.bosstest2f[index2%9];
else if(Flys.k==2)
return Images.bosstest3[index3%16];
else if(Flys.k==3)
return Images.bosstest4[index4%33];
else
return Images.bosstest5[index4%41];
}else if(isDead()){
img = Images.boom[index%6];//获取爆破图
if(index%Images.boom.length==0){
state = REMOVE;
}
return img;
}else
return null;
}
@Override
public void step() {
getImage();
if(flag){
x++;
}else{
x--;
}
if(x==700){
flag = !flag;
}
if(x==-100){
flag = !flag;
}
}
int count = 0;
public void step2(){
getImage();
if(flag){
y++;
}else{
y--;
}
if(y==800){
flag = !flag;
}
if(y==-100){
flag = !flag;
}
}
public void step5(){
x += 3*xspeed;
y += 4*yspeed;
if(x>=700||x<=-100)
xspeed = -xspeed;
if(y>=700||y<=-50)
yspeed = -yspeed;
if(x>=700){
flag = !flag;
}
if(x<=-100){
flag = !flag;
}
}
/**
* BOSS机射击的方法
* @return
*/
public LinkedList<EBullet> shoot(){
if(Flys.k==0){
LinkedList<EBullet> list = new LinkedList<EBullet>();
if(flag){
list.add(new EBullet(x+120,y-50,0));
list.add(new EBullet(x+120,y-50,1));
list.add(new EBullet(x+120,y-50,2));
}else{
list.add(new EBullet(x-120,y-50,0));
list.add(new EBullet(x-120,y-50,1));
list.add(new EBullet(x-120,y-50,2));
}
return list;
}else if(Flys.k==1){
LinkedList<EBullet> list = new LinkedList<EBullet>();
if(flag){
list.add(new EBullet(x+50,y-200,0));
list.add(new EBullet(x+40,y-100,2));
}else{
list.add(new EBullet(x-80,y-200,0));
list.add(new EBullet(x-80,y-200,1));
}
return list;
}else if(Flys.k==2){
LinkedList<EBullet> list = new LinkedList<EBullet>();
list.add(new EBullet(x+100,y-250,4));
return list;
}else if(Flys.k==3){
LinkedList<EBullet> list = new LinkedList<EBullet>();
list.add(new EBullet(x+50,y-350,3));
list.add(new EBullet(x-200,y-350,3));
return list;
}
else {
LinkedList<EBullet> list2 = new LinkedList<EBullet>();
list2.add(new EBullet(x+50,y-20,0));
list2.add(new EBullet(x+60,y-20,3));
list2.add(new EBullet(x,y-50,2));
return list2;
}
}
}
最后再贴一下World类,所有的画图和方法调用以及主方法都在这里控制
package Game;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.LinkedList;
import java.util.List;
/**
* 窗口类
* 用于绘制图形界面
* @author JSD
*
*/
@SuppressWarnings("all")
public class World extends JPanel{
public static int x=100;
long time1 = System.currentTimeMillis();
int count = 1;
//int bcount = 0;//用于记录bOSS技能音效
int rindex = 0;//机器人开枪
int sindex = 0;//计算超级奖励出现时间
int awardindex = 0;//记录奖励出现时间
//窗口的宽高设置为常量
public static final int WIDTH = 1200;
public static final int HIGH = 1000;
public static final int START = 0;//启动状态
public static final int RUNNING = 1;//运行状态
public static final int PAUSE = 2;//暂停状态
public static final int GAMEOVER = 3;//结束状态
public static final int CLEAR = 4;//结束状态
File file = new File("D:\\EclipseWorkspace\\ShootGame\\资源读取\\仙剑三.mp3");
File file1 = new File("D:\\EclipseWorkspace\\ShootGame\\资源读取\\迪迦奥特曼.mp3");
File file2 = new File("D:\\EclipseWorkspace\\ShootGame\\资源读取\\龙王.mp3");
File file3 = new File("D:\\EclipseWorkspace\\ShootGame\\资源读取\\FAIRYTAIL.mp3");
File file4 = new File("D:\\EclipseWorkspace\\ShootGame\\资源读取\\数码宝贝.mp3");
File file5 = new File("D:\\EclipseWorkspace\\ShootGame\\资源读取\\hope.mp3");
AudioPlayer au = new AudioPlayer(file);
int state = START;
private int score = 0;
boolean help = false;
int scount = 1;
private List<AwardType> at = new LinkedList<AwardType>();//奖励类型集合
private List<EBullet> eb = new LinkedList<EBullet>();
private List<Robot> robot = new LinkedList<Robot>();
private Hero h = new Hero();//创建英雄机对象
private Sky s = new Sky();//创建天空对象
private List<SuperAward> sa = new LinkedList<SuperAward>();//创建超级奖励对象
private List<BossAirplane> boss = new LinkedList<BossAirplane>();
private List<Flys> enemy = new LinkedList<Flys>();//定义父类集合,可用于存放大小敌机和蜜蜂(对象的向上转型)
private List<Bullet> biu = new LinkedList<Bullet>();//子弹数组
private List<Skill> sk = new LinkedList<Skill>();
long time = 18;
public World(JFrame frame) {
//设置背景颜色
setBackground(Color.BLACK);
//创建鼠标监听器(固定格式)
//1.创建鼠标适配器
MouseAdapter adpter = new MouseAdapter() {
//2.确定需要监听的鼠标事件
public void mouseMoved(MouseEvent e) {
//获取鼠标的XY坐标
//让英雄机随着鼠标一起移动
int mx = e.getX();
int my =e.getY();
if(h.isLife()&&state==RUNNING) {
//让英雄级移动到鼠标的位置上,
h.moveMouse(mx, my);
}
//刷新界面,将英雄机绘制到新的位置上
repaint();
}
};
//3.将适配器加入到监听器中
addMouseListener(adpter);
addMouseMotionListener(adpter);
//使用键盘监听器
//1创建键盘适配器
KeyAdapter kb = new KeyAdapter(){
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();//获取按下的键盘值
//按下A召唤支援
if(key == KeyEvent.VK_A){
h.help =true;
}
if(key == KeyEvent.VK_Z)
h.help = false;
//按下M播放背景音乐
if(key == KeyEvent.VK_M){
au = new AudioPlayer(file);
au.start();
}
//按下N停止背景音乐
if(key == KeyEvent.VK_N)
au.stop();
if(key == KeyEvent.VK_S){
Music.ac.play();
sk.add(new Skill(h.x-100,h.y-100));
}
if(key == KeyEvent.VK_X){
if(robot.isEmpty()){
robot.add(new Robot(-100,100));
}
rindex=1;
//System.out.println(robot.size());
}
if(key == 32){
switch(state){
case START:
state = RUNNING;
au.start();
//Music.bg.play();
break;
case GAMEOVER:
case CLEAR:
state = START;
score = 0;
s = new Sky();
h = new Hero();
enemy = new LinkedList<Flys>();
biu = new LinkedList<Bullet>();
boss = new LinkedList<BossAirplane>();
eb = new LinkedList<EBullet>();
count = 1;
Flys.k = 0;
record = true;
rindex = 0;
//bcount = 0;//用于记录bOSS技能音效
rindex = 0;//机器人开枪
sindex = 0;//计算超级奖励出现时间
awardindex = 0;//记录奖励出现时间
scount = 1;
}
}
if(key == KeyEvent.VK_0){
state = PAUSE;
}
if(key == KeyEvent.VK_9){
state = RUNNING;
}
}
};
frame.addKeyListener(kb);
}
public void action(){
new Thread(new Runnable(){
@Override
public void run() {
while(true){
if(state==RUNNING){
try {
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!sa.isEmpty()){
getSuperAward();
}
SuperAwardEnter();
rindex++;
AllStep();//移动方法
BossEnter();//BOSS入场方法
EnemyEnter();//调用敌人入场的方法
BulletEnter();//调用子弹入场的方法
isHit();//判断撞击的方法
heroHit();//英雄机碰撞方法
if(boss!=null){
BossHit();//boss机子弹碰撞的方法
hitBoss();//子弹击中BOSS
EBulletEnter();//调用BOSS机子弹入场方式
BossHitHero();
}
if(!sk.isEmpty()){
skillHit();//技能判定调用
}
if(!at.isEmpty()){
getAward();
}
}
repaint();
}
}
}).start();
}
/**
* 超级奖励出现时间
*/
int heropindex = 0;
public void SuperAwardEnter(){
sindex++;
awardindex++;
if(h.protect)
heropindex++;
if(sindex%1300==0){
sa.add(new SuperAward());
sindex=1;
}
if(awardindex%800==0){
sa.add(new SuperAward(1));
awardindex=1;
}
if(heropindex%400==0&&h.protect){
h.protect = false;
heropindex=1;
}
}
/**
* 普通奖励获得方法
*/
public void getAward(){
for (int i = 0; i < at.size(); i++) {
AwardType a = at.get(i);
if(h.hit(at.get(i))){
switch(at.get(i).type){
case 0:
//生命值小于30则增加生命值
if(h.life<=30)
h.addLife();
//生命值大于10则增加五分
else
score+=5;
at.remove(a);
break;
case 1:
//如果火力值小于5则增加火力
if(h.fire<=5)
h.addFire();
//如果火力值大于5则增加五分
else
score += 5;
at.remove(a);
break;
}
}
}
}
/**
* 超级奖励获得方法
*/
public void getSuperAward(){
for (int i = 0; i < sa.size(); i++) {
SuperAward s = sa.get(i);
if(s.hit(h)){
//判断奖励类型
if(s.type==0)
h.fire=5;
else
h.protect=true;
sa.remove(s);
}
}
}
/**
* 敌人和超级奖励移动的方法
*/
public void AllStep(){
s.step();
for (int i = 0; i < enemy.size(); i++) {
if(enemy.get(i).isLife()){
enemy.get(i).step();
}
}
for (int i = 0; i < sa.size(); i++) {
SuperAward s = sa.get(i);
if(!s.isBound()){
s.step();
}
}
if(!boss.isEmpty())
boss.get(0).step5();
if(!at.isEmpty()){
for (int i = 0; i < at.size(); i++) {
at.get(i).step();
}
}
}
/**
* 技能检测
*/
public void skillHit(){
for (int j = 0; j < enemy.size(); j++) {
Flys f = enemy.get(j);
f.goDead();
score += 2;
count +=5;
enemy.remove(f);
}
}
/**
* 检测Boss机与子弹碰撞的方法
* BOSS机血量为后更换图片加快时间进入下一关
*/
protected void hitBoss() {
for (int i = 0; i < biu.size(); i++) {
Bullet b = biu.get(i);
for (int j = 0; j < boss.size(); j++) {
BossAirplane bo = boss.get(0);
if(boss.get(0).isLife()&&b.isLife()&&b.hit(bo)){
biu.remove(b);
bo.hp--;
if(bo.hp<=0){
bo.goDead();
au.stop();
if(Flys.k<=3){
Flys.k ++;
}else
state= CLEAR;
switch(Flys.k){
case 1:
au = new AudioPlayer(file1);
break;
case 2:
au = new AudioPlayer(file3);
break;
case 3:
au = new AudioPlayer(file4);
break;
case 4:
au = new AudioPlayer(file5);
break;
}
au.start();
count = 1;
score += 500+Flys.k*100;
eb.clear();
s.setChange(Flys.k);
}
}
}
}
}
/**
* 检测BOSS机与英雄机碰撞
*/
int peng = 0;
public void BossHitHero(){
for (int i = 0; i < boss.size(); i++) {
BossAirplane b = boss.get(0);
if(b.hit(h)){
peng++;
if(peng%40==0){
if(!h.protect){
h.clearFire();
h.subLife();
}
}
if(h.getLife()==0){
h.goDead();
state = GAMEOVER;
}
}
}
}
/**
* 检测BOSS机子弹碰撞
*/
protected void BossHit() {
for (int i = 0; i < eb.size(); i++) {
EBullet e = eb.get(i);
if(h.isLife()&&e.hit(h)){
if(!h.protect){
h.clearFire();
h.subLife();
}
if(h.getLife()==0){
h.goDead();
state = GAMEOVER;
}
eb.remove(e);
}
}
}
/**
* 英雄机检测碰撞的方法
*/
protected void heroHit() {
for (int i = 0; i < enemy.size(); i++) {
Flys f = enemy.get(i);
if(h.isLife()&&f.isLife()&&f.hit(h)){
if(!h.protect){
h.clearFire();
h.subLife();
}
f.goDead();
if(h.getLife()==0){
h.goDead();
state = GAMEOVER;
}
}
}
}
/**
* 实现敌机和子弹碰撞
*/
int ecount = 0;
protected void isHit() {
for (int i = 0; i < enemy.size(); i++) {
Flys f = enemy.get(i);
for (int j = 0; j < biu.size(); j++) {
Bullet bu = biu.get(j);
if(bu.isLife()&&f.isLife()&&bu.hit(f)){
ecount++;
biu.remove(bu);
j--;
f.Hp();
if(f.Hp()<=0){
count++;
BossEnter();
f.goDead();
if(ecount%2==0)
Music.fs.play();
if(f instanceof EnemyGetScore){
EnemyGetScore es = (EnemyGetScore)f;
score +=es.getscore();
}
if(f instanceof EnemyAward){
EnemyAward es = (EnemyAward)f;
switch(es.getAwardType()){
case 0:
// if(h.getLife()<=10){
// h.addLife();
// }
at.add(new AwardType(f.x,f.y,0));
break;
case 1:
// h.addFire();
at.add(new AwardType(f.x,f.y,1));
break;
//default:
}
}
}
}
}
}
}
/**
* Boss机入场方式
*/
public void BossEnter(){
if(count%50==0&&Flys.k==0&&boss.isEmpty()){
boss.add(new BossAirplane());
count++;
au.stop();
au = new AudioPlayer(file2);
au.start();
}
if(count%50==0&&Flys.k==1&&boss.isEmpty()){
boss.add(new BossAirplane());
count++;
au.stop();
au = new AudioPlayer(file2);
au.start();
}
if(count%50==0&&Flys.k==2&&boss.isEmpty()){
boss.add(new BossAirplane());
count++;
au.stop();
au = new AudioPlayer(file2);
au.start();
}
if(count%50==0&&Flys.k==3&&boss.isEmpty()){
boss.add(new BossAirplane());
count++;
au.stop();
au = new AudioPlayer(file2);
au.start();
}
if(count%50==0&&Flys.k==4&&boss.isEmpty()){
boss.add(new BossAirplane());
count++;
au.stop();
au = new AudioPlayer(file2);
au.start();
}
}
/**
* BOSS机子弹入场方式
*/
int ebulletindex = 0;
protected void EBulletEnter() {
if(!boss.isEmpty()){
for (int i = 0; i < boss.size(); i++) {
if(boss.get(0).isLife()){
ebulletindex++;
}
if(ebulletindex==160&&Flys.k!=4){
for (int j = 0; j < boss.get(0).shoot().size(); j++) {
eb.add(boss.get(0).shoot().get(j));
ebulletindex = 0;
if(Flys.k==0)
Music.ly.play();
else if(Flys.k==1)
Music.jf.play();
else if(Flys.k==2)
Music.gd.play();
else{
Music.dj.play();
}
}
}else if(ebulletindex==160&&Flys.k==4){
for (int j = 0; j < boss.get(0).shoot().size(); j++) {
eb.add(boss.get(0).shoot().get(j));
Music.my.play();
ebulletindex = 0;
}
}
}
}
}
/**
* 子弹入场的方法
* 每10ms走一次
*/
public int bulletindex = 0;
protected void BulletEnter() {
bulletindex++;
if(bulletindex == 8){
for (int i = 0; i < h.shoot().size(); i++) {
biu.add(h.shoot().get(i));
}
bulletindex = 0;
}
}
/**
* 敌人入场的方法
*/
int index = 0;
private int num = 0;
protected void EnemyEnter() {
num++;
if(num==13){
index++;
if(index<4){
Flys f = new Airplane();
enemy.add(f);
}else if(index<7){
Flys f = new BigAirplane();
enemy.add(f);
}else if(index<8){
Flys f = new Bee();
enemy.add(f);
}
else{
index = 0;
}
num = 0;
}
}
//int hpindex = 0;
int[] arr = new int[3];
int[] arr2 = new int[3];
boolean record = true;
/**
* 画图的方法
*/
@Override
public void paint(Graphics g) {
g.setFont(new Font("楷体",Font.BOLD,30));
g.setColor(Color.yellow);
if(state!=PAUSE){
s.paint(g);
//s.step();
if(!sk.isEmpty()){
g.drawImage(Images.skill, sk.get(0).x-100, sk.get(0).y, sk.get(0).width, sk.get(0).high,null);
sk.get(0).step();
if(sk.get(0).y<=-1000)
sk.remove(0);
}
if(!biu.isEmpty()){
for (int i = 0; i < biu.size(); i++) {
Bullet b = biu.get(i);
if(b.isBound()){
biu.remove(b);
i--;
}
else{
b.paint(g);
b.step();
}
}
}
for (int i = 0; i < enemy.size(); i++) {
Flys f = enemy.get(i);
if(f.isBound()||f.isRemove()){
enemy.remove(f);
i--;
}
else{
f.paint(g);
}
}
if(!at.isEmpty()){
for (int i = 0; i < at.size(); i++) {
AwardType a = at.get(i);
a.paint(g);
}
}
g.drawString("SCORE: "+score, 20, 30);
g.drawString("关卡:"+(Flys.k+1), 1050, 30);
//画BOSS敌机
for (int i = 0; i < boss.size(); i++) {
BossAirplane b = boss.get(i);
if(!b.isRemove()){
b.paint(g);
g.setColor(Color.red);
g.fillRect(45, 40, b.hp*3, 30);
g.drawImage(b.getImage(), 5, 30,40,40,null);
//boss.remove(b);
}else{
boss.remove(b);
}
// bcount++;
if(!eb.isEmpty()){
for (int j = 0; j < eb.size(); j++) {
EBullet e = eb.get(j);
e.paint(g);
e.step();
}
}
}
for (int i = 0; i < sa.size(); i++) {
SuperAward s = sa.get(i);
if(s.isBound()){
sa.remove(s);
}else{
s.paint(g);
}
}
/**
* 画机器人
*/
if(!robot.isEmpty()){
for (int i = 0; i < robot.size(); i++) {
Robot r = robot.get(0);
r.paint(g);
r.step();
if(rindex%20==0){
biu.add(new Bullet(r.x+150,r.y+90,120,1));
biu.add(new Bullet(r.x+150,r.y+60,120,1));
}
if(rindex%400==0){
robot.remove(r);
rindex=0;
}
}
}
g.drawImage(h.getImage(), h.x, h.y,h.width,h.high,null );
if(h.isLife()){
g.setColor(Color.CYAN);
g.drawRect(h.x+20, h.y+200, 140, 15);
g.fillRect(h.x+20, h.y+200, h.life*5,15);
}
switch(state){
case START:
g.drawImage(Images.start, 0, 0, 1200, 1000,null);
g.drawString("英雄出世逆流行险---", 200, 200);
g.drawString("按下Space开启征程吧!", 200, 400);
break;
case GAMEOVER:
au.stop();
h.paint(g);
try {
if(record){
arr = ReadScore.read(score);
ReadScore.write(arr);
record = false;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.drawString("曾经沧海难为水", 450, 200);
g.drawString("除却巫山不是云", 450, 300);
g.drawString("勇士,再接再厉!", 450, 400);
g.drawImage(Images.paihang,1050,50,null);
g.drawString("1st:"+arr[0], 1050, 200);
g.drawString("2st:"+arr[1], 1050, 250);
g.drawString("3st:"+arr[2], 1050, 300);
g.drawImage(Images.gameover, 400, 400, 300, 200,null);
// g.setColor(Color.red);
break;
case CLEAR:
state = CLEAR;
au.stop();
enemy.clear();
boss.clear();
g.setColor(Color.yellow);
try {
if(record){
arr2 = ReadScore.read(score);
ReadScore.write(arr2);
record = false;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.drawString("恭喜通关", 500, 200);
g.drawImage(Images.paihang,1050,50,null);
g.drawString("1st:"+arr2[0], 1050, 200);
g.drawString("2st:"+arr2[1], 1050, 250);
g.drawString("3st:"+arr2[2], 1050, 300);
g.drawImage(Images.tongguan, 350, 250, 450, 450, null);
}
repaint();
if(h.help == true){
g.drawImage(Images.helphero, h.x-110, h.y+20,Images.helphero.getWidth(),Images.helphero.getHeight(),null );
g.drawImage(Images.helphero, h.x+90, h.y+20,Images.helphero.getWidth(),Images.helphero.getHeight(),null );
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(WIDTH, HIGH);
frame.setLocationRelativeTo(null);
frame.setTitle("BiuBiuBiu");
//设置窗口可见
frame.setVisible(true);
//设置窗口大小不可变
frame.setResizable(false);
World world = new World(frame);
frame.add(world);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
world.action();
}
}
代码写得并不好,只是简单的实现了功能,还有许多需要优化的地方
核心代码介绍就写到这里,还有一些其他的类没有贴出来,想要源文件的朋友们可以访问链接或者私信我
[提取码:8080]https://pan.baidu.com/s/1JhxwEbtVQKE2wncR0wuxkQ
本文地址:https://blog.csdn.net/qq_41880807/article/details/109955759