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. ...
随机推荐
- 读书笔记-Ribbon源码分析
@LoadBalanced注解用来给RestTemplate做标记,以使用负载均衡的客户端来配置. 通过搜索LoadBalancerClient可以发现,LoadBalancerClient是Spri ...
- 【数据库】python访问mysql
import MySQLdb 所有的数据库遵循相同的python database API 需要建立connection对象连接数据库,之后建立cursor对象处理数据. conn = MySQLdb ...
- 软工15个人作业4——alpha阶段
一.个人总结 1.在alpha 结束之后, 每位同学写一篇个人博客, 总结自己的alpha 过程: 2.请用自我评价表:http://www.cnblogs.com/xinz/p/3852177.ht ...
- Arcgis For Android之离线地图实现的几种方式
为什么要用,我想离线地图的好处是不言而喻的,所以很多人做系统的时候都会考虑用离线地图.在此,我给大家介绍几种Arcgis For Android下加载离线地图的方式. 在Arcgis For Andr ...
- 旧书重温:0day2【10】第五章 堆溢出利用2
好久没有发帖子啦!最近一直很忙!但是还是抽空学习啦下! 前段时间匆匆忙忙的把0day2上的堆溢出实验做啦! 可能当时太浮躁啦,很多细节没注意!结果:实验结果很不满意!所以就有啦这一篇!! 上一篇是发布 ...
- keras 入门模型训练
# -*- coding: utf-8 -*- from keras.models import Sequential from keras.layers import Dense from kera ...
- Proposition
提供 \(k\) 个变量 \((k\leq 4)\) 可独立取值为 \(0,1\),两种运算分别等价于 \(\neg a\) 和 \(\neg a \lor b\) . 你需要恰好使用 \(n\) 个 ...
- HohoCoder 1184 : 连通性二·边的双连通分量(+原理证明)
1184 : 连通性二·边的双连通分量 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在基本的网络搭建完成后,学校为了方便管理还需要对所有的服务器进行编组,网络所的老师 ...
- N位N进制里有多少个N
32位二进制里有多少个1 https://blog.csdn.net/zhangsj1007/article/details/81411063 有这样一道计算机问题"32位二进制里面有多少个 ...
- linux下安装ZipArchive扩展和libzip扩展
在项目开发的时候,由于要下载多个录音文件,我就需要打包下载这个功能 学习源头: https://www.landui.com/help/show-8079 https://www.aliyun.com ...