/**
 * 窗口类 飞机游戏
 * @author 小帆敲代码
 *
 */
public class MyGameFrame extends JFrame{
 Plane plane=new Plane(GetImage.getImage("image/plane0.jpg"),Constant.PLANE_START_X,Constant.PLANE_START_Y,Constant.PLANE_SPEED);
 ArrayList<Shell> shells=new ArrayList();
 Exploer exploer;//爆炸
 boolean flag=true;//控制画面停止
 Timer timer=new Timer();//计时器
 
    //初始化窗口
 public void init() {
  this.setTitle("小帆敲代码--飞机游戏");
  this.setVisible(true);
  this.setSize(Constant.FRAME_WIDTH, Constant.FRAME_HEITH);
  this.setLocation(300,100);
  
  timer.startTimer();//开始计时
  
  //内部类实现重画和动画
  class FrameThread extends Thread{
   public void run() {
    while(flag) {
    repaint();
    try {
     Thread.sleep(40);//形成动画
    } catch (InterruptedException e) {
    }
    }
   }  
  }
  new FrameThread().start();
  
  //添加炮弹
  for(int i=0;i<Constant.SHELL_NUM;i++) {
   shells.add(new Shell());
  }
  
  class KeyMinitor extends KeyAdapter{//键盘监听 内部实现类
   //重写按下
   public void keyPressed(java.awt.event.KeyEvent e) {
    plane.addMove(e);
   };
   //松开
   public void keyReleased(java.awt.event.KeyEvent e) {
    plane.miniMove(e);
   };
  }
  this.addWindowListener(new WindowAdapter(){//设置窗口监听
   public void windowClosing(WindowEvent e) {
    System.exit(0);//退出
   }
  });
  this.addKeyListener(new KeyMinitor());//键盘监听
 }
 public void paint(Graphics g) {
  Color c=g.getColor();
  g.drawImage(GetImage.getImage("image/bg.jpg"), 0,0, null);//bg
  
  plane.drawSelf(g);//飞机
  
  for(int i=0;i<shells.size();i++) {
   if(plane.live)
   {
    shells.get(i).drawSelfMoving(g);
    //检测碰撞
    boolean boom=shells.get(i).getRect().intersects(plane.getRect());
   if(boom) {
    plane.live=false;//飞机结束
    exploer=new Exploer(plane.x,plane.y);
    timer.running=false;//计时停止
     }
   }
   else {
    //游戏结束场景 炮弹静止 飞机爆炸
    shells.get(i).drawSelfStop(g);
    exploer.draw(g);
   }
  }
  //显示时间
  g.setColor(Color.WHITE);
  Font f=new Font("宋体",Font.BOLD,50);
  g.setFont(f);
  g.drawString(timer.time+"秒",30,100);
  g.setColor(c);
  }
 public static void main(String[] args) {
  MyGameFrame f=new MyGameFrame();
  f.init();
 }
}
/**
 * 计时器
 * @author 小帆敲代码
 *
 */
public class Timer {
int time;
 boolean running=true;
 
 public void startTimer() {
  new Thread(()-> {
    while (running) {
     try {
      Thread.sleep(1000);
     } catch (InterruptedException e) {
     }
     time++;
    }
  }).start();
 }
}
/**
 * 炮弹
 * @author 小帆敲代码
 *
 */
public class Shell extends GameObject{
 double degree;//角度
 
 public Shell() {
  //初始位置
  x=Math.random()*Constant.FRAME_WIDTH;
  y=Math.random()*(Constant.FRAME_HEITH-200);
  
  //宽高速度
  width=Constant.SHELL_WIDTH;
  height=Constant.SHELL_HEIGTH;
  speed=Constant.PLANE_SPEED;
  
  //随机角度
  degree=Math.random()*Math.PI*2;
 }
 //炮弹移动
 public void drawSelfMoving(Graphics g) {
  Color c=g.getColor();
  g.setColor(Color.YELLOW);
  
  g.fillOval((int)x, (int)y, width, height);
  //移动的坐标变化
  x+=speed*Math.cos(degree);
  y+=speed*Math.sin(degree);
  
  //碰到墙壁的角度变化
  if(x<0||x>Constant.FRAME_WIDTH-width) {
   degree=Math.PI-degree;
  }
  if(y<30||y>Constant.FRAME_HEITH-height) {
   degree=-degree;
  }
  g.setColor(c);
 }
 //炮弹静止
 public void drawSelfStop(Graphics g) {
  Color c=g.getColor();
  g.setColor(Color.YELLOW);
  g.fillOval((int)x, (int)y, width, height);
  g.setColor(c);
  
 }
}
/**
 * 飞机类
 * @author 小帆敲代码
 *
 */
