ink 1 year ago
commit 029383c1e6

8
.idea/.gitignore vendored

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="openjdk-19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/plane.iml" filepath="$PROJECT_DIR$/plane.iml" />
</modules>
</component>
</project>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 152 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 408 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1007 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 246 B

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,15 @@
public class Main {
public static void main(String[] args) throws InterruptedException {
gameFrame_01 gameframe = new gameFrame_01();
gamePanel panel = new gamePanel(gameframe);
gameframe.addPanel(panel);
// 未父窗口添加监听事件
gameframe.addKeyListener(panel);
// 设置可见性
gameframe.setVisible(true);
panel.init();
}
}

@ -0,0 +1,94 @@
import javax.swing.*;
import java.awt.*;
//炸弹类
public class bomb extends fly {
//目标的坐标
public int targetX;
public int targetY;
//重新定义x,y类型为double类型
public double x;
public double y;
//xy方向上的运动速度
double speedX;
double speedY;
//运动方向
int directionX;
int directionY;
public bomb(int x,int y,int target_x,int target_y) {
this.x=x;
this.y=y;
this.height=16;
this.width=16;
this.speed=2;
this.targetX =target_x;
this.targetY =target_y;
icon = new ImageIcon("images/bomb.png");
//计算发射点到目标点的xy方向上的长度比,勾股定理
double temp = Math.sqrt(Math.pow(Math.abs(x-target_x),2)+Math.pow(Math.abs(y-target_y),2));
speedX = Math.abs(x-target_x)/temp*2;
speedY = Math.abs(y-target_y)/temp*2;
//判断运动方向direction_x为1向右移为0左移direction_y同理
if(target_x>x)
{
directionX =1;
}
if(target_x<x)
{
directionX =0;
}
if(target_y>y)
{
directionY =1;
}
if(target_y<y)
{
directionY =0;
}
}
@Override
public void drawImage(Graphics g) {
g.drawImage(icon.getImage(), (int)this.x,(int)this.y,this.height,this.width,null);
}
//炸弹的移动
@Override
public void move() {
switch (directionX)
{
case 1:
{
this.x+= speedX;
break;
}
case 0:
{
this.x-= speedX;
break;
}
}
switch (directionY)
{
case 1:
{
this.y+= speedY;
break;
}
case 0:
{
this.y-= speedY;
break;
}
}
}
}

@ -0,0 +1,83 @@
import javax.swing.*;
import java.awt.*;
//boss类
public class boss extends fly {
//boss生命值
public int life=25;
//记录boss初始生命值
public int firstLife=life;
//x轴上运动方向
int directionX;
//重新初始化
public void restart()
{
icon = new ImageIcon("images/boss.png");
this.height=42;
this.width=55;
this.speed=1;
this.directionX =1;
this.pWidth=pWidth;
this.pHeight=pHeight;
this.y=-height;
this.x=pWidth/2;
}
public boss(int pHeight,int pWidth) {
icon = new ImageIcon("images/boss.png");
this.height=42;
this.width=55;
this.speed=1;
this.directionX =1;
this.pWidth=pWidth;
this.pHeight=pHeight;
this.y=-height;
this.x=pWidth/2;
}
@Override
public void drawImage(Graphics g) {
g.drawImage(icon.getImage(),this.x,this.y,this.height,this.width,null);
}
//boss在x轴和y周上移动
@Override
public void move() {
if (this.y<this.height)
{
this.y+=speed;
}
switch (directionX)
{
case 1:
{
this.x+=speed;
break;
}
case 0:
{
this.x-=speed;
break;
}
}
if(this.x<0)
{
directionX =1;
}
if(this.x>pWidth-width)
{
directionX =0;
}
}
}

@ -0,0 +1,26 @@
import javax.swing.*;
import java.awt.*;
//我方子弹类
public class bullet extends fly {
//初始化
public bullet(int pHeight,int pWidth,int x,int y) {
icon = new ImageIcon("images/bullet2.png");
this.speed=2;
this.height=4;
this.width=8;
this.pHeight=pHeight;
this.pWidth=pWidth;
this.x=x;
this.y=y;
}
@Override
public void drawImage(Graphics g) {
g.drawImage(icon.getImage(), this.x,this.y,height,width,null);
}
@Override
public void move() {
this.y-=speed;
}
}

@ -0,0 +1,27 @@
import javax.swing.*;
import java.awt.*;
//爆炸类
public class crash extends fly {
// 记录爆炸存在时间
public int count=0;
public crash(int x,int y,int height, int width) {
this.x=x;
this.y=y;
this.height=height;
this.width=width;
icon=new ImageIcon("images/bloom.png");
}
@Override
public void drawImage(Graphics g) {
g.drawImage(icon.getImage(), this.x,this.y,this.height,this.width,null);
}
@Override
public void move() {
}
}

