java之飞机大战的记分标准
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之飞机大战的记分标准的更多相关文章
- java版飞机大战 实战项目详细步骤.md
[toc] 分析 飞机大战 首先对这个游戏分析,在屏幕上的物体都是飞行物,我们可以把建一个类,让其他飞行物继承这个类.游戏中应有英雄机(也就是自己控制的飞机).敌人.而敌人应该分为打死给分的飞机(就是 ...
- Java实现飞机大战游戏
飞机大战详细文档 文末有源代码,以及本游戏使用的所有素材,将plane2文件复制在src文件下可以直接运行. 实现效果: 结构设计 角色设计 飞行对象类 FlyObject 战机类 我的飞机 MyPl ...
- java版飞机大战代码
@ 目录 前言 Plane PlaneStatus类 Power类 Gift Diji play类 over类 MainFrame主类 MyZiDan DijiZiDan Before 前言 很久之前 ...
- 飞机大战编写以及Java的面向对象总结
面向对象课程完结即可编写一个简单的飞机大战程序.我觉得我需要总结一下 飞机大战中类的设计: 父类:FlyingObject(抽象类) 接口:Award .Enemy 子类:Hero.Bullet.Ai ...
- Java飞机大战源代码
刚学不久java,做了一个飞机大战的小小小小游戏,现在把这个思路总结以及代码分享出来.大佬别吐槽(emmmmmm .....开发环境:jdk1.7 开发工具:eclipese PlanelJPanel ...
- 用面向对象的编程方式实现飞机大战小游戏,java版
概述 本文将使用java语言以面向对象的编程方式一步一步实现飞机大战这个小游戏 本篇文章仅供参考,如有写的不好的地方或者各位读者哪里没看懂可以在评论区给我留言 或者邮件8274551712@qq.co ...
- Java飞机大战MVC版
PlaneWar Java飞机大战MVC版 //无聊时偷的雷霆战机素材写了一个飞机大战,本意是练习mvc,但写得还是不清晰 github下载:https://github.com/dejavudwh/ ...
- java飞机大战之子弹的自动生成
import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing. ...
- java之线程飞机大战制作
import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing. ...
随机推荐
- canvas - 柱子效果
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- aac adts & LATM封装码流分析
本文继续上一篇文章的内容,介绍一个音频码流处理程序.音频码流在视频播放器中的位置如下所示. 本文中的程序是一个AAC码流解析程序.该程序可以从AAC码流中分析得到它的基本单元ADTS frame,并且 ...
- Lua基础---变量与赋值
看以下案例: test.lua -- 第一个lua脚本 --注释使用"--"符 --变量未定义时,默认初始化的值为nil --这样的定义为全局 num1 = 1 ; --加了关键字 ...
- Struts2 级联下拉框 详解析
目录(?)[+] 运行环境:myeclipse8.6+jboss5.1+jvm1.6 先看最后目录结构: 直接上源码: complexFormTag.jsp: <%@ page language ...
- matlab数据流仿真和时间流仿真
simulink 使用的动态系统仿真,仿真需要求状态方程和输出方程,关键是求状态方程,而状态方程的求解有多种算法,可变步长和定步长,所以仿真时对求解器的选择和步长的设置就比较重要. 所谓基於数据流的仿 ...
- Linux环境安装git
git配置 源码安装 检测当前git版本是否是2.7.4以上 git --version 如果没有安装git直接源码安装即可,如果安装了先删除原来的git. yum -y remove git 先安装 ...
- weblogic控制台的启动与禁用
在一些安全漏洞扫描中,经常会扫描发现,使用weblogic管理控制台,会有个中危的漏洞. http://192.168.10.46:7001/console/login/LoginForm.jsp W ...
- [Err] 1067 - Invalid default value for 'xxxTime'
下面是导入sql脚本的的局部脚本 `xxxTime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 发现是NO_ZERO_IN_DATE,NO_Z ...
- UNION和UNION ALL关键字
UNION和UNION ALL关键字都是将两个结果集合并为一个,但这两者从使用和效率上来说都有所不同. 1.对重复结果的处理:UNION在进行表链接后会筛选掉重复的记录,Union All不会去除重复 ...
- gson在android中的应用
首先需要建一个实体类 Person.java 来对应json 需要注意的是实体类中的变量名必须和json传过来的key值完全一样(大小写) public class Person { private ...