游戏骨架:
1、在src中创建Gamewin类作为程序运行的入口,创建Gameobj类作为父类,并以此为基础创建出游戏所需的飞机、敌机、子弹、爆炸等类,创建Gameutil类存放各个对象的集合,并调用对象对应的图片
2、在游戏文件夹中创建image文件夹,用于存放对象的图片
3、在各个obj类中重写构造方法、重绘方法等
4、在Gamewin中创建窗口,声明state变量控制游戏状态,count记录重绘次数,循环creat方法和repaint方法创造新的对象并重绘界面,用计时器控制重绘速率
5、监听键盘和鼠标,键盘控制暂停,鼠标控制飞机位置和点击开始游戏
Gamewin类(主界面)
package com.game;
import com.game.obj.*;
import com.game.utils.gameutils;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import static com.game.obj.Bossobj.life;
import static com.game.utils.gameutils.*;
public class gamewin extends JFrame {
//游戏状态 0未开始 1进行中 2暂停 3游戏失败 4游戏通关
public static int state=0;//游戏的默认状态
//计分数
public static int score=0;
Image offSreenimage=null;
int width=600;
int height=600;
//游戏的重绘次数
public static int count=1;
//敌机出现的数量
int enemyCount=0;
//背景图图像的移动
Bgobj bgobj=new Bgobj(gameutils.bgimg,0,-400,0.5);
//我方飞机的对象
public Planeobj planeobj =new Planeobj(gameutils.planeimg,290,550,30,40,0,this);
//boss对象
public Bossobj bossobj =null;
public void launch(){
//设置窗口大小
this.setSize(width,height);
//设置界面置顶
setAlwaysOnTop(true);
//设置窗口位置居中
this.setLocationRelativeTo(null);
//用户不能调整界面
setResizable(false);
//设置关闭模式
setDefaultCloseOperation(3);
//设置窗口标题
this.setTitle("飞机大战");
//设置窗口是否可见
this.setVisible(true);
gameobjList.add(bgobj);
gameobjList.add(planeobj);
//鼠标点击
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//在未开始并且点击鼠标左键,
if(e.getButton()==1&&state==0){
state=1;
repaint();
} else if (e.getButton() == 1 && (state == 3 || state == 4)) {
count=1;
enemyCount=0;
score=0;
removeobjList.clear();
gameobjList.clear();
shellobjList.clear();
enemyobjList.clear();
bulletobjList.clear();
explodeobjList.clear();
gameobjList.add(bgobj);
gameobjList.add(planeobj);
// bulletimg = null;
//boss对象
bossobj =null;
Planeobj.planelife=5;
life=150;
state=1;
repaint();
}
}
});
//游戏的暂停功能
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//按下空格键,空格键的代码为32
if(e.getKeyCode() == 32){
switch (state){
case 1:
state=2;
break;
case 2:
state=1;
break;
default:
}
} else if (e.getKeyCode() == 90) {
Planeobj.planelife++;
} else if (e.getKeyCode() == 81) {
removeobjList.clear();
enemyobjList.clear();
bulletobjList.clear();
explodeobjList.clear();
shellobjList.clear();
} else if (e.getKeyCode() == 69) {
}
}
});
while(true){
if(state==1){
create();
repaint();
}
try {
Thread.sleep(15);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
@Override
public void paint(Graphics g) {
if(offSreenimage==null){
offSreenimage=createImage(width,height);
}
//获取offScrenimage的画笔对象
Graphics gimage=offSreenimage.getGraphics();
//填充一个宽六百,高六百的区域
gimage.fillRect(0,0,width,height);
//游戏未开始
if(state==0){
gimage.drawImage(gameutils.bgimg,0,0,null);
gimage.drawImage(gameutils.bossimg,230,100,null);
gimage.drawImage(gameutils.planeimg,290,400,null);
gameutils.drawWord(gimage,"点击开始游戏",Color.yellow,40,180,300);
// gimage.setColor(Color.yellow);
// gimage.setFont(new Font("仿宋",Font.BOLD,40));
// gimage.drawString("点击开始游戏",180,300);
}
//游戏开始
if(state==1){
gameobjList.addAll(gameutils.explodeobjList);
for(int i = 0; i< gameobjList.size(); i++){
gameobjList.get(i).paintself(gimage);
}
gameobjList.removeAll(removeobjList);
}
//游戏失败
if(state==3) {
gimage.drawImage(gameutils.explodeimg, planeobj.getX() - 35, planeobj.getY() - 50, null);
gameutils.drawWord(gimage,"GAME OVER",Color.red,40,180,250);
// gimage.setColor(Color.red);
// gimage.setFont(new Font("仿宋", Font.BOLD, 40));
// gimage.drawString("GAME OVER", 180, 300);
gameutils.drawWord(gimage,"点击界面后重新开始",Color.red,40,110,300);
}
//游戏通关
if(state==4) {
gimage.drawImage(gameutils.explodeimg, bossobj.getX() + 35, bossobj.getY() + 50, null);
gameutils.drawWord(gimage, " Game Win", Color.red, 40, 180, 250);
gameutils.drawWord(gimage,"点击界面后重新开始",Color.red,40,110,300);
}
//绘制得分
gameutils.drawWord(gimage,score+"分",Color.green,30,30,80);
//绘制生命值
gameutils.drawWord(gimage,"血量:"+Planeobj.planelife,Color.red,30,30,560);
//把新图片一次性绘制到主窗口中
g.drawImage(offSreenimage,0,0,null);
count++;
}
//创建方法用来批量生成子弹和敌机
void create(){
//我方子弹 除以10是为了控制子弹的速率
if(count%15==0){
shellobjList.add(new Shellobj(gameutils.shellimg,planeobj.getX()+4,planeobj.getY()-16,14,29,8,this));
gameobjList.add(shellobjList.get(shellobjList.size()-1));
}
//敌方战机
if(count%15==0){
enemyobjList.add(new Enemyobj(enemyimg,(int)(Math.random()*12)*50,0,50,50,3,this));
gameobjList.add(enemyobjList.get(enemyobjList.size()-1));
enemyCount++;
}
//小boss
if (count%20==0){
littleobjList.add(new Littleobj(littleimg,(int)(Math.random()*12)*50,0,40,40,5,this));
gameobjList.add(littleobjList.get(littleobjList.size()-1));
}
//turn飞机
if (count%70==0){
turnobjList.add(new Turnobj(turnimg,(int)(Math.random()*12)*50,0,40,40,2,this));
gameobjList.add(turnobjList.get(turnobjList.size()-1));
}
//敌方boss子弹
//直到boss出现的时候才会生成子弹
if (life <= 50) {
// if(count%20==0 && bossobj !=null){
// gameutils.bulletobjList.add(new Bulletobj(gameutils.bulletimg,bossobj.getX()+70,bossobj.getY()+87,20,47,5,this));
// gameobjList.add(gameutils.bulletobjList.get(gameutils.bulletobjList.size()-1));
// }
if(count%40==0 && bossobj !=null) {
canisterobj1List.add(new Canisterobj1(canisterimg1,bossobj.getX()+70,bossobj.getY()+87,20,20,2,this));
gameobjList.add(canisterobj1List.get(canisterobj1List.size()-1));
canisterobj2List.add(new Canisterobj2(canisterimg2,bossobj.getX()+70,bossobj.getY()+87,20,20,2,this));
gameobjList.add(canisterobj2List.get(canisterobj2List.size()-1));
canisterobj3List.add(new Canisterobj3(canisterimg3,bossobj.getX()+70,bossobj.getY()+87,20,20,2,this));
gameobjList.add(canisterobj3List.get(canisterobj3List.size()-1));
}
} else if (life>=100) {
if(count%30==0 && bossobj !=null) {
gameutils.bulletobjList.add(new Bulletobj(gameutils.bulletimg, bossobj.getX() + 70, bossobj.getY() + 87, 20, 47, 4, this));
gameobjList.add(gameutils.bulletobjList.get(gameutils.bulletobjList.size() - 1));
}
} else if (life < 100 && life > 50) {
if(count%20==0 && bossobj !=null) {
gameutils.bulletobjList.add(new Bulletobj(gameutils.bulletimg, bossobj.getX() + 70, bossobj.getY() + 87, 20, 47, 3, this));
gameobjList.add(gameutils.bulletobjList.get(gameutils.bulletobjList.size() - 1));
}
}
if( score>30 && bossobj == null ){
bossobj=new Bossobj(gameutils.bossimg,250,20,145,107,3,this);
gameobjList.add(bossobj);
}
}
public static void main(String[] args) {
gamewin Gamewin=new gamewin();
Gamewin.launch();
}
}
Gameutil类(工具类)
package com.game.utils;
import com.game.obj.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class gameutils {
//背景图片
public static Image bgimg =Toolkit.getDefaultToolkit().getImage("image/背景.jpeg");
//boss图片
public static Image bossimg =Toolkit.getDefaultToolkit().getImage("image/boss.png");
//爆炸图片
public static Image explodeimg =Toolkit.getDefaultToolkit().getImage("image/explode/e11.gif");
//我方飞机图片
public static Image planeimg =Toolkit.getDefaultToolkit().getImage("image/plane.png");
//我方子弹图片
public static Image shellimg =Toolkit.getDefaultToolkit().getImage("image/shell.png");
//敌方boss子弹图片
public static Image bulletimg =Toolkit.getDefaultToolkit().getImage("image/boss子弹.png");
//敌方boss的喷子
public static Image canisterimg1 =Toolkit.getDefaultToolkit().getImage("image/canister.png");
public static Image canisterimg2 =Toolkit.getDefaultToolkit().getImage("image/canister.png");
public static Image canisterimg3 =Toolkit.getDefaultToolkit().getImage("image/canister.png");
//敌方飞机的图片
public static Image enemyimg =Toolkit.getDefaultToolkit().getImage("image/enemy.png");
//小boss图片
public static Image littleimg =Toolkit.getDefaultToolkit().getImage("image/little.png");
//turn飞机图片
public static Image turnimg =Toolkit.getDefaultToolkit().getImage("image/turn.png");
//要删除元素的集合
public static List<Gameobj> removeobjList =new ArrayList<>();
//所有物体游戏的集合
public static List<Gameobj> gameobjList =new ArrayList<>();
//我方子弹的集合
public static List<Shellobj> shellobjList =new ArrayList<>();
//敌方飞机的集合
public static List<Enemyobj> enemyobjList =new ArrayList<>();
//小boss集合
public static List<Littleobj> littleobjList =new ArrayList<>();
//turn飞机的集合
public static List<Turnobj> turnobjList =new ArrayList<>();
//敌方boss方子弹的集合
public static List<Bulletobj> bulletobjList =new ArrayList<>();
//敌方boss喷子子弹的合集
public static List<Canisterobj1> canisterobj1List =new ArrayList<>();
public static List<Canisterobj2> canisterobj2List =new ArrayList<>();
public static List<Canisterobj3> canisterobj3List =new ArrayList<>();
//爆炸图片的集合
public static List<Explodeobj> explodeobjList =new ArrayList<>();
//绘制字符串的工具类
public static void drawWord(Graphics gImage,String str,Color color,int size,int x,int y){
gImage.setColor(color);
gImage.setFont(new Font("仿宋",Font.BOLD,size));
gImage.drawString(str,x,y);
}
}
Gameobj类(obj父类)
package com.game.obj;
import com.game.gamewin;
import java.awt.*;
//游戏类父类的编写
public class Gameobj {
Image img;
int x;
int y;
int width;
int height;
double speed;//移动速度
gamewin frame;//窗口的引用
public Image getImg() {
return img;
}
public void setImg(Image img) {
this.img = img;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public Gameobj(int x, int y) {
this.x = x;
this.y = y;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public gamewin getFrame() {
return frame;
}
public void setFrame(gamewin frame) {
this.frame = frame;
}
//有参构造和无参构造函数
public Gameobj() {
}
public Gameobj(Image img, int x, int y, double speed) {
this.img = img;
this.x = x;
this.y = y;
this.speed = speed;
}
public Gameobj(Image img, int x, int y, int width, int height, double speed, gamewin frame) {
this.img = img;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.speed = speed;
this.frame = frame;
}
//绘制自身
public void paintself(Graphics gImage){
gImage.drawImage(img,x,y,null);
}
//绘制矩形的方法用来碰撞检测
public Rectangle getrect(){
return new Rectangle(x,y,width,height);
}
}
Bgobj类(背景)
package com.game.obj;
import java.awt.*;
public class Bgobj extends Gameobj {
//重写构造方法和paintself放法
public Bgobj() {
super();
}
public Bgobj(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
@Override
public void paintself(Graphics gImage) {
super.paintself(gImage);
y+=speed;
if(y>=0){
y=-400;
}
}
}
Bossobj(boss)
package com.game.obj;
import com.game.gamewin;
import com.game.utils.gameutils;
import java.awt.*;
public class Bossobj extends Gameobj{
//定义敌方boss的生命值
public static int life=150;
public Bossobj(Image img, int x, int y, int width, int height, double speed, gamewin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintself(Graphics gImage) {
super.paintself(gImage);
//控制敌方boss在整个窗口中
if(x>550 || x<0){
speed=-speed;
}
x+=speed;
for(Shellobj shellobj: gameutils.shellobjList){
//检测子弹和敌方boss碰撞
if(this.getrect().intersects(shellobj.getrect())){
//子弹击中敌方飞机爆炸图
Explodeobj explodeobj=new Explodeobj(shellobj.x,y+20);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
shellobj.setX(-100);
shellobj.setY(100);
gameutils.removeobjList.add(shellobj);
life-=3;
}
if(life<=0){
gamewin.state=4;//表明游戏通关
}
//血条的白色背景
gImage.setColor(Color.white);
gImage.fillRect(20,40,300,10);
//血条的绘制
gImage.setColor(Color.red);
gImage.fillRect(20,40,life*2,10);
//一个整数除以另一个整数,如果结果为小于一,它会算为0
// gImage.fillRect(20,40,life/10 *100,50);
}
}
@Override
public Rectangle getrect() {
return super.getrect();
}
}
Bulletobj(boss子弹)
package com.game.obj;
import com.game.gamewin;
import com.game.utils.gameutils;
import java.awt.*;
public class Bulletobj extends Gameobj{
public Bulletobj(Image img, int x, int y, int width, int height, double speed, gamewin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintself(Graphics gImage) {
super.paintself(gImage);
y+=speed;
//敌方boss子弹超出窗口,判断为y>600,调整坐标为-300,300
if(y>600){
this.x=-300;
this.y=300;
gameutils.removeobjList.add(this);
}
//敌方boss子弹和我方飞机的碰撞检测
if(this.getrect().intersects(this.frame.planeobj.getrect())&&gamewin.count >=Planeobj.miss + 100){
if (Planeobj.planelife==1) {
//被击中爆炸图
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
this.x=-100;
this.y=100;
gameutils.removeobjList.add(this);
gamewin.state=3;
} else if (Planeobj.planelife > 1) {
Planeobj.planelife--;
Planeobj.miss = gamewin.count;
//被击中爆炸图
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
gameutils.removeobjList.add(this);
}
}
}
@Override
public Rectangle getrect() {
return super.getrect();
}
}
Canisterobj1(boss升级子弹)
package com.game.obj;
import com.game.gamewin;
import com.game.utils.gameutils;
import java.awt.*;
public class Canisterobj1 extends Gameobj{
public Canisterobj1(Image img, int x, int y, int width, int height, double speed, gamewin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintself(Graphics gImage) {
super.paintself(gImage);
y+=2*speed;
x-=speed;
//敌方boss子弹超出窗口,判断为y>600,调整坐标为-300,300
if(y>600) {
this.x = -300;
this.y = 300;
gameutils.removeobjList.add(this);
}
//敌方boss子弹和我方飞机的碰撞检测
if(this.getrect().intersects(this.frame.planeobj.getrect())&&gamewin.count >=Planeobj.miss + 100){
if (Planeobj.planelife==1) {
gamewin.state=3;
Planeobj.miss = gamewin.count;
//被击中爆炸图
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
this.x=-100;
this.y=100;
gameutils.removeobjList.add(this);
} else if (Planeobj.planelife > 1) {
Planeobj.planelife--;
Planeobj.miss = gamewin.count;
//被击中爆炸图
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
// this.x=-100;
// this.y=100;
gameutils.removeobjList.add(this);
}
}
}
@Override
public Rectangle getrect() {
return super.getrect();
}
}
Canisterobj2(boss升级子弹)
package com.game.obj;
import com.game.gamewin;
import com.game.utils.gameutils;
import java.awt.*;
public class Canisterobj2 extends Gameobj{
public Canisterobj2(Image img, int x, int y, int width, int height, double speed, gamewin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintself(Graphics gImage) {
super.paintself(gImage);
y+=2*speed;
//敌方boss子弹超出窗口,判断为y>600,调整坐标为-300,300
if(y>600) {
this.x = -300;
this.y = 300;
gameutils.removeobjList.add(this);
}
//敌方boss子弹和我方飞机的碰撞检测
if(this.getrect().intersects(this.frame.planeobj.getrect())&&gamewin.count >=Planeobj.miss + 100){
if (Planeobj.planelife==1) {
gamewin.state=3;
Planeobj.miss = gamewin.count;
//被击中爆炸图
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
this.x=-100;
this.y=100;
gameutils.removeobjList.add(this);
} else if (Planeobj.planelife > 1) {
Planeobj.planelife--;
Planeobj.miss = gamewin.count;
//被击中爆炸图
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
this.x=-100;
this.y=100;
gameutils.removeobjList.add(this);
}
}
}
@Override
public Rectangle getrect() {
return super.getrect();
}
}
Canisterobj3(boss升级子弹)
package com.game.obj;
import com.game.gamewin;
import com.game.utils.gameutils;
import java.awt.*;
public class Canisterobj3 extends Gameobj {
public Canisterobj3(Image img, int x, int y, int width, int height, double speed, gamewin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintself(Graphics gImage) {
super.paintself(gImage);
y+=2*speed;
x+=speed;
//敌方boss子弹超出窗口,判断为y>600,调整坐标为-300,300
if(y>600) {
this.x = -300;
this.y = 300;
gameutils.removeobjList.add(this);
}
//敌方boss子弹和我方飞机的碰撞检测
if(this.getrect().intersects(this.frame.planeobj.getrect())&&gamewin.count >=Planeobj.miss + 100){
if (Planeobj.planelife==1) {
gamewin.state=3;
Planeobj.miss = gamewin.count;
//被击中爆炸图
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
this.x=-100;
this.y=100;
gameutils.removeobjList.add(this);
} else if (Planeobj.planelife > 1) {
Planeobj.planelife--;
Planeobj.miss = gamewin.count;
//被击中爆炸图
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
this.x=-100;
this.y=100;
gameutils.removeobjList.add(this);
}
}
}
@Override
public Rectangle getrect() {
return super.getrect();
}
}
Enemyobj(普通敌机)
package com.game.obj;
import com.game.gamewin;
import com.game.utils.gameutils;
import java.awt.*;
public class Enemyobj extends Gameobj{
public Enemyobj() {
super();
}
public Enemyobj(Image img, int x, int y, int width, int height, double speed, gamewin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintself(Graphics gImage) {
super.paintself(gImage);
y+=speed;
//敌我飞机的碰撞检测
if(this.getrect().intersects(this.frame.planeobj.getrect())&&gamewin.count >= Planeobj.miss + 100){
if (Planeobj.planelife==1) {
gamewin.state=3;
Planeobj.miss = gamewin.count;
//被击中爆炸图
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
this.x=-100;
this.y=100;
gameutils.removeobjList.add(this);
} else if (Planeobj.planelife > 1) {
Planeobj.planelife--;
Planeobj.miss = gamewin.count;
//被击中爆炸图
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
this.x=-100;
this.y=100;
gameutils.removeobjList.add(this);
}
}
//敌方飞机超出窗口,判断条件y>600,调整坐标为-200,200
if(y>600){
this.x=-200;
this.y=200;
gameutils.removeobjList.add(this);
}
//子弹和敌方飞机碰撞的检测
for (Shellobj shellobj: gameutils.shellobjList) {
if(this.getrect().intersects(shellobj.getrect())){
//子弹击中敌方飞机爆炸图
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
// shellobj.setX(-100);
// shellobj.setY(100);
//改变当前敌机的坐标
gameutils.removeobjList.add(shellobj);
gameutils.removeobjList.add(this);
//加分数
gamewin.score++;
}
}
}
@Override
public Rectangle getrect() {
return super.getrect();
}
}
Explodeobj(爆炸)
package com.game.obj;
import java.awt.*;
public class Explodeobj extends Gameobj{
static Image[] pic=new Image[16];
//爆炸图只显示一次
int explodeCount=0;
static {
for (int i=0;i<pic.length;i++){
pic[i]=Toolkit.getDefaultToolkit().getImage("image/explode/e"+(i+1)+".gif");
}
}
public Explodeobj(int x, int y) {
super(x, y);
}
@Override
public void paintself(Graphics gImage) {
if(explodeCount<16){
//绘制数组中的图片
img=pic[explodeCount];
super.paintself(gImage);
explodeCount++;
}
}
}
Littleobj(精英敌机,子弹免伤)
package com.game.obj;
import com.game.gamewin;
import com.game.utils.gameutils;
import java.awt.*;
public class Littleobj extends Gameobj{
public Littleobj() {
}
public Littleobj(Image img, int x, int y, int width, int height, double speed, gamewin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintself(Graphics gImage) {
super.paintself(gImage);
y+=speed;
//敌我飞机的碰撞检测
if(this.getrect().intersects(this.frame.planeobj.getrect())){
if (Planeobj.planelife==1) {
gamewin.state=3;
Planeobj.miss = gamewin.count;
//被击中爆炸图
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
this.x=-100;
this.y=100;
gameutils.removeobjList.add(this);
} else if (Planeobj.planelife > 1) {
Planeobj.planelife--;
Planeobj.miss = gamewin.count;
//被击中爆炸图
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
this.x=-100;
this.y=100;
gameutils.removeobjList.add(this);
} }
//敌方飞机超出窗口,判断条件y>600,调整坐标为-200,200
if(y>600){
this.x=-300;
this.y=300;
gameutils.removeobjList.add(this);
}
//子弹和敌方飞机碰撞的检测
for (Shellobj shellobj: gameutils.shellobjList) {
if(this.getrect().intersects(shellobj.getrect())){
shellobj.x=-100;
shellobj.y=100;
gameutils.removeobjList.add(shellobj);
//子弹击中敌方飞机爆炸图
// Explodeobj explodeobj=new Explodeobj(x,y);
// gameutils.explodeobjList.add(explodeobj);
// gameutils.removeobjList.add(explodeobj);
}
}
}
@Override
public Rectangle getrect() {
return super.getrect();
}
}
Planeobj(我的飞机)
package com.game.obj;
import com.game.gamewin;
import com.game.utils.gameutils;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Planeobj extends Gameobj {
public static int planelife = 5;
public static int miss=0;
@Override
public Image getImg() {
return super.getImg();
}
public Planeobj(Image planeimg, int x, int y, int width, int height, int speed, MouseAdapter mouseAdapter) {
super();
}
public Planeobj(Image img, int x, int y, int width, int height, double speed, gamewin frame) {
super(img, x, y, width, height, speed, frame);
planelife=5;
//飞机随着鼠标的移动而移动
this.frame.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
Planeobj.super.x=e.getX()-11;
Planeobj.super.y=e.getY()-16;
}
});
}
@Override
public void paintself(Graphics gImage) {
super.paintself(gImage);
//y-=speed;
if(this.frame.bossobj !=null && this.getrect().intersects(this.frame.bossobj.getrect())&&gamewin.count >= miss + 100){
if (planelife==1) {
gamewin.state=3;
miss = gamewin.count;
//被击中爆炸图
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
} else if (planelife > 1) {
planelife--;
miss = gamewin.count;
//被击中爆炸图
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
}
}
}
@Override
public Rectangle getrect() {
return super.getrect();
}
}
Shellobj(我的子弹)
package com.game.obj;
import com.game.gamewin;
import com.game.utils.gameutils;
import java.awt.*;
public class Shellobj extends Gameobj{
@Override
public Image getImg() {
return super.getImg();
}
public Shellobj() {
super();
}
public Shellobj(Image img, int x, int y, int width, int height, double speed, gamewin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintself(Graphics gImage) {
super.paintself(gImage);
y-=speed;
// 我方子弹的越界消失,条件y<0时,,改变后的坐标为-100,100
if(y<0){
this.x=-100;
this.y=100;
gameutils.removeobjList.add(this);
}
//检测子弹碰撞 bullet
for (Bulletobj bulletobj: gameutils.bulletobjList) {
if (this.getrect().intersects(bulletobj.getrect())) {
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
//删除子弹
bulletobj.x=-100;
bulletobj.y=100;
this.x=-100;
this.y=100;
gameutils.removeobjList.add(bulletobj);
gameutils.removeobjList.add(this);
}
}
//检测子弹碰撞 canister1
for (Canisterobj1 canisterobj1: gameutils.canisterobj1List) {
if (this.getrect().intersects(canisterobj1.getrect())) {
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
//删除子弹
canisterobj1.x=-100;
canisterobj1.y=100;
this.x=-100;
this.y=100;
gameutils.removeobjList.add(canisterobj1);
gameutils.removeobjList.add(this);
}
}
//检测子弹碰撞 canister2
for (Canisterobj2 canisterobj2: gameutils.canisterobj2List) {
if (this.getrect().intersects(canisterobj2.getrect())) {
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
//删除子弹
canisterobj2.x=-100;
canisterobj2.y=100;
this.x=-100;
this.y=100;
gameutils.removeobjList.add(canisterobj2);
gameutils.removeobjList.add(this);
}
}
//检测子弹碰撞 canister3
for (Canisterobj3 canisterobj3: gameutils.canisterobj3List) {
if (this.getrect().intersects(canisterobj3.getrect())) {
Explodeobj explodeobj=new Explodeobj(x,y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
//删除子弹
canisterobj3.x=-100;
canisterobj3.y=100;
this.x=-100;
this.y=100;
gameutils.removeobjList.add(canisterobj3);
gameutils.removeobjList.add(this);
}
}
}
@Override
public Rectangle getrect() {
return super.getrect();
}
}
Turnobj(精英飞机,可横向移动)
package com.game.obj;
import com.game.gamewin;
import com.game.utils.gameutils;
import java.awt.*;
public class Turnobj extends Gameobj{
public Turnobj() {
}
public Turnobj(Image img, int x, int y, int width, int height, double speed, gamewin frame) {
super(img, x, y, width, height, speed, frame);
if ((int) Math.round(Math.random()) == 1) {
setSpeed(-speed);
}
}
@Override
public void paintself(Graphics gImage) {
super.paintself(gImage);
y+=Math.abs(speed);
if(x>550 || x<0){
speed=-speed;
}
x+=2*speed;
//敌我飞机的碰撞检测
if(this.getrect().intersects(this.frame.planeobj.getrect())) {
if (Planeobj.planelife == 1) {
gamewin.state = 3;
Planeobj.miss = gamewin.count;
//被击中爆炸图
Explodeobj explodeobj = new Explodeobj(x, y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
this.x = -100;
this.y = 100;
gameutils.removeobjList.add(this);
} else if (Planeobj.planelife > 1) {
Planeobj.planelife--;
Planeobj.miss = gamewin.count;
//被击中爆炸图
Explodeobj explodeobj = new Explodeobj(x, y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
this.x = -100;
this.y = 100;
gameutils.removeobjList.add(this);
}
}
//子弹和敌方飞机碰撞的检测
for (Shellobj shellobj: gameutils.shellobjList) {
if (this.getrect().intersects(shellobj.getrect())) {
//子弹击中敌方飞机爆炸图
Explodeobj explodeobj = new Explodeobj(x, y);
gameutils.explodeobjList.add(explodeobj);
gameutils.removeobjList.add(explodeobj);
// shellobj.setX(-100);
// shellobj.setY(100);
//改变当前敌机的坐标
shellobj.x=-100;
shellobj.y=100;
this.x=-100;
this.y=100;
gameutils.removeobjList.add(shellobj);
gameutils.removeobjList.add(this);
//加分数
gamewin.score++;
}
}
//敌方飞机超出窗口,判断条件y>600,调整坐标为-200,200
if(y>600) {
this.x = -300;
this.y = 300;
gameutils.removeobjList.add(this);
}
}
@Override
public Rectangle getrect() {
return super.getrect();
}
}