@ -0,0 +1,41 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
//敌机1类不能发射炮弹只能一直移动
public class enemy01 extends fly {
// 重新初始化
public void restart()
{
// 随机生成出现位置
Random random = new Random();
this.x=random.nextInt(pWidth/width)*width;
this.y=-random.nextInt(15)*height-height;
}
public enemy01(int pHeight,int pWidth) throws InterruptedException {
icon = new ImageIcon("images/enemy01.png");
this.speed=1;
this.height=30;
this.width=30;
this.pHeight=pHeight;
this.pWidth=pWidth;
// 随机生成出现位置
Random random = new Random();
this.x=random.nextInt(pWidth/width)*width;
this.y=-random.nextInt(15)*height-height;
}
@Override
public void drawImage(Graphics g) {
g.drawImage(icon.getImage(),this.x,this.y,height,width,null);
}
@Override
public void move() {
this.y+=speed;
}
}

@ -0,0 +1,43 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
//敌机二类,能发射炮弹和移动
public class enemy02 extends fly {
//重新初始化
public void restart()
{
// 随机生成出现位置
Random random = new Random();
this.x=random.nextInt(pWidth/width)*width;
this.y=-height-random.nextInt(5)*height;
}
public enemy02(int pHeight,int pWidth) throws InterruptedException {
icon = new ImageIcon("images/enemy02.png");
this.speed=1;
this.height=35;
this.width=35;
this.pHeight=pHeight;
this.pWidth=pWidth;
// 随机生成出现位置
Random random = new Random();
this.x=random.nextInt(pWidth/width)*width;
Thread.sleep(10);
this.y=-height-random.nextInt(5)*height;
}
@Override
public void drawImage(Graphics g) {
g.drawImage(icon.getImage(),this.x,this.y,height,width,null);
}
@Override
public void move() {
this.y+=speed;
}
}

@ -0,0 +1,92 @@
import javax.swing.*;
import java.awt.*;
//敌方的子弹类
public class enemyBullet extends fly {
//目标的坐标
public int target_x;
public int target_y;
//重新定义x,y类型为double类型
public double x;
public double y;
//xy方向上的运动速度
double decide_x;
double decide_y;
//运动方向
int direction_x;
int direction_y;
public enemyBullet(int x, int y, int target_x, int target_y) {
this.x=x;
this.y=y;
this.height=4;
this.width=8;
this.speed=2;
this.target_x=target_x;
this.target_y=target_y;
icon = new ImageIcon("images/bullet1.png");
//计算发射点到目标点的xy方向上的长度比,勾股定理
double temp = Math.sqrt(Math.pow(Math.abs(x-target_x),2)+Math.pow(Math.abs(y-target_y),2));
decide_x= (Math.abs(x-target_x)/temp)*2;
decide_y= (Math.abs(y-target_y)/temp)*2;
//判断运动方向direction_x为1向右移为0左移direction_y同理
if(target_x>x)
{
direction_x=1;
}
if(target_x<x)
{
direction_x=0;
}
if(target_y>y)
{
direction_y=1;
}
if(target_y<y)
{
direction_y=0;
}
}
@Override
public void drawImage(Graphics g) {
g.drawImage(icon.getImage(), (int)this.x,(int)this.y,this.height,this.width,null);
}
@Override
public void move() {
switch (direction_x)
{
case 1:
{
this.x+=decide_x;
break;
}
case 0:
{
this.x-=decide_x;
break;
}
}
switch (direction_y)
{
case 1:
{
this.y+=decide_y;
break;
}
case 0:
{
this.y-=decide_y;
break;
}
}
}
}

