观看了尚学堂讲解飞机大战的视频,再加之自己的一些想法,添加了一些元素,如:做一个开始界面,加入几种道具,添加背景音乐,改变Boss的攻击方式等等;以此整理出一篇博客,给各位看官老爷配上思维导图。
目录
主函数Gamewin
绘制副窗口-开始界面,主窗口-游戏界面,背景,飞机,子弹,道具等等;
游戏的开始,暂停,胜利,失败,再来一次.
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class GameWin extends JFrame{
//一种道具增加的伤害,没吃到道具之前先设置为0
static int hit=0;
//控制子弹生成速度
int actionTime;
double figure=Math.random();
double help=Math.random();
//得分
static int score=0;
//玩家子弹
static int MyBullet=0;
//玩家血量
static int planeblood=3;
//血量耗尽之后有一次 复活机会
static int change=1;
int n;
//游戏重绘次数
int count=1;
//敌机子弹集合
List<enemyBullet> enemyBulletList=new ArrayList<>();
//Boss子弹集合
List<BossBullet> bossBulletList=new ArrayList<>();
//Boss半血子弹集合
List<BossBullet1> bossBullet1List=new ArrayList<>();
List<BossBullet2> bossBullet2List=new ArrayList<>();
List<BossBullet3> bossBullet3List=new ArrayList<>();
//敌方boss
Boss boss=new Boss(this);
//道具1
Tools tools=new Tools(this);
//道具2
Tools1 tools1=new Tools1(this);
//道具3
Tools2 tools2=new Tools2(this);
//道具4
Tools3 tools3=new Tools3(this);
//道具5
Tools4 tools4=new Tools4(this);
//道具6
Tools5 tools5=new Tools5(this);
//敌机集合
List<EnemyPlane> enemyPlaneList=new ArrayList<>();
//我方子弹集合
List<Bullet> bulletList=new ArrayList<>();
List<Bullet2> bulletList2=new ArrayList<>();
//游戏状态 0:开始界面,1:游戏中,2:游戏暂停,3:通关失败,4:通关成功
static int state=1;
//创建背景对象
Bg bg=new Bg();
//我方飞机
GamePlane myplane=new GamePlane(this);
//Bullet bullet=new Bullet(this,myplane.getX(),myplane.getY());
//画布,用于下面双缓存技术解决闪烁问题
Image offScreenImage;
//判断该进入哪个窗口
public static int flag = 0;
//窗口1
public GameWin(){
JFrame jFrame=new JFrame();
jFrame.setSize(472,300);
jFrame.setTitle("单机版飞机大战");
jFrame.setLocationRelativeTo(null);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setLayout(null);
JMenuBar jMenuBar=new JMenuBar();
JMenu jMenu=new JMenu("简介");
jMenuBar.add(jMenu);
jFrame.setJMenuBar(jMenuBar);
//添加鼠标监听
jMenu.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if (e.getButton() == 1) {
JOptionPane.showMessageDialog(jFrame, "请用鼠标操控着你的小灰机,击败Boss,在这里,运气和实力一样重要!");
}
}
});
//背景图片
ImageIcon icon=new ImageIcon("C:\\Users\\21299\\Pictures\\Saved Pictures\\72-1F12316295HO.jpg");
JLabel jLabel=new JLabel(icon);
jLabel.setBounds(0,0,472,287);
jFrame.add(jLabel);
//开始游戏按钮
JButton jtb=new JButton("开始游戏");
jtb.setBounds(160,170,152,52);
jtb.setIcon(new ImageIcon("C:\\Users\\21299\\Pictures\\Saved Pictures\\26C975B3D8236FDA9A4C3DAE40CCA2F4.jpg"));
jtb.setFont(new Font("楷体",Font.BOLD,20));
//给开始游戏添加鼠标监听
jtb.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if (e.getButton() == 1) {
//关闭窗口
jFrame.dispose();
flag=1;
}
}
});
jFrame.add(jtb);
jFrame.setVisible(true);
}
//窗口2
public void initJframe(){
this.setSize(512,768);
this.setLocationRelativeTo(null);
this.setTitle("单机版飞机大战(运气or霉气)");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//给窗口2添加监听
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
switch(state){
//重新开始页面
case 0:
restart();
if(e.getButton()==1){
Music.playBackground();
state=1;
}
break;
case 1://游戏进行中
if(e.getButton()==3){
state=2;
}
break;
case 2://暂停
if(e.getButton()==1){
state=1;
}
break;
case 3://死亡
if(change>0){
java.util.Timer invincibleTimer = new Timer();
if(e.getButton()==1){
//两秒的无敌时间
invincibleTimer.schedule(new TimerTask() {
@Override
public void run() {
GamePlane.state = false; // 重置无敌状态
}
}, 2000);
state=1;
change--;
planeblood=0;
}
}
if(e.getButton()==3){
Music.bg.stop();
state=0;
}
break;
case 4://通关
if(e.getButton()==1){
Music.bg.stop();
state=0;
}
break;
}
}
});
//每10毫秒绘制一次
while(true){
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
//绘制
public void paint(Graphics g){
offScreenImage=this.createImage(516,800);//画布
Graphics gImage=offScreenImage.getGraphics();
//绘制背景
bg.paint(gImage);
if(state!=2&&state!=3){
if(GameWin.MyBullet==0){
if(count%15==0){
//生成我方子弹
bulletList.add(new Bullet(this,myplane.getX()+12,myplane.getY()));
}
} else if (GameWin.MyBullet==1) {
if(count%15==0){
//生成我方子弹
bulletList.add(new Bullet(this,myplane.getX()+12,myplane.getY()));
bulletList2.add(new Bullet2(myplane.getX()+12,myplane.getY()));
}
} else if(GameWin.MyBullet==2){
if(count%50==0){
bulletList.add(new Bullet(this,myplane.getX()+12,myplane.getY()));
}
}else if(GameWin.MyBullet==3){
if(count%8==0){
bulletList.add(new Bullet(this,myplane.getX()+12,myplane.getY()));
}
}
//生成敌方战斗机
if(count%30==0){
n=(int)(Math.random()*11)*51;
enemyPlaneList.add(new EnemyPlane(this,n+5,0));
//生成敌方子弹
for(EnemyPlane enemyPlane:enemyPlaneList){
if(count%40==0){
enemyBulletList.add(new enemyBullet(this,enemyPlane.x+15,enemyPlane.y+50));
}
}
}
}
if(state==1){//
//第一批道具的出现
if(score>=15&&figure>=0.6&&figure<0.9){
tools1.paint(gImage);
}
if(score>=15&&figure<0.6){
tools1.paint(gImage);
}
if(score>=15&&figure>=0.9){
tools1.paint(gImage);
}
//boss的出现
if(score>=25){
boss.paint(gImage);
if(count%20==0){
//boss第一形态
bossBulletList.add(new BossBullet(this,boss.getX()+85,boss.getY()+100));
}
if(Boss.blood<90&&Boss.blood>30){
//第二形态
if(count%20==0){
bossBullet1List.add(new BossBullet1(this,boss.getX()+85,boss.getY()+100))