在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Java语言基础 → java 飞机大战 小游戏源码

java 飞机大战 小游戏源码

Java语言基础

下载此实例
  • 开发语言:Java
  • 实例大小:0.11M
  • 下载次数:49
  • 浏览次数:1228
  • 发布时间:2019-04-01
  • 实例类别:Java语言基础
  • 发 布 人:太白神龙
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 飞机 飞机大战

实例介绍

【实例简介】

    本项目是一个使用java做的一个飞机大战的小游戏,一个英雄机,初始有三次生命,当打中蜜蜂会有一次生命奖励,当打中敌机会有相应分数奖励,但如果被敌机打中会失去一次生命机会。如果生命都失去,则游戏结束。

【实例截图】

from clipboard

【核心代码】

public static final int WIDTH = 400; // 面板宽 
public static final int HEIGHT = 654; // 面板高 
/** 游戏的当前状态: START RUNNING PAUSE GAME_OVER */
private int state; 
private static final int START = 0; 
private static final int RUNNING = 1; 
private static final int PAUSE = 2; 
private static final int GAME_OVER = 3; 
 
private int score = 0; // 得分 
private Timer timer; // 定时器 
private int intervel = 1000 / 100; // 时间间隔(毫秒) 
 
public static BufferedImage background; 
public static BufferedImage start; 
public static BufferedImage airplane; 
public static BufferedImage bee; 
public static BufferedImage bullet; 
public static BufferedImage hero0; 
public static BufferedImage hero1; 
public static BufferedImage pause; 
public static BufferedImage gameover; 
 
private FlyingObject[] flyings = {}; // 敌机数组 
private Bullet[] bullets = {}; // 子弹数组 
private Hero hero = new Hero(); // 英雄机 
 
