一:游戏展示
飞机大战小游戏我们都玩过,通过移动飞机来打敌机,这里给大家展示一下游戏成果:
呜呜呜由于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");//飞机图像资源 public static Image zd=PUtils.getImage("images/zd.png");//子弹图片资源 public ImageIcon bz2=new ImageIcon(PUtils.getImage("images/dd2.png")); public FightPlane(){ x=300; y=300; width=120; height=120; new CreateBullet().start();//创建画出子弹的线程 } @Override public void drawMe(Graphics g) { g.drawImage(image,x,y,width,height,null); //画出子弹; for(int i=0;i<arrayList.size();i++){ //检测子弹是否与敌机相撞 for(int j=0;j<GamePanel.EnemyPlaneArrayList.size();j++){ if(GamePanel.EnemyPlaneArrayList.get(j).getRect().intersects(arrayList.get(i).getRect())){ /* int x=GamePanel.EnemyPlaneArrayList.get(j).x; int y=GamePanel.EnemyPlaneArrayList.get(j).y; GamePanel.EnemyPlaneArrayList.remove(j); g.drawImage(bz2.getImage(),500,500,150,150,null);*/ GamePanel.EnemyPlaneArrayList.get(j).flat=1; } } if(arrayList.get(i).y<20){ arrayList.remove(i);//优化子弹类,当子弹达到一定位置时让其消失 } arrayList.get(i).drawMe(g); } } @Override public Rectangle getRect() { return new Rectangle(x,y,width,height); } //创建子弹内部类 class Bullet extends GameObject{ public Bullet(){ width=60; height=120; } @Override public void drawMe(Graphics g) {//在面板上画出子弹 g.drawImage(zd,x,y,width,height,null); y-=3; } @Override public Rectangle getRect() { return new Rectangle(x,y,width,height); } } /** * 创建一个不断画出子弹的线程 */ class CreateBullet extends Thread{ @Override public void run() { while(true){ try { Bullet bullet=new Bullet(); bullet.x=x+32; bullet.y=y+20; arrayList.add(bullet); Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
2.3敌机类
package com.eneity; import com.utils.P; import com.utils.PUtils; import java.awt.*; import java.util.LinkedList; import java.util.List; /** * 敌机类 */ public class EnemyPlane extends GameObject { public int flat=0;//检测子弹是否与敌机相撞 public static Image dj =PUtils.getImage("images/wugui.png");//飞机图像资源 /* public static Image zd=PUtils.getImage("images/zd.png");//子弹图片资源*/ public EnemyPlane(){ x=300; y=300; width=120; height=120; // new CreateBullet().start();//创建画出子弹的线程 } @Override public void drawMe(Graphics g) { g.drawImage(dj,x,y,width,height,null); y+=3;//每次画完更新y坐标 if(y> P.HEIGHT){//节约资源重复利用每架飞机 y=-150; } /* //画出子弹 for(int i=0;i<arrayList.size();i++){ arrayList.get(i).drawMe(g); }*/ } @Override public Rectangle getRect() { return new Rectangle(x,y,width,height); } /* //创建子弹内部类 class Bullet extends GameObject{ public Bullet(){ width=60; height=120; } @Override public void drawMe(Graphics g) {//在面板上画出子弹 g.drawImage(zd,x,y,width,height,null); y-=3; } @Override public Rectangle getRect() { return null; } } *//** * 创建一个不断画出子弹的线程 *//* class CreateBullet extends Thread{ @Override public void run() { while(true){ try { Bullet bullet=new Bullet(); bullet.x=x+32; bullet.y=y+20; arrayList.add(bullet); Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } }*/ }
2.4战机不断出现类
package com.eneity; import com.view.GamePanel; import java.util.Random; /** * 源源不断的创建敌机 */ public class CreateEnemy extends Thread{ Random random =new Random();//创建随机数保证每次出现的敌机x坐标随机出现 @Override public void run() { while(true){ try { EnemyPlane e= new EnemyPlane(); e.x=random.nextInt(650)+50; e.y=-150; GamePanel.EnemyPlaneArrayList.add(e); Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } } } }
3.controller控制飞机移动层
3.1PlaneController类
package com.controller; import com.eneity.FightPlane; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; /** * 控制飞机运动 */ public class PlaneController extends KeyAdapter { FightPlane fightPlane; public PlaneController(FightPlane fightPlane){ this.fightPlane=fightPlane; } @Override public void keyPressed(KeyEvent e) { diretion(e.getKeyCode()); } //控制飞机移动 public void diretion(int code){ switch(code){ case 37://对应键盘左键 fightPlane.x-=15; break; case 38://对应键盘上键 fightPlane.y-=15; break; case 39://对应键盘右键 fightPlane.x+=15; break; case 40://对应键盘下键 fightPlane.y+=5; break; } } }
4.utils工具层
4.1飞机常量类
package com.utils; /** * 用来定义使用到的常量类 */ public class P { public static int HEIGHT=800,WEIGHT=800; }
4.2加载图片类
package com.utils; import javax.swing.*; import java.awt.*; import java.util.Objects; /** * 加载图片工具类 */ public class PUtils { public static Image getImage(String url){ ImageIcon icon=new ImageIcon(PUtils.class.getClassLoader().getResource(url)); return icon.getImage(); } }
5.run启动层
5.1游戏启动类
package com.run; import com.view.FrameWindow; /** * 启动类 */ public class Main { public static void main(String[] args) { new FrameWindow(); } }
三:游戏源码
源码传送门:https://github.com/xuyuanzhi051/personalprojects/tree/master/03PlaneDemo
最后最后如果您觉得这篇文章对您有帮助,给博主个小小的关注如何,大家一起学习进步,一起交流,嘤嘤嘤。