public class Plane extends GameObject{
 boolean left,right,up,down;//使用可以同时向两个方向行驶
 boolean live=true;//游戏状态
 
 //飞机显示
 public void drawSelf(Graphics g) {
  if(live) {
  if(up) {
   y-=speed;
  }
  if(down) {
   y+=speed;
  }
  if(right) {
   x+=speed;
  }
  if(left) {
   x-=speed;
  }
  g.drawImage(img,(int)x,(int)y,null);
  }
 }
 
 //WASD建控制移动
 //如果直接对位置进行修改 没办法沿两个方向同时进行
 public void addMove(KeyEvent e) {
  switch(e.getKeyCode()) {
  case KeyEvent.VK_W:
   up=true;break;
  case KeyEvent.VK_S:
   down=true;break;
  case KeyEvent.VK_D:
   right=true;break;
  case KeyEvent.VK_A:
   left=true;break;
  }
 }
 public void miniMove(KeyEvent e) {
  switch(e.getKeyCode()) {
  case KeyEvent.VK_W:
   up=false;break;
  case KeyEvent.VK_S:
   down=false;break;
  case KeyEvent.VK_D:
   right=false;break;
  case KeyEvent.VK_A:
   left=false;break;
  }
 }
 
public Plane(Image img,double x,double y,int speed) {
 super(x,y,speed,img);
 height=img.getHeight(null);
 width=img.getWidth(null);
}
public Plane() {
}
}
/**
 * 工具类 用来获得图片
 * @author 小帆敲代码
 *
 */
public class GetImage {
private GetImage() {//构造函数私有化
}
public static Image getImage(String path) {
 BufferedImage bi=null;
 try {
  URL u=GetImage.class.getClassLoader().getResource(path);
  bi=ImageIO.read(u);
 } catch (IOException e) {
  e.printStackTrace();
 }
 return bi;
}
}
/**
 * 物体的根类
 * @author 小帆敲代码
 *
 */
public class GameObject {
    //坐标
 double x,y;
 //长宽
 int width,height;
 //运动速度
 int speed;
 //图片
 Image img;
 
 //显示自己
 public void drawSelf(Graphics g) {
  g.drawImage(img,(int)x,(int)y,null);
 }
 
 //得到矩形用于碰撞检测
 public Rectangle getRect() {
  return new Rectangle((int)x,(int)y,width,height);
 }
 
 public GameObject(double x, double y, int speed, Image img) {
  super();
  this.x = x;
  this.y = y;
  this.speed = speed;
  this.img = img;
 }
 public GameObject() {
 }
}
/**
 * 爆炸类
 * @author 小帆敲代码
 *
 */
public class Exploer {
double x,y;
 //动画爆炸 静态加载
 static Image[] images=new Image[14];
 static {
  for(int i=0;i<14;i++) {
   images[i]=GetImage.getImage("image/exploer/boom"+(i+1)+".gif");
   images[i].getWidth(null);
  }
 }
 
 int count;
 public void draw(Graphics g) {//爆炸场景
  new Thread(()-> {
   for(int i=0;i<13;i++)
   {
    if(count<=13) {
     g.drawImage(images[count], (int)x,(int) y, null);
     count++;
    } 
    try {
     Thread.sleep(180);
    } catch (InterruptedException e) {
    }
   }
  }).start();
 }
// 
// int begingsize=50;
// public void draw(Graphics g) {
//  Color c=g.getColor();
//  g.setColor(Color.RED);
//  g.fillOval((int)x,(int) y, begingsize, begingsize);
//  if(begingsize>0)
//  {
//   begingsize--;
//  }
//  
//  g.setColor(c);
// }
 public Exploer(double x, double y) {
  this.x = x;
  this.y = y;
 }
}
/**
 * 常量
 * @author 小帆敲代码
 *
 */
public class Constant {
public static final int FRAME_WIDTH=500;
 public static final int FRAME_HEITH=500;
 
    public static final int SHELL_NUM=25;
    public static final int SHELL_WIDTH=15;
    public static final int SHELL_HEIGTH=15;
    public static final int SHELL_SPLEED=4;
   
   
    public static final int PLANE_START_X=150;
    public static final int PLANE_START_Y=350;
    public static final int PLANE_SPEED=4;
}

