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. Activity has leaked window that was originally added(以解决)

     在编写Android程序的时候,遇到一个隐藏性问题.仔细查看LogCat,错误信息如下: 10-31 13:03:34.549: ERROR/WindowManager(444): Activi ...

  2. Python解析excel文件并存入sqlite数据库

    最近由于工作上的需求 需要使用Python解析excel文件并存入sqlite 就此做个总结 功能:1.数据库设计 建立数据库2.Python解析excel文件3.Python读取文件名并解析4.将解 ...

  3. vc++ windows获取计算机信息

    在软件开发中,我们经常要获当前系统的版本号,判断当前是什么系统,获取获取物理内存和可用内存大小,获取CPU名称.内核数目.主频,获取MAC地址,获取屏幕分辨率,下面的这个c++类将包含所有这些信息. ...

  4. 微博6月底升级后 报 10017/2/statuses/share.json或者10014/2/statuses/share.json错误

    一,背景 2017-06-26微博公告替换了一些接口,导致以前的: statuses/repost 转发一条微博 statuses/update 发布一条微博 statuses/upload 上传图片 ...

  5. matlab数据流仿真和时间流仿真

    simulink 使用的动态系统仿真,仿真需要求状态方程和输出方程,关键是求状态方程,而状态方程的求解有多种算法,可变步长和定步长,所以仿真时对求解器的选择和步长的设置就比较重要. 所谓基於数据流的仿 ...

  6. LOJ10050 The XOR Largest Pair

    题意 在给定的 \(N\) 个整数 \(A_1,A_2,-,A_N\) 中选出两个进行异或运算,得到的结果最大是多少? 对于 \(100\%\) 的数据,\(1\le N\le 10^5, 0\le ...

  7. admins.py总结比较,转

    转:http://blog.csdn.net/pipisorry/article/details/46764495

  8. openfaas 私有镜像配置

    备注: 此项目是使用nodejs  生成唯一id 的\ 预备环境 docker harbor faas-cli openfaas k8s 1. 项目初始化 faas-cli new node --la ...

  9. hbase使用中需要注意一些问题

    接触hbase已经两年之久,但是真正的在实际项目中使用却只有半年的时间,使用过程中,一方面在在为hbase强大的性能兴奋之余,另一方面却也给我和我的团队造成了很多的麻烦,起初在使用我的水平也就停留在会 ...

  10. selenium - 三种元素等待

    1.sleep 休眠方法 sleep()由python的time模块提供. 当执行到sleep()方法时,脚本会定时休眠所设置的时长,sleep()方法默认参数是s(秒),sleep(2) 表示休眠2 ...