飞机大战编程过程 java飞机大战小游戏

多条告白如次剧本只需引入一次
一:玩耍展现
铁鸟大战小玩耍咱们都玩过,经过挪动铁鸟来打敌机,这边给大师展现一卑劣戏功效:哇哇呜因为gif只能上传5M巨细,以是就不许给大师展现操纵了,即使大师有爱好不妨本人本人做出来再玩哟 。
这内里一切的铁鸟、枪弹、敌机、爆裂功效都不妨随便替代成本人想要的图片 。大师不妨大肆表现 。也不妨创造无敌版 。
比方我不妨把敌机替代成王八:
大师不妨随便表现哦 。
二:玩耍教程
1.View视图层
名目构造:
1.1创造玩耍面板类
package com.view;
import com.controller.PlaneController;
import com.eneity.EnemyPlane;
import com.eneity.FightPlane;
import com.utils.P;
import javax.swing.*;
/**
* 窗口类
*/
public class FrameWindow {
/**
* 结构本领
*/
public FrameWindow() {
launch();
}
/**
* 创造窗口界面
*/
public void launch() {
FightPlane fightPlane = new FightPlane();
PlaneController controller = new PlaneController(fightPlane);//铁鸟遏制类
GamePanel gamePanel = new GamePanel(fightPlane);
JFrame jFrame = new JFrame();
jFrame.add(gamePanel);
jFrame.setTitle(“铁鸟大战”);
jFrame.addKeyListener(controller);//给窗口树立监听事变
jFrame.setSize(P.WEIGHT, P.HEIGHT);//树立窗口巨细
jFrame.setVisible(true);//表露在窗口
jFrame.setResizable(false);//使窗口巨细恒定
jFrame.setLocationRelativeTo(null);//初始化的界面表露的场所,null为实质自符合
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击封闭之后后盾也会封闭
}
}
1.2.创造玩耍实质表露类
package com.view;
import com.eneity.CreateEnemy;
import com.eneity.EnemyPlane;
import com.eneity.FightPlane;
import com.utils.P;
import com.utils.PUtils;
import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
/**
* 玩耍面板
*/
public class GamePanel extends JPanel {
public static long count = 0;//祛除敌机数目
public static List<EnemyPlane> EnemyPlaneArrayList = new LinkedList<EnemyPlane>();
private FightPlane fightPlane;//战役机
public ImageIcon bz2 = new
ImageIcon(PUtils.getImage(“images/dd2.png”));
//结构本领,启用这个线程
public GamePanel(FightPlane fightPlane) {
this.fightPlane = fightPlane;
new updateGame().start();//连接重画出画布
new CreateEnemy().start();//画出10架敌机
}
boolean flage = true;
@Override
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, P.WEIGHT, P.HEIGHT);
g.setColor(Color.red);
g.drawString(“杀龟数目:” + count, 650, 50);
//画出战役机
if (fightPlane != null) {
fightPlane.drawMe(g);
}
//画出敌机
for (int i = 0; i < EnemyPlaneArrayList.size(); i++) {
if (EnemyPlaneArrayList.get(i).flat == 1) {
int x = EnemyPlaneArrayList.get(i).x;
int y = EnemyPlaneArrayList.get(i).y;
g.drawImage(bz2.getImage(), x, y, 150, 150, null);
EnemyPlaneArrayList.get(i).flat–;
GamePanel.count++;
System.out.println(count);
if (EnemyPlaneArrayList.get(i).flat == -3) {
int c = new Random().nextInt(650) + 50;
EnemyPlaneArrayList.get(i).x = c;
EnemyPlaneArrayList.get(i).y = -150;
}
} else {
EnemyPlaneArrayList.get(i).drawMe(g);
}
}
}
//里面类,创造一个线程不停的革新界面
class updateGame extends Thread {
@Override
public void run() {
while (flage) {
repaint();
}
}
}
}
2.enetiy实业层
2.1玩耍实业笼统类
package com.eneity;
import java.awt.*;
/**
* 设置玩耍中一切东西的父类
*/
public abstract class GameObject {
public int x,y,width,height;//设置铁鸟的坐标,长,宽
public abstract void drawMe(Graphics g);//铁鸟画本人的本领
public abstract Rectangle getRect();//获得一个矩形用来检验和测定有没有和铁鸟相撞
}
2.2战机类
package com.eneity;
import com.utils.P;
import com.utils.PUtils;
import com.view.GamePanel;
import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;
import java.util.List;
/**
* 战役机
*/
public class FightPlane extends GameObject {
public List<Bullet> arrayList =new LinkedList<Bullet>();
public static Image image =PUtils.getImage(“images/zdj.png”);//铁鸟图像资源

推荐阅读