飞机大战小游戏1.0相关推荐

  1. 飞机大战小游戏1.0版本

    小时候大家应该都玩过飞机大战吧,这就是仿的一个飞机大战,但是没有写的很全,只能玩一次,死掉之后需要刷新页面玩第二次,话不说多,上代码: 初始页面: 整个的html代码还是很少,如下: <div ...

  2. python 飞机大战小游戏

    飞机大战小游戏,这里需要下载pygame模块 这是需要的素材,需要的自取: 上代码: import time import pygame from pygame.locals import *#检测事 ...

  3. 点击list view中一行内容可以在combox中显示_java版飞机大战小游戏详细教程(零基础小白也可以分分钟学会!)...

    一:游戏展示 飞机大战小游戏我们都玩过,通过移动飞机来打敌机,这里给大家展示一下游戏成果:呜呜呜由于gif只能上传5M大小,所以就不能给大家展示操作了,如果大家有兴趣可以自己自己做出来再玩哟. 这里面 ...

  4. 飞机大战小游戏(超详细)

    偷学Python之最后的项目二:飞机大战小游戏(超详细) 古之立大事者,不惟有超世之才,亦必有坚忍不拔之志.--苏轼 甜甜先说 这次用Python中的pygame模块来完成一个飞机大战的小游戏:基本思 ...

  5. 使用小程序制作一个飞机大战小游戏

    此文主要基于微信小程序制作一个飞机大战小游戏,上手即用,操作简单. 一.创建小程序 二.页面实现 三.代码块 一.创建小程序 访问微信公众平台,点击账号注册. 选择小程序,并在表单填写所需的各项信息进 ...

  6. C语言—飞机大战小游戏

    哈工大经典C语言大作业-飞机大战小游戏,源码如下,已经通过编译获得评分19+ (满分20)当时还是太菜了呜呜呜. 可以给大家参考一下,好像本来是加了音乐的,但是你们可能没有对应的音乐MP3文件,所以如 ...

  7. 基于Java语言在窗体上实现飞机大战小游戏

    全套资料下载地址:https://download.csdn.net/download/sheziqiong/85594271 项目介绍 飞机大战:用 Java 语言在窗体上实现飞机大战小游戏,运行程 ...

  8. 【Java代码实现飞机大战小游戏】简单理解

    飞机大战 飞机大战小游戏历经10天完成,主要用到的就是我们面向对象部分的知识:类,封装,继承,多态,静态代码块等等内容+swing部分内容.所以即使你是java小白,也不用担心欧! 游戏说明:游戏有3 ...

  9. 【Java】Java基础飞机大战小游戏完整代码

    Java基础飞机大战小游戏完整代码 先来展示一下代码实现结果图 主函数ShootGame 初始化游戏原始背景图片,游戏人物图片,游戏开始结束图片:构建产生敌人算法:产生英雄机算法:发射子弹算法:判断是 ...

  10. Vue 开发一个简略版的飞机大战小游戏

    文章目录 使用 Vue 开发一个简略版的飞机大战小游戏 一.实现思路 二.所需知识点 三.实现步骤 使用 Vue 开发一个简略版的飞机大战小游戏 如题,假设你为了向更多访问你博客的人展示你的技术,你决 ...

最新文章

  1. 《UML用户指南第二版》再次温读笔记(一)(downmoon)
  2. ITK:创建具有相关类ID的样本列表
  3. Navicat将mysql表结构导成oracle表结构
  4. Flutter、ReactNative、uniapp对比
  5. MySQL下载和安装以及问题解决
  6. 数字图像处理(作业一)——matlab工具箱初探
  7. hdu5033 Building 单调队列
  8. 东北大学linux程序设计考试,Linux认证考试试题及答案
  9. html5 怎么插指南针,HTML5 App实战(5):指南针
  10. yang模型中rpc_RPC原理篇
  11. Counting swaps
  12. C语言:字母金字塔(输入一个大写字母,输出从A到这个字母的金字塔
  13. JTAG与SWD接口定义映射
  14. windows Outlook邮箱无法连接服务器
  15. android实现棱形效果
  16. 1688官网以图搜图爬虫案例
  17. android 图库刷新,关于主动刷新Android系统图库方法总结
  18. USB协议与Windows USB设备驱动程序笔记
  19. 最后的颁奖典礼:汉芯的幕后英雄集体亮相
  20. Python编程:从入门到实践-第二章:变量和简单数据(语法)

热门文章

  1. 《零基础入门学习Python》学习过程笔记【40类和对象的相关内置函数】
  2. PL/SQL 包头和包体
  3. WPF的TextBox产生内存泄露的情况
  4. 北京赛区参赛选手代表吴翼在开幕式上的发言稿完整版2011年11月02日 09:50:34
  5. 深入理解strcpy,strncpy
  6. matlab红字怎么删除,matlab-系统爱好者
  7. 思科bfd静态路由切换_配置静态路由与BFD联动
  8. 拓端tecdat|R语言在RCT中调整基线时对错误指定的稳健性
  9. 拓端tecdat|用R语言软件估计光谱密度
  10. oracle 9i linux内核,在Linux下安装Oracle9i_oracle