static { // 静态代码块,初始化图片资源 
  try { 
   background = ImageIO.read(ShootGame.class
     .getResource("/background.png")); 
   start = ImageIO.read(ShootGame.class.getResource("/start.png")); 
   airplane = ImageIO 
     .read(ShootGame.class.getResource("/airplane.png")); 
   bee = ImageIO.read(ShootGame.class.getResource("/bee.png")); 
   bullet = ImageIO.read(ShootGame.class.getResource("/bullet.png")); 
   hero0 = ImageIO.read(ShootGame.class.getResource("/hero0.png")); 
   hero1 = ImageIO.read(ShootGame.class.getResource("/hero1.png")); 
   pause = ImageIO.read(ShootGame.class.getResource("/pause.png")); 
   gameover = ImageIO 
     .read(ShootGame.class.getResource("/gameover.png")); 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } 

 
/** 画 */
@Override
public void paint(Graphics g) { 
  g.drawImage(background, 0, 0, null); // 画背景图 
  paintHero(g); // 画英雄机 
  paintBullets(g); // 画子弹 
  paintFlyingObjects(g); // 画飞行物 
  paintScore(g); // 画分数 
  paintState(g); // 画游戏状态 

 
/** 画英雄机 */
public void paintHero(Graphics g) { 
  g.drawImage(hero.getImage(), hero.getX(), hero.getY(), null); 

 
/** 画子弹 */
public void paintBullets(Graphics g) { 
  for (int i = 0; i < bullets.length; i ) { 
   Bullet b = bullets[i]; 
   g.drawImage(b.getImage(), b.getX() - b.getWidth() / 2, b.getY(), 
     null); 
  } 

 
/** 画飞行物 */
public void paintFlyingObjects(Graphics g) { 
  for (int i = 0; i < flyings.length; i ) { 
   FlyingObject f = flyings[i]; 
   g.drawImage(f.getImage(), f.getX(), f.getY(), null); 
  } 

 
/** 画分数 */
public void paintScore(Graphics g) { 
  int x = 10; // x坐标 
  int y = 25; // y坐标 
  Font font = new Font(Font.SANS_SERIF, Font.BOLD, 22); // 字体 
  g.setColor(new Color(0xFF0000)); 
  g.setFont(font); // 设置字体 
  g.drawString("SCORE:" score, x, y); // 画分数 
  y=y 20; // y坐标增20 
  g.drawString("LIFE:" hero.getLife(), x, y); // 画命 

 
/** 画游戏状态 */
public void paintState(Graphics g) { 
  switch (state) { 
  case START: // 启动状态 
   g.drawImage(start, 0, 0, null); 
   break; 
  case PAUSE: // 暂停状态 
   g.drawImage(pause, 0, 0, null); 
   break; 
  case GAME_OVER: // 游戏终止状态 
   g.drawImage(gameover, 0, 0, null); 
   break; 
  } 

 
public static void main(String[] args) { 
  JFrame frame = new JFrame("Fly"); 
  ShootGame game = new ShootGame(); // 面板对象 
  frame.add(game); // 将面板添加到JFrame中 
  frame.setSize(WIDTH, HEIGHT); // 设置大小 
  frame.setAlwaysOnTop(true); // 设置其总在最上 
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 默认关闭操作 
  frame.setIconImage(new ImageIcon("images/icon.jpg").getImage()); // 设置窗体的图标 
  frame.setLocationRelativeTo(null); // 设置窗体初始位置 
  frame.setVisible(true); // 尽快调用paint 
 
  game.action(); // 启动执行 

 
/** 启动执行代码 */
public void action() { 
  // 鼠标监听事件 
  MouseAdapter l = new MouseAdapter() { 
   @Override
   public void mouseMoved(MouseEvent e) { // 鼠标移动 
    if (state == RUNNING) { // 运行状态下移动英雄机--随鼠标位置 
     int x = e.getX(); 
     int y = e.getY(); 
     hero.moveTo(x, y); 
    } 
   } 
 
   @Override
   public void mouseEntered(MouseEvent e) { // 鼠标进入 
    if (state == PAUSE) { // 暂停状态下运行 
     state = RUNNING; 
    } 
   } 
 
   @Override
   public void mouseExited(MouseEvent e) { // 鼠标退出 
    if (state == RUNNING) { // 游戏未结束,则设置其为暂停 
     state = PAUSE; 
    } 
   } 
 
   @Override
   public void mouseClicked(MouseEvent e) { // 鼠标点击 
    switch (state) { 
    case START: 
     state = RUNNING; // 启动状态下运行 
     break; 
    case GAME_OVER: // 游戏结束,清理现场 
     flyings = new FlyingObject[0]; // 清空飞行物 
     bullets = new Bullet[0]; // 清空子弹 
     hero = new Hero(); // 重新创建英雄机 
     score = 0; // 清空成绩 
     state = START; // 状态设置为启动 
     break; 
    } 
   } 
  }; 
  this.addMouseListener(l); // 处理鼠标点击操作 
  this.addMouseMotionListener(l); // 处理鼠标滑动操作 
 
  timer = new Timer(); // 主流程控制 
  timer.schedule(new TimerTask() { 
   @Override
   public void run() { 
    if (state == RUNNING) { // 运行状态 
     enterAction(); // 飞行物入场 
     stepAction(); // 走一步 
     shootAction(); // 英雄机射击 
     bangAction(); // 子弹打飞行物 
     outOfBoundsAction(); // 删除越界飞行物及子弹 
     checkGameOverAction(); // 检查游戏结束 
    } 
    repaint(); // 重绘,调用paint()方法 
   } 
 
  }, intervel, intervel); 

 
int flyEnteredIndex = 0; // 飞行物入场计数 
 
/** 飞行物入场 */
public void enterAction() { 
  flyEnteredIndex ; 
  if (flyEnteredIndex % 40 == 0) { // 400毫秒生成一个飞行物--10*40 
   FlyingObject obj = nextOne(); // 随机生成一个飞行物 
   flyings = Arrays.copyOf(flyings, flyings.length 1); 
   flyings[flyings.length - 1] = obj; 
  } 

 
/** 走一步 */
public void stepAction() { 
  for (int i = 0; i < flyings.length; i ) { // 飞行物走一步 
   FlyingObject f = flyings[i]; 
   f.step(); 
  } 
 
  for (int i = 0; i < bullets.length; i ) { // 子弹走一步 
   Bullet b = bullets[i]; 
   b.step(); 
  } 
  hero.step(); // 英雄机走一步 

 
/** 飞行物走一步 */
public void flyingStepAction() { 
  for (int i = 0; i < flyings.length; i ) { 
   FlyingObject f = flyings[i]; 
   f.step(); 
  } 

 
int shootIndex = 0; // 射击计数 
 
/** 射击 */
public void shootAction() { 
  shootIndex ; 
  if (shootIndex % 30 == 0) { // 300毫秒发一颗 
   Bullet[] bs = hero.shoot(); // 英雄打出子弹 
   bullets = Arrays.copyOf(bullets, bullets.length bs.length); // 扩容 
   System.arraycopy(bs, 0, bullets, bullets.length - bs.length, 
     bs.length); // 追加数组 
  } 

 
/** 子弹与飞行物碰撞检测 */
public void bangAction() { 
  for (int i = 0; i < bullets.length; i ) { // 遍历所有子弹 
   Bullet b = bullets[i]; 
   bang(b); // 子弹和飞行物之间的碰撞检查 
  } 

 
/** 删除越界飞行物及子弹 */
public void outOfBoundsAction() { 
  int index = 0; // 索引 
  FlyingObject[] flyingLives = new FlyingObject[flyings.length]; // 活着的飞行物 
  for (int i = 0; i < flyings.length; i ) { 
   FlyingObject f = flyings[i]; 
   if (!f.outOfBounds()) { 
    flyingLives[index ] = f; // 不越界的留着 
   } 
  } 
  flyings = Arrays.copyOf(flyingLives, index); // 将不越界的飞行物都留着 
 
  index = 0; // 索引重置为0 
  Bullet[] bulletLives = new Bullet[bullets.length]; 
  for (int i = 0; i < bullets.length; i ) { 
   Bullet b = bullets[i]; 
   if (!b.outOfBounds()) { 
    bulletLives[index ] = b; 
   } 
  } 
  bullets = Arrays.copyOf(bulletLives, index); // 将不越界的子弹留着 

 
/** 检查游戏结束 */
public void checkGameOverAction() { 
  if (isGameOver()==true) { 
   state = GAME_OVER; // 改变状态 
  } 

 
/** 检查游戏是否结束 */
public boolean isGameOver() { 
 
  for (int i = 0; i < flyings.length; i ) { 
   int index = -1; 
   FlyingObject obj = flyings[i]; 
   if (hero.hit(obj)) { // 检查英雄机与飞行物是否碰撞 
    hero.subtractLife(); // 减命 
    hero.setDoubleFire(0); // 双倍火力解除 
    index = i; // 记录碰上的飞行物索引 
   } 
   if (index != -1) { 
    FlyingObject t = flyings[index]; 
    flyings[index] = flyings[flyings.length - 1]; 
    flyings[flyings.length - 1] = t; // 碰上的与最后一个飞行物交换 
 
    flyings = Arrays.copyOf(flyings, flyings.length - 1); // 删除碰上的飞行物 
   } 
  } 
 
  return hero.getLife() <= 0; 

 
/** 子弹和飞行物之间的碰撞检查 */
public void bang(Bullet bullet) { 
  int index = -1; // 击中的飞行物索引 
  for (int i = 0; i < flyings.length; i ) { 
   FlyingObject obj = flyings[i]; 
   if (obj.shootBy(bullet)) { // 判断是否击中 
    index = i; // 记录被击中的飞行物的索引 
    break; 
   } 
  } 
  if (index != -1) { // 有击中的飞行物 
   FlyingObject one = flyings[index]; // 记录被击中的飞行物 
 
   FlyingObject temp = flyings[index]; // 被击中的飞行物与最后一个飞行物交换 
   flyings[index] = flyings[flyings.length - 1]; 
   flyings[flyings.length - 1] = temp; 
 
   flyings = Arrays.copyOf(flyings, flyings.length - 1); // 删除最后一个飞行物(即被击中的) 
 
   // 检查one的类型(敌人加分,奖励获取) 
   if (one instanceof Enemy) { // 检查类型,是敌人,则加分 
    Enemy e = (Enemy) one; // 强制类型转换 
    score = e.getScore(); // 加分 
   } else { // 若为奖励,设置奖励 
    Award a = (Award) one; 
    int type = a.getType(); // 获取奖励类型 
    switch (type) { 
    case Award.DOUBLE_FIRE: 
     hero.addDoubleFire(); // 设置双倍火力 
     break; 
    case Award.LIFE: 
     hero.addLife(); // 设置加命 
     break; 
    } 
   } 
  } 

 
/** 
  * 随机生成飞行物 
  * 
  * @return 飞行物对象 
  */
public static FlyingObject nextOne() { 
  Random random = new Random(); 
  int type = random.nextInt(20); // [0,20) 
  if (type < 4) { 
   return new Bee(); 
  } else { 
   return new Airplane(); 
  } 


标签: 飞机 飞机大战

实例下载地址

java 飞机大战 小游戏源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警