|
|
|
@ -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) {
|
|
|
|
|
// 由于分数增加为1,2,3所以可能直接跳过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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果子弹状态为2,3还需要判断子弹集合2,3
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 在子弹状态为2,3时还需判断子弹集合2,3
|
|
|
|
|
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添加子弹,子弹状态为2,3还需分别向相应集合添加子弹
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|