@ -0,0 +1,68 @@
import javax.swing.*;
import java.awt.*;
public abstract class fly {
//x,y坐标
public int x;
public int y;
//图片的长度和宽度
public int height;
public int width;
//运动的速度
public double speed;
//父窗口的宽度和高度
public int pHeight;
public int pWidth;
//图片
public ImageIcon icon;
//以下为获取和设置变量
public void setSpeed(int speed)
{
this.speed=speed;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setHeight(int height) {
this.height = height;
}
public void setWidth(int width) {
this.width = width;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
//画图
public abstract void drawImage(Graphics g);
//移动
public abstract void move();
}

@ -0,0 +1,104 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口类
public class gameFrame_01 extends JFrame implements ActionListener {
// 面板,用于操作其中变量,函数
gamePanel panel=null;
//设置窗口类
gameFrame_02 g_02;
public gameFrame_01() {
setSize(320,480);
// 不可改变窗口大小
setResizable(false);
// 关闭窗口能关闭程序
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 设置布局,边界布局
setLayout(new BorderLayout());
// 中心显示
setLocationRelativeTo(null);
// 初始化菜单
initMenu();
}
public void addPanel(gamePanel panel)
{
// 添加面板
this.panel=panel;
this.add(panel);
}
// 初始化菜单
// 两个菜单,游戏和帮助,帮助有重新开始,复活,设置三个选项,帮助有介绍选项,并且设置每个选项的命令字符串
void initMenu()
{
JMenuBar jMenuBar = new JMenuBar();
JMenu jMenu = new JMenu("游戏");
JMenuItem jMenuItem = new JMenuItem("重新开始");
jMenuItem.addActionListener(this);
jMenuItem.setActionCommand("restart");
JMenuItem jMenuItem1 = new JMenuItem("设置");
jMenuItem1.addActionListener(this);
jMenuItem1.setActionCommand("set");
JMenuItem jMenuItem2 = new JMenuItem("复活");
jMenuItem2.addActionListener(this);
jMenuItem2.setActionCommand("relive");
jMenu.add(jMenuItem);
jMenu.add(jMenuItem1);
jMenu.add(jMenuItem2);
jMenuBar.add(jMenu);
JMenu jMenu1 = new JMenu("帮助");
JMenuItem jMenuItem3 = new JMenuItem("简介");
jMenuItem3.addActionListener(this);
jMenuItem3.setActionCommand("introduction");
jMenu1.add(jMenuItem3);
jMenuBar.add(jMenu1);
setJMenuBar(jMenuBar);
}
// 添加监听事件,点击对应选项,进行相应操作
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
//重新开始
if(command.equals("restart"))
{
try {
this.panel.restart();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
//复活
if (command.equals("relive"))
{
try {
panel.relive();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
// 弹出设置窗口
if(command.equals("set"))
{
g_02=new gameFrame_02(panel);
g_02.setVisible(true);
}
// 提示窗口,介绍游戏相关规则
if(command.equals("introduction"))
{
JOptionPane.showMessageDialog(this,"该游戏通过上下左右键控制飞机移动并带有计分功能敌机1一分敌机2两分击中boss三分,"+
'\n'+"当达到50分时子弹为双子弹为100时为三子弹s暂停或重新回到游戏,"+'\n'+"可以通过设置菜单指定难度以及相关参数设计");
}
}
}

@ -0,0 +1,157 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//设置窗口,
public class gameFrame_02 extends JFrame {
// 六个面板
JPanel jp1,jp2,jp3,jp4,jp5,jp6;
// 五个标签控件
JLabel jb1,jb2,jb3,jb4,jb5;
// 游戏面板
gamePanel panel;
//按钮
JButton jtn;
//敌机一数量下拉框
JComboBox<Integer> e_01=new JComboBox<Integer>();
// 敌机二数量下拉框
JComboBox<Integer> e_02=new JComboBox<Integer>();
// 我方飞机生命值的下拉框
JComboBox<Integer> myLife=new JComboBox<Integer>();
// boss生命值的下拉框
JComboBox<Integer> bossLife=new JComboBox<Integer>();
// 子弹状态的下拉框
JComboBox<Integer> bulletState=new JComboBox<Integer>();
public gameFrame_02(gamePanel panel) throws HeadlessException {
this.panel=panel;
setTitle("设置界面");
setSize(250,400);
setResizable(false);
setLayout(new BorderLayout());
setLocationRelativeTo(null);
init();
// 将下拉框选项设为游戏目前相应的值
myLife.setSelectedItem((Integer)panel.myplane.firstLife);
if (panel.boss_1!=null)
bossLife.setSelectedItem((Integer)panel.boss_1.firstLife);
e_01.setSelectedItem((Integer)panel.e_01Nums);
e_02.setSelectedItem((Integer)panel.e_02Nums);
bulletState.setSelectedItem((Integer)panel.bulletState);
// 退出窗口不会结束程序而是隐藏窗口
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
// 编写按钮点击监听事件
jtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
// 将游戏相应值设置为选择的值
panel.bulletState = (int) bulletState.getSelectedItem();
panel.myplane.life = (int) myLife.getSelectedItem();
panel.myplane.firstLife=panel.myplane.life;
if (panel.boss_1!=null) {
panel.boss_1.life = (int) bossLife.getSelectedItem();
panel.boss_1.firstLife=panel.boss_1.life;
}
panel.e_01Nums = (int) e_01.getSelectedItem();
panel.e_02Nums = (int) e_02.getSelectedItem();
}finally {
// 设置窗口不可见
setVisible(false);
}
}
});
}
void init()
{
this.setLayout(new GridLayout(6,1));
jtn =new JButton("确认");
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jp4 = new JPanel();
jp5 = new JPanel();
jp6=new JPanel();
jb1=new JLabel("敌机一数量:");;
jb2=new JLabel("敌机二数量:");
jb3=new JLabel("boss生命值");
jb4=new JLabel("我方生命值");
jb5=new JLabel("子弹状态:");
e_01.addItem(5);
e_01.addItem(10);
e_01.addItem(15);
e_02.addItem(1);
e_02.addItem(2);
e_02.addItem(3);
bossLife.addItem(25);
bossLife.addItem(50);
bossLife.addItem(125);
bossLife.addItem(250);
myLife.addItem(5);
myLife.addItem(10);
myLife.addItem(20);
myLife.addItem(40);
bulletState.addItem(1);
bulletState.addItem(2);
bulletState.addItem(3);
jp1.add(jb1);
jp1.add(e_01);
jp2.add(jb2);
jp2.add(e_02);
jp3.add(jb3);
jp3.add(bossLife);
jp4.add(jb4);
jp4.add(myLife);
jp5.add(jb5);
jp5.add(bulletState);
jp6.add(jtn);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(jp4);
this.add(jp5);
this.add(jp6);
}
}

@ -0,0 +1,798 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
public class gamePanel extends JPanel implements KeyListener {
// 判断游戏是否失败
boolean state=true;
// 判断是否成功击败boss
boolean success=false;
// 判断是否处于暂停状态
boolean stop=true;
// 子弹状态
int bulletState=1;
// 记录分数
int nums=0;
// 线程
public Thread thread;
// 计算boss出现时间
int time=0;
// boss类
boss boss_1;
// 敌机一的数量
public int e_01Nums=5;
// 敌机二的数量
public int e_02Nums=1;
// 我方飞机的长方形区域
Rectangle eRec;
// 炸弹类集合
ArrayList<bomb> bombs=new ArrayList<>();
// boss所发射的普通子弹集合
ArrayList<enemyBullet> bossBullets =new ArrayList<>();
// 敌机二所发射的子弹集合
ArrayList<enemyBullet> enemyBullets = new ArrayList<>();
// 我方发射子弹集合一
ArrayList<bullet> bullets_01 =new ArrayList<>();
// 我方发射子弹集合二
ArrayList<bullet> bullets_02=new ArrayList<>();
// 我方发射子弹集合三
ArrayList<bullet> bullets_03=new ArrayList<bullet>();
// 敌机一集合
ArrayList<enemy01> enemy01s=new ArrayList<>();
// 敌机二集合
ArrayList<enemy02> enemy02s =new ArrayList<>();
// 爆炸集合
ArrayList<crash> crashes=new ArrayList<>();
// 背景图片
ImageIcon bg = new ImageIcon("images/bg.png");
// 父窗口
JFrame jFrame;
// 我方飞机
myPlane myplane;
// 判断我方子弹是否击中飞机
public boolean isHit1(fly b, fly p)
{
Rectangle rectangle = new Rectangle(p.x,p.y,p.height,p.width);
Rectangle rectangle1 = new Rectangle((int)b.x,(int)b.y,(int)b.height,(int)b.width);
return rectangle.contains(rectangle1);
}
//判断炸弹是否击中我方飞机
public boolean isHit2(bomb b)
{
Rectangle rectangle = new Rectangle(myplane.x,myplane.y,myplane.height,myplane.width);
return rectangle.contains(b.x,b.y,b.height,b.width);
}
// 判断敌方子弹是否击中我方飞机
public boolean isHit3(enemyBullet b)
{
Rectangle rectangle = new Rectangle(myplane.x,myplane.y,myplane.height,myplane.width);
return rectangle.contains(b.x,b.y,b.height,b.width);
}
// 判断我方飞机是否与敌方碰撞
public boolean isHit4(fly p){
eRec = new Rectangle(p.getX(),p.getY(),p.getHeight(),p.getWidth());
if (eRec.contains(myplane.x,myplane.y)|| eRec.contains(myplane.x+myplane.width,myplane.y)||
eRec.contains(myplane.x,myplane.y+myplane.height)|| eRec.contains(myplane.x+myplane.width,myplane.y+myplane.height))
{
return true;
}
return false;
}
// 初始化游戏面板
public gamePanel(JFrame jFrame) throws InterruptedException {
this.jFrame=jFrame;
// 创建敌机一对象集合
for (int i = 0; i < 5; i++) {
enemy01 enemy1 = new enemy01(jFrame.getHeight(),jFrame.getWidth());
enemy01s.add(enemy1);
}
// 创建敌机二对象集合
for (int i = 0; i < 1; i++) {
enemy02 enemy2 = new enemy02(jFrame.getHeight(),jFrame.getWidth());
enemy02s.add(enemy2);
}
// 创建飞机对象
myplane = new myPlane(jFrame.getHeight(),jFrame.getWidth());
}
//重画函数
@Override
public void paint(Graphics g) {
super.paint(g);
// 若游戏未成功
if (!success) {
// 避免我方飞机移动至窗口外
if (myplane.y < 0) {
myplane.y = 0;
}
if (myplane.y > myplane.pHeight - myplane.height) {
myplane.y = myplane.pHeight - myplane.height;
}
if (myplane.x < 0) {
myplane.x = 0;
}
if (myplane.x > myplane.pWidth - myplane.width) {
myplane.x = myplane.pWidth - myplane.width;
}
// 画背景
g.drawImage(bg.getImage(),0,0,320,480,this);
// 分三种状态判断是否画相应子弹集合
for (int i = 0; i < bullets_01.size(); i++) {
bullets_01.get(i).drawImage(g);
}
if (bulletState==2||bulletState==3)
{
for (int i = 0; i < bullets_02.size(); i++) {
bullets_02.get(i).drawImage(g);
}
}
if (bulletState==3)
{
for (int i = 0; i < bullets_03.size(); i++) {
bullets_03.get(i).drawImage(g);
}
}
// 画敌机一
for (int i = 0; i < enemy01s.size(); i++) {
enemy01s.get(i).drawImage(g);
}
// 画敌机二
for (int i = 0; i < enemy02s.size(); i++) {
enemy02s.get(i).drawImage(g);
}
// 画爆炸
for (int i = 0; i < crashes.size(); i++) {
crashes.get(i).drawImage(g);
}
// 画敌方二子弹
for (int i = 0; i < enemyBullets.size(); i++) {
enemyBullets.get(i).drawImage(g);
}
// 画炸弹
for (int i = 0; i < bombs.size(); i++) {
bombs.get(i).drawImage(g);
}
// 画boss
if (boss_1!=null) {
boss_1.drawImage(g);
}
// 画boss的子弹
for (int i = 0; i < bossBullets.size(); i++) {
bossBullets.get(i).drawImage(g);
}
if (stop)
g.drawString("按s键开始", 130, jFrame.getHeight() / 2 - 50);
// 如果游戏失败,则显示字符串提示,不画飞机,否则画我方飞机
if (!state)
g.drawString("游戏结束,请点击菜单栏重新开始", jFrame.getWidth() / 6, jFrame.getHeight() / 2 - 50);
else
myplane.drawImage(g);
// 画分数
String str="分数:"+nums;
g.setFont(new Font("宋体",Font.BOLD,10));
// 画boss生命值和我方生命值使用矩形画
g.drawString(str,50,415);
g.drawRect(0,407,40,7);
if (boss_1!=null) {
g.drawRect(0,0,250,4);
g.setColor(Color.red);
g.fillRect(0, 0, (250 / boss_1.firstLife) * boss_1.life, 4);
}
g.setColor(Color.red);
g.fillRect(0,407,(40/myplane.firstLife)*myplane.life,7);
}
else
{
// 游戏成功则画提示字符串
g.drawString("恭喜!游戏成功,请点击菜单栏重新开始", jFrame.getWidth() / 6, jFrame.getHeight() / 2 - 50);
}
}
// 计算子弹发射间隔
int count =0;
public void init() throws InterruptedException {
//创建播放音乐的线程
/* playMusic playmusic = new playMusic("music/river flows in you.wav");
playmusic.start();*/
//创建线程对象
thread=new Thread(new Runnable() {
@Override
public void run() {
// 未失败并且未成功则持续执行以下函数
while (state&&!success) {
// 将敌机一的数量添加或减少至目标数量
if (e_01Nums>enemy01s.size())
{
for (int i = 0; i < e_01Nums-enemy01s.size(); i++) {
try {
enemy01s.add(new enemy01(jFrame.getHeight(),jFrame.getWidth()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
else {
for (int i = 0; i < enemy01s.size()-e_01Nums; i++) {
enemy01s.remove(enemy01s.size()-i-1);
}
}
// 将敌机一的数量添加或减少至目标数量
if (e_02Nums>enemy02s.size())
{
for (int i = 0; i < e_02Nums-enemy02s.size(); i++) {
try {
enemy02s.add(new enemy02(jFrame.getHeight(),jFrame.getWidth()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
else {
for (int i = 0; i < enemy02s.size() - e_02Nums; i++) {
enemy02s.remove(enemy02s.size() - i - 1);
}
}
// 如果通过设置将子弹状态设为3避免又因为分数将状态改为2
if (bulletState!=3) {
// 由于分数增加为123所以可能直接跳过50所以设置为大于50小于100
if (nums > 50&&nums<100) {
bulletState = 2;
}
}
// 如果分数大于一百则将子弹状态改为3,并且状态不为1
if (nums>100&&bulletState!=1)
{
bulletState=3;
}
// 如果是暂停状态则跳出循环
if (stop)
{
break;
}
// 如果boss对象未创建则时间加一
if (boss_1 == null) {
time++;
}
// 记录时间time到500时将创建boss对象
if (time == 500) {
time = 0;
boss_1 = new boss(jFrame.getHeight(), jFrame.getWidth());
}
// boss不为空则boss移动
if (boss_1 != null) {
boss_1.move();
}
// 对所有敌机一移动,判断是否被击中,或超过窗口大小
for (int j = 0; j < enemy01s.size(); j++) {
enemy01 tempEnemy = enemy01s.get(j);
tempEnemy.move();
// 判断是否超过窗口大小
if (tempEnemy.y > tempEnemy.pHeight) {
enemy01s.remove(tempEnemy);
try {
enemy01s.add(new enemy01(jFrame.getHeight(), jFrame.getWidth()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 判断是否被击中击中后销毁相应敌机分数加1添加爆炸创建新的敌机1
for (int m = 0; m < bullets_01.size(); m++) {
bullet bullet1 = bullets_01.get(m);
if (isHit1(bullet1, tempEnemy)) {
enemy01s.remove(tempEnemy);
nums++;
bullets_01.remove(bullet1);
try {
enemy01s.add(new enemy01(jFrame.getHeight(), jFrame.getWidth()));
} catch (InterruptedException e) {
e.printStackTrace();
}
crashes.add(new crash(tempEnemy.x, tempEnemy.y, tempEnemy.height, tempEnemy.width));
}
}
// 如果子弹状态为23还需要判断子弹集合23
if (bulletState==2||bulletState==3)
{
for (int n = 0; n < bullets_02.size(); n++) {
bullet bullet1 = bullets_02.get(n);
if (isHit1(bullet1, tempEnemy)) {
enemy01s.remove(tempEnemy);
nums++;
bullets_02.remove(bullet1);
try {
enemy01s.add(new enemy01(jFrame.getHeight(), jFrame.getWidth()));
} catch (InterruptedException e) {
e.printStackTrace();
}
crashes.add(new crash(tempEnemy.x, tempEnemy.y, tempEnemy.height, tempEnemy.width));
}
}
}
if (bulletState==3)
{
for (int n = 0; n < bullets_03.size(); n++) {
bullet bullet1 = bullets_03.get(n);
if (isHit1(bullet1, tempEnemy)) {
enemy01s.remove(tempEnemy);
bullets_03.remove(bullet1);
nums++;
try {
enemy01s.add(new enemy01(jFrame.getHeight(), jFrame.getWidth()));
} catch (InterruptedException e) {
e.printStackTrace();
}
crashes.add(new crash(tempEnemy.x, tempEnemy.y, tempEnemy.height, tempEnemy.width));
}
}
}
}
// 判断boss是否被子弹击中击中则生命值降低分数增加3
if (boss_1 != null) {
for (int t = 0; t < bullets_01.size(); t++) {
bullet b = bullets_01.get(t);
if (isHit1(b, boss_1)) {
bullets_01.remove(b);
boss_1.life--;
nums += 3;
}
}
for (int t = 0; t < bullets_02.size(); t++) {
bullet b = bullets_02.get(t);
if (isHit1(b, boss_1)) {
bullets_02.remove(b);
boss_1.life--;
nums += 3;
}
}
for (int t = 0; t < bullets_03.size(); t++) {
bullet b = bullets_03.get(t);
if (isHit1(b, boss_1)) {
bullets_03.remove(b);
boss_1.life--;
nums += 3;
}
}
}
// 判断是否被击中击中后销毁相应敌机分数加1添加爆炸创建新的敌机2
for (int i = 0; i < enemy02s.size(); i++) {
enemy02 tempenemy02 = enemy02s.get(i);
tempenemy02.move();
if (tempenemy02.y > tempenemy02.pHeight) {
enemy02s.remove(tempenemy02);
try {
enemy02s.add(new enemy02((jFrame.getHeight()), jFrame.getWidth()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int n = 0; n < bullets_01.size(); n++) {
bullet bullet1 = bullets_01.get(n);
if (isHit1(bullet1, tempenemy02)) {
enemy02s.remove(tempenemy02);
nums+=2;
bullets_01.remove(bullet1);
try {
enemy02s.add(new enemy02(jFrame.getHeight(), jFrame.getWidth()));
} catch (InterruptedException e) {
e.printStackTrace();
}
crashes.add(new crash(tempenemy02.x, tempenemy02.y, tempenemy02.height, tempenemy02.width));
}
}
// 在子弹状态为23时还需判断子弹集合23
if (bulletState==2||bulletState==3)
{
for (int n = 0; n < bullets_02.size(); n++) {
bullet bullet1 = bullets_02.get(n);
if (isHit1(bullet1, tempenemy02)) {
enemy02s.remove(tempenemy02);
nums+=2;
bullets_02.remove(bullet1);
try {
enemy02s.add(new enemy02(jFrame.getHeight(), jFrame.getWidth()));
} catch (InterruptedException e) {
e.printStackTrace();
}
crashes.add(new crash(tempenemy02.x, tempenemy02.y, tempenemy02.height, tempenemy02.width));
}
}
}
if (bulletState==3)
{
for (int n = 0; n < bullets_03.size(); n++) {
bullet bullet1 = bullets_03.get(n);
if (isHit1(bullet1, tempenemy02)) {
enemy02s.remove(tempenemy02);
nums+=2;
bullets_03.remove(bullet1);
try {
enemy02s.add(new enemy02(jFrame.getHeight(), jFrame.getWidth()));
} catch (InterruptedException e) {
e.printStackTrace();
}
crashes.add(new crash(tempenemy02.x, tempenemy02.y, tempenemy02.height, tempenemy02.width));
}
}
}
}
// 记录子弹发射间隔的变量增加
count++;
// 如果count为201的倍数则添加炸弹并设为0避免count增加过大造成溢出
if (count % 201 == 0) {
count = 0;
if (boss_1 != null) {
bombs.add(new bomb(boss_1.x + boss_1.width / 2, boss_1.y + boss_1.height / 2, myplane.x+myplane.width, myplane.y+myplane.height));
}
}
// 如果count为60的倍数则添加为boss和敌机二添加子弹
if (count % 60 == 0) {
for (enemy02 enemy2 : enemy02s) {
if (enemy2.y>enemy2.getHeight()&&enemy2.y<jFrame.getHeight()-enemy2.getHeight())
enemyBullets.add(new enemyBullet(enemy2.x + enemy2.width / 2, enemy2.y + enemy2.height / 2, myplane.x+myplane.width/2, myplane.y+myplane.height/2));
}
if (boss_1!=null)
{
bossBullets.add(new enemyBullet(boss_1.x + boss_1.width / 2, boss_1.y + boss_1.height / 2, myplane.x+myplane.width/2, myplane.y+myplane.height/2));
}
}
// 为子弹集合1添加子弹子弹状态为23还需分别向相应集合添加子弹
if (bulletState==1) {
if (count % 15 == 0) {
bullets_01.add(new bullet(jFrame.getHeight(), jFrame.getWidth(), myplane.x + myplane.width / 2, myplane.y));
}
}
if (bulletState==2)
{
if (count%15==0)
{
bullets_01.add(new bullet(jFrame.getHeight(),jFrame.getWidth(),myplane.x+myplane.width/3,myplane.y));
bullets_02.add(new bullet(jFrame.getHeight(),jFrame.getWidth(),myplane.x+2*myplane.width/3,myplane.y));
}
}
if (bulletState==3)
{
if (count%15==0)
{
bullets_01.add(new bullet(jFrame.getHeight(),jFrame.getWidth(),myplane.x,myplane.y));
bullets_02.add(new bullet(jFrame.getHeight(),jFrame.getWidth(),myplane.x+myplane.width/2,myplane.y));
bullets_03.add(new bullet(jFrame.getHeight(),jFrame.getWidth(),myplane.x+myplane.width,myplane.y));
}
}
// 在不同子弹状态下移动相应子弹集合中的子弹,并判断是否超过窗口,超过则销毁
if (bulletState==2||bulletState==3)
{
bullets_02.stream().forEach(bullet2 -> {
bullet2.move();
if (bullet2.y > bullet2.pHeight) {
bullets_02.remove(bullet2);
}
});
}
if (bulletState==3)
{
bullets_03.stream().forEach(bullet2 -> {
bullet2.move();
if (bullet2.y > bullet2.pHeight) {
bullets_02.remove(bullet2);
}
});
}
// 移动子弹集合1并判断是否超过窗口超过则销毁
bullets_01.stream().forEach(bullet1 -> {
bullet1.move();
if (bullet1.y > bullet1.pHeight) {
bullets_01.remove(bullet1);
}
});
// 移动敌机二的子弹,并判断是否超过窗口,超过则销毁
for (int i = 0; i < enemyBullets.size(); i++) {
enemyBullet eb = enemyBullets.get(i);
eb.move();
if (eb.x > jFrame.getWidth() || eb.x - eb.width < 0 || eb.y > jFrame.getHeight() || eb.y + eb.height < 0) {
enemyBullets.remove(eb);
}
}
// 移动boss的子弹并判断是否超过窗口超过则销毁
if (boss_1!=null)
{
for (int i = 0; i < bossBullets.size(); i++) {
enemyBullet eb = bossBullets.get(i);
eb.move();
if (eb.x > jFrame.getWidth() || eb.x - eb.width < 0 || eb.y > jFrame.getHeight() || eb.y + eb.height < 0) {
enemyBullets.remove(eb);
}
}
}
// 移动炸弹,并判断是否超过窗口,超过则销毁
for (int i = 0; i < bombs.size(); i++) {
bomb bomb_01 = bombs.get(i);
bomb_01.move();
if (bomb_01.x > jFrame.getWidth() || bomb_01.x - bomb_01.width < 0 || bomb_01.y > jFrame.getHeight() || bomb_01.y + bomb_01.height < 0) {
bombs.remove(bomb_01);
}
}
// 如果爆炸存在超过相应时间,则爆炸炸消失
for (int i = 0; i < crashes.size(); i++) {
crash crash01 = crashes.get(i);
crash01.count++;
if (crash01.count == 5) {
crashes.remove(crash01);
}
}
// 判断炸弹是否击中我方飞机,击中则消除炸弹,设为失败,添加爆炸
for (int i = 0; i < bombs.size(); i++) {
if (isHit2(bombs.get(i))) {
state = false;
bombs.remove(i);
crashes.add(new crash(myplane.x,myplane.y,myplane.height,myplane.width));
}
}
// 判断boss生命值是否为0游戏是否成功
if (boss_1 != null) {
if (boss_1.life <= 0) {
success = true;
}
}
// 判断boss子弹是否击中我方飞机击中则我方飞机生命值减1消除子弹
for (int p = 0; p < bossBullets.size(); p++) {
enemyBullet e = bossBullets.get(p);
if (isHit3(e)) {
myplane.life--;
bossBullets.remove(e);
}
if (myplane.life == 0) {
state = false;
}
}
// 判断敌机2子弹是否击中我方飞机击中则我方飞机生命值减1消除子弹
for (int p = 0; p < enemyBullets.size(); p++) {
enemyBullet e = enemyBullets.get(p);
if (isHit3(e)) {
myplane.life--;
enemyBullets.remove(e);
}
if (myplane.life == 0) {
state = false;
}
}
// 判断我方飞机是否撞击到boss是则游戏失败
if (boss_1 != null) {
if (isHit4(boss_1)) {
state = false;
crashes.add(new crash(myplane.x, myplane.y, myplane.width, myplane.height));
}
}
// 判断我方飞机是否撞击到任何一架敌机一,是则游戏失败,添加爆炸
for (int i =0;i<enemy01s.size();i++) {
enemy01 e =enemy01s.get(i);
if (isHit4(e)){
state = false;
enemy01s.remove(e);
crashes.add(new crash(myplane.x, myplane.y, myplane.width, myplane.height));
crashes.add(new crash(e.x, e.y, e.width,e.height));
}
}
// 判断我方飞机是否撞击到任何一架敌机二,是则游戏失败,添加爆炸
for (int i =0;i<enemy02s.size();i++) {
enemy02 e =enemy02s.get(i);
if (isHit4(e)) {
state = false;
crashes.add(new crash(myplane.x, myplane.y, myplane.width, myplane.height));
crashes.add(new crash(e.x, e.y, e.width,e.height));
enemy02s.remove(e);
}
}
// 线程“睡”15毫秒
try {
Thread.sleep(15);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 重画
repaint();
}
/*if (playmusic!=null) {
playmusic.stop();
}*/
}
});
// 开启线程
thread.start();
repaint();
}
// 重新初始化
public void restart() throws InterruptedException {
// 所有敌机初始化
for (int i = 0; i < enemy01s.size(); i++) {
enemy01s.get(i).restart();
}
for (int j = 0; j < enemy02s.size(); j++) {
enemy02s.get(j).restart();
}
myplane.restart();
// 重新设置初始变量
boss_1 = null;
state=true;
time=0;
success=false;
bulletState=1;
nums=0;
// 消除所有子弹炸弹
for (int i = 0; i < bullets_01.size(); i++) {
bullets_01.remove(i);
}
for (int i = 0; i < bullets_03.size(); i++) {
bullets_03.remove(i);
}
for (int i = 0; i < bullets_02.size(); i++) {
bullets_02.remove(i);
}
for (int i = 0; i < enemyBullets.size(); i++) {
enemyBullets.remove(i);
}
for (int i = 0; i < bombs.size(); i++) {
bombs.remove(i);
}
repaint();
init();
}
// 复活,将生命值改为初始值
public void relive() throws InterruptedException {
myplane.life=5;
state=true;
success=false;
repaint();
init();
}
@Override
public void keyTyped(KeyEvent e) {
}
// 键盘监听事件
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode())
{
// 如果为s则将stop换为另一状态如果为并重新调用init函数
case KeyEvent.VK_S:
stop=!stop;
try {
init();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
// 按下向上键,并且未停止或失败,飞机移动
case KeyEvent.VK_UP:
if (!stop&&state) {
if (myplane.y > 0) {
myplane.y -= myplane.speed;
}
}
break;
// 按下向下键,并且未停止或失败,飞机移动
case KeyEvent.VK_DOWN:
if (!stop&&state) {
if (myplane.y < this.getHeight() - myplane.height) {
myplane.y += myplane.speed;
}
}
break;
// 按下向左键,并且未停止或失败,飞机移动
case KeyEvent.VK_LEFT:
if (!stop&&state) {
if (myplane.x > 0) {
myplane.x -= myplane.speed;
}
}
break;
// 按下向右键,并且未停止或失败,飞机移动
case KeyEvent.VK_RIGHT:
if (!stop&&state) {
if (myplane.x < this.getWidth() - myplane.width) {
myplane.x += myplane.speed;
}
}
break;
}
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
}

@ -0,0 +1,42 @@
import javax.swing.*;
import java.awt.*;
public class myPlane extends fly {
ImageIcon myplane = new ImageIcon("images/myPlane.png");
// 我方飞机生命值
public int life=10;
// 记录我方飞机初始生命值
public int firstLife =life;
// 重新初始化
public void restart()
{
this.x =140;
this.y=350;
life=10;
}
public myPlane(int pHeight, int pWidth) {
this.x =140;
this.y=350;
this.speed=15;
this.pWidth=pWidth;
this.pHeight=pHeight;
this.height=40;
this.width=40;
}
//画图
@Override
public void drawImage(Graphics g) {
g.drawImage(myplane.getImage(), this.x,this.y, height, height, null);
}
//
@Override
public void move() {
}
}
Loading…
Cancel
Save