import java.awt.Image;
import java.util.ArrayList;
import java.util.Timer; import javax.swing.ImageIcon; public class Plane { public Image image;
public int y = 300, x = 100, width = 50, height = 50;
private String imageName;
private boolean flag = true;// true表示子弹往下走,false表示子弹往上走
private int score;
private Timer timer; private PlaneDemo pd; public void setPD(PlaneDemo pd) {
this.pd = pd;
} public void setTimer(Timer timer) {
this.timer = timer;
} public Plane(int x, int y, int width, int height, String imageName,
boolean flag, int score) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.imageName = imageName;
this.flag = flag;
this.score = score;
image = new ImageIcon(this.getClass().getResource(imageName))
.getImage();
} public void move(ArrayList<Plane> list) {
if (imageName.equals("plane.jpg")) {
if (flag) {
y += 3;
if (y >= 400)
list.remove(this);
} else {
y -= 3;
if (y <= 40)
y = 300;
}
} else if (imageName.equals("bullet.png")) {
if (flag)
y += 3;
else
y -= 4;
if (y >= 400)
list.remove(this);
else if (y + height <= 0)
list.remove(this);
} else if (imageName.equals("background.jpg")) {
y += 2;
if (y >= 0)
y = -226;
}
} /**
* 检测当前飞机对象和其他的子弹或者飞机对象是否发生碰撞
*
* @param list就是存储了所有敌方飞机和子弹的对象
* (包括自己的)
*/
public void collision(ArrayList<Plane> list) {
for (int i = 1; i < list.size(); i++) {
Plane plane = list.get(i);// 获取队列中的飞机或者子弹对象
if (plane != this && plane.flag != flag) {// 检测不是自己本身
// 计算两个圆的距离
double distance = Math.sqrt((plane.x - this.x)
* (plane.x - this.x) + (plane.y - this.y)
* (plane.y - this.y));
// 判断两个图片是否发生了碰撞
if (distance <= plane.height / 2 + this.height / 2) {
// 销毁这两个图片
list.remove(i);
list.remove(this); // 判断是否要增加这一局游戏的分数
if (plane.imageName.equals("bullet.png") && !plane.flag) {
if (this.imageName.equals("plane.jpg")) {
PlaneDemo.count += this.score;
System.out.println(">>>>>>>>>>>1");
}
} else if (this.imageName.equals("bullet.png")
&& !this.flag) {
if (plane.imageName.equals("plane.jpg")) {
PlaneDemo.count += plane.score;
System.out.println(">>>>>>>>>>>2");
}
} // 停止我方飞机的自动生成子弹的线程对象
if ((!plane.flag && plane.imageName.equals("plane.jpg"))) {
plane.timer.cancel();
new SaveInfo(plane.pd);
} else if (!this.flag && this.imageName.equals("plane.jpg")) {
timer.cancel();
new SaveInfo(this.pd); } }
}
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList; import javax.swing.JFrame; public class PlaneDemo extends JFrame { public static void main(String[] args) {
new PlaneDemo();
} public static int count = 0;
private ArrayList<Plane> list;
private Image image; public PlaneDemo() {
list = new ArrayList<Plane>();
initUI(); Plane background = new Plane(0, -226, 1024, 626, "background.jpg",
false,0);
list.add(background);
} private void initUI() {
this.setTitle("线程控制图片移动");
this.setSize(600, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setResizable(false);
this.setVisible(true); PlaneListener pl = new PlaneListener(list, this); this.addMouseListener(pl); Thread t = new Thread(pl);
t.start(); } /**
* 重写窗体的paint方法
*/
public void paint(Graphics g) {
super.paint(g); for (int i = 0; i < list.size(); i++) {
Plane plane = list.get(i);
g.drawImage(plane.image, plane.x, plane.y, plane.width,
plane.height, this);
}
//设置要绘制文字的字体
g.setFont(new Font("正楷",Font.BOLD,50));
g.setColor(Color.WHITE);
//绘制分数
g.drawString("分数:"+count, 20, 60);
} }
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Timer; import javax.swing.JFrame; public class PlaneListener extends MouseAdapter implements Runnable { private ArrayList<Plane> list;
private PlaneDemo frame;
private int count = 0; private Plane plane = null; public PlaneListener(ArrayList<Plane> list, PlaneDemo frame) {
this.list = list;
this.frame = frame;
} public void mouseClicked(MouseEvent e) {
int x = e.getX();
if (count == 0) {
plane = new Plane(x, 300, 50, 50, "plane.jpg",false,0);
plane.setPD(frame);
//实例化定时任务类的对象
BulletAI task = new BulletAI(plane,list);
//实例化时间类的对象
Timer timer = new Timer();
//让时间类的对象来启动任务,第一个参数表示要启动的任务,第二个参数表示第一次执行的间隔时间,第三个之后每次执行的间隔时间
timer.schedule(task, 1000, 2000); plane.setTimer(timer); count++;
} else {
plane = new Plane(x, 40, 50, 50, "plane.jpg",true,5);
} list.add(plane);
} public void run() {
while (true) {
for (int i = 0; i < list.size(); i++) {
Plane plane = list.get(i);
plane.move(list);
if(i!=0)
plane.collision(list);
} frame.repaint(); try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} }
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField; public class SaveInfo extends JFrame { private PlaneDemo pd; public SaveInfo(PlaneDemo pd) {
this.pd = pd;
initUI();
} private void initUI() {
this.setTitle("保存信息");
this.setSize(200, 200);
this.setDefaultCloseOperation(2);
this.setLocationRelativeTo(pd);
this.setResizable(false);
this.setLayout(new FlowLayout()); JLabel labName = new JLabel("用户名:");
this.add(labName);
textName = new JTextField();
textName.setPreferredSize(new Dimension(100, 30));
this.add(textName);
JLabel labScore = new JLabel("分 数:");
this.add(labScore); JButton butSave = new JButton("保存");
this.add(butSave); JButton butReset = new JButton("取消");
this.add(butReset); this.setVisible(true); SaveListener l = new SaveListener(this); butSave.addActionListener(l);
butReset.addActionListener(l);
} public JTextField textName; public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream(
"src/xxj/thread0908/score.txt");
// 创建一个数据输出流对象
DataInputStream dis = new DataInputStream(fis); int length = dis.readByte(); StringBuffer strB = new StringBuffer(); for (int i = 0; i < length; i++) {
char c = dis.readChar();
strB.append(c);
}
int score = dis.readInt();
System.out.println("名字:"+strB.toString()+" 分数:"+score);
dis.close();
fis.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
} }
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; public class SaveListener implements ActionListener { private SaveInfo si; public SaveListener(SaveInfo si){
this.si = si;
} public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("取消"))
si.dispose();
else{
try {
FileOutputStream fos = new FileOutputStream("src/xxj/thread0908/score.txt");
//创建一个数据输出流对象
DataOutputStream dos = new DataOutputStream(fos); String name = si.textName.getText(); dos.writeByte(name.length());//写入名字的长度
//遍历字符串
for(int i=0;i<name.length();i++){
dos.writeChar(name.charAt(i));//将字符写入到文件中
} dos.writeInt(PlaneDemo.count);//写入分数 dos.flush();//强制写入
//关闭流
dos.close();
fos.close();
si.dispose();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} }
}
}
import java.util.ArrayList;
import java.util.TimerTask; /**
* 定义自动生成子弹的线程类
*
* @author 熊哥
*
*/
public class BulletAI extends TimerTask { private Plane bullet;// 声明子弹对象名
private Plane plane;// 声明飞机对象名
private ArrayList<Plane> list;// 声明存储子弹的数组队列对象名 /**
* 构造方法
*
* @param plane飞机对象
* @param list存储要绘制的飞机和子弹对象
*/
public BulletAI(Plane plane, ArrayList<Plane> list) {
this.plane = plane;
this.list = list;
} /**
* 线程的运行方法
*/
public void run() {
// 实例化子弹类的对象
bullet = new Plane(plane.x + plane.width / 2 - 2, plane.y, 4, 8,
"bullet.png", false,0);
// 将bullet对象添加到list中
list.add(bullet);
} }

java之飞机大战的记分标准的更多相关文章

  1. java版飞机大战 实战项目详细步骤.md

    [toc] 分析 飞机大战 首先对这个游戏分析,在屏幕上的物体都是飞行物,我们可以把建一个类,让其他飞行物继承这个类.游戏中应有英雄机(也就是自己控制的飞机).敌人.而敌人应该分为打死给分的飞机(就是 ...

  2. Java实现飞机大战游戏

    飞机大战详细文档 文末有源代码,以及本游戏使用的所有素材,将plane2文件复制在src文件下可以直接运行. 实现效果: 结构设计 角色设计 飞行对象类 FlyObject 战机类 我的飞机 MyPl ...

  3. java版飞机大战代码

    @ 目录 前言 Plane PlaneStatus类 Power类 Gift Diji play类 over类 MainFrame主类 MyZiDan DijiZiDan Before 前言 很久之前 ...

  4. 飞机大战编写以及Java的面向对象总结

    面向对象课程完结即可编写一个简单的飞机大战程序.我觉得我需要总结一下 飞机大战中类的设计: 父类:FlyingObject(抽象类) 接口:Award .Enemy 子类:Hero.Bullet.Ai ...

  5. Java飞机大战源代码

    刚学不久java,做了一个飞机大战的小小小小游戏,现在把这个思路总结以及代码分享出来.大佬别吐槽(emmmmmm .....开发环境:jdk1.7 开发工具:eclipese PlanelJPanel ...

  6. 用面向对象的编程方式实现飞机大战小游戏,java版

    概述 本文将使用java语言以面向对象的编程方式一步一步实现飞机大战这个小游戏 本篇文章仅供参考,如有写的不好的地方或者各位读者哪里没看懂可以在评论区给我留言 或者邮件8274551712@qq.co ...

  7. Java飞机大战MVC版

    PlaneWar Java飞机大战MVC版 //无聊时偷的雷霆战机素材写了一个飞机大战,本意是练习mvc,但写得还是不清晰 github下载:https://github.com/dejavudwh/ ...

  8. java飞机大战之子弹的自动生成

    import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing. ...

  9. java之线程飞机大战制作

    import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing. ...

随机推荐

  1. 序(转) · 为 React 而写 -- 废话比较多, 你也可以说丰满

    流形 2 年前 (废话比较多       从今年开始,就一直在规划技术沉淀这事. 在阿里巴巴工作的这些年,前端团队日益壮大,同时聚集了一帮志趣相投的伙伴. 作为团队负责人,一方面是借团队在技术道路上的 ...

  2. 简要谈谈javascript bind 方法

    最近去参加了场面试,跟面试官聊了很多JS基础上的东西,其中有个问题是谈谈对apply.call.bind的理解和区别.顿时一愣,apply.call我知道,经常用的东西,bind是什么鬼!!!好像见过 ...

  3. 你所不知道的,Java 中操作符的秘密?

    在 Java 编程的过程中,我们对数据的处理,都是通过操作符来实现的.例如,用于赋值的赋值操作符.用于运算的运算操作符等.用于比较的比较操作符,还包括逻辑操作符.按位操作符.移位操作符.三元操作符等等 ...

  4. 对结合BDD进行DDD开发的一点思考和整理

    引言 二十年前的我,还在学校里抱着一台DIY机(德州486+大众主板+16M内存+3.5inch软驱+昆腾320M硬盘,当时全校最快主机没有之一),揣着一本<Undocumented DOS&g ...

  5. [QT]问题记录-控件初始化导致程序异常关闭

    qt新手,在设置 pushButton 的字体颜色时,出现软件异常闭,代码如下: 按钮的初始化在  ui->setupUi(this); 前边,会出现一下问题. 解决办法:将按钮的初始化在  u ...

  6. HihoCoder 1097 kruscal

    最小生成树二·Kruscal算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 随着小Hi拥有城市数目的增加,在之间所使用的Prim算法已经无法继续使用了——但是幸运的 ...

  7. 在IIS站点中使用数字证书

    1. SSL解析(内容来自百度百科) SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安 ...

  8. cordic算法的fpga实现

    cordic算法参考:http://wenku.baidu.com/view/6c623aa8910ef12d2bf9e732.html 这是百度文库的一个文档,详细介绍了cordic算法的基本内容. ...

  9. flume采集log4j日志到kafka

    简单测试项目: 1.新建Java项目结构如下: 测试类FlumeTest代码如下: package com.demo.flume; import org.apache.log4j.Logger; pu ...

  10. HTTP/HTTPS原理详解

    简介 HTTP(Hypertext Transfer Protocal,超文本传输协议)是WWW(World Wide Web,万维网)数据传输的基础,规定如何传输超文本.HTTP协议存在多个版本:H ...