java之线程飞机大战制作
import java.awt.Graphics;
import java.util.ArrayList; import javax.swing.JFrame;
import javax.swing.JPanel; public class PlaneMain extends JPanel { public static void main(String[] args) {
new PlaneMain();
} private ArrayList<View> list; public PlaneMain() {
list = new ArrayList<View>();
View background = new View("background.jpg", 0, -60, 700, 460, 2, this);
list.add(background);
initUI();
} private void initUI() {
JFrame frame = new JFrame("飞机大战");
frame.setSize(700, 400);
frame.setDefaultCloseOperation(3);
frame.setLocationRelativeTo(null);
frame.setResizable(false); frame.add(this); frame.setVisible(true); AddListener al = new AddListener(this, list); this.addMouseListener(al); Thread t = new Thread(al);
t.start();// 启动线程
} /**
* 重写JPanel的重绘方法
*/
public void paint(Graphics g) {
super.paint(g); for (int i = 0; i < list.size(); i++) {
View v = list.get(i);
g.drawImage(v.getBackground(), v.getX(), v.getY(), v.getWidth(),
v.getHeight(), this);
}
} }
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList; import javax.swing.JPanel; public class AddListener extends MouseAdapter implements Runnable { private int count = 0; private JPanel panel;
private ArrayList<View> list; public AddListener(JPanel panel, ArrayList<View> list) {
this.panel = panel;
this.list = list;
} public void mouseReleased(MouseEvent e) {
if (count % 2 == 0) {
View plane = new View("plane.jpg", e.getX(), e.getY(), 50, 50, 3,
panel);
list.add(plane);
count++;
} else {
View bullet = new View("bullet.png", e.getX(), e.getY(), 10, 20, 5,
panel);
list.add(bullet);
count++;
}
} public void run() {
while (true) {
for (int i = 0; i < list.size(); i++) {
View v = list.get(i);
v.move();
if(i!=0)
v.collisions(list);
} panel.repaint(); try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} }
} }
import java.awt.Image;
import java.util.ArrayList; import javax.swing.ImageIcon;
import javax.swing.JPanel; public class View { private Image background;
private int x = 0, y = -60, moveY, width, height;
private JPanel panel;
private String imageName; /**
* 构造方法
*
* @param background背景图片的对象
* @param x起始X坐标
* @param y起始Y坐标
*/
public View(String imageName, int x, int y, int width, int height,
int moveY, JPanel panel) {
this.imageName = imageName;
this.background = new ImageIcon(this.getClass().getResource(imageName))
.getImage();
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.moveY = moveY;
this.panel = panel;
} public int getWidth() {
return width;
} public void setWidth(int width) {
this.width = width;
} public int getHeight() {
return height;
} public void setHeight(int height) {
this.height = height;
} public Image getBackground() {
return background;
} public void setBackground(Image background) {
this.background = background;
} public int getX() {
return x;
} public void setX(int x) {
this.x = x;
} public int getY() {
return y;
} public void setY(int y) {
this.y = y;
} public int getMoveY() {
return moveY;
} public void setMoveY(int moveY) {
this.moveY = moveY;
} public JPanel getPanel() {
return panel;
} public void setPanel(JPanel panel) {
this.panel = panel;
} public void move() {
if (imageName.equals("background.jpg")) {
y += moveY;
if (y == 0)
y = -60;
} else if (imageName.equals("bullet.png")) {
y += moveY;
if (y >= 400)
y = 0;
} else if (imageName.equals("plane.jpg")) {
y -= moveY;
if (y <= 0)
y = 400;
}
} /**
* 碰撞方法
*/
public void collisions(ArrayList<View> list) {
for (int i = 1; i < list.size(); i++) {
View v = list.get(i);
if (this != v) {
double distance = Math.sqrt((this.x - v.x) * (this.x - v.x)
+ Math.pow(this.y - v.y, 2));
if (distance <= this.height + v.height) {
System.out.println(v.imageName + "和" + this.imageName
+ "发生了碰撞");
}
}
}
} }
java之线程飞机大战制作的更多相关文章
- Python版飞机大战
前面学了java用java写了飞机大战这次学完python基础后写了个python版的飞机大战,有兴趣的可以看下. 父类是飞行物类是所有对象的父类,setting里面是需要加载的图片,你可以换称自己的 ...
- Java飞机大战源代码
刚学不久java,做了一个飞机大战的小小小小游戏,现在把这个思路总结以及代码分享出来.大佬别吐槽(emmmmmm .....开发环境:jdk1.7 开发工具:eclipese PlanelJPanel ...
- java飞机大战之子弹的自动生成
import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing. ...
- 飞机大战编写以及Java的面向对象总结
面向对象课程完结即可编写一个简单的飞机大战程序.我觉得我需要总结一下 飞机大战中类的设计: 父类:FlyingObject(抽象类) 接口:Award .Enemy 子类:Hero.Bullet.Ai ...
- java版飞机大战 实战项目详细步骤.md
[toc] 分析 飞机大战 首先对这个游戏分析,在屏幕上的物体都是飞行物,我们可以把建一个类,让其他飞行物继承这个类.游戏中应有英雄机(也就是自己控制的飞机).敌人.而敌人应该分为打死给分的飞机(就是 ...
- 用面向对象的编程方式实现飞机大战小游戏,java版
概述 本文将使用java语言以面向对象的编程方式一步一步实现飞机大战这个小游戏 本篇文章仅供参考,如有写的不好的地方或者各位读者哪里没看懂可以在评论区给我留言 或者邮件8274551712@qq.co ...
- Java飞机大战MVC版
PlaneWar Java飞机大战MVC版 //无聊时偷的雷霆战机素材写了一个飞机大战,本意是练习mvc,但写得还是不清晰 github下载:https://github.com/dejavudwh/ ...
- 制作python程序windows安装包(飞机大战源码)
本文以飞机大战源码为例: 1.首先使用pyinstaller -w xxx.py打包 -w的意思是不显示命令行:飞机大战源码由多个.py文件以及一些图片,音乐文件组成,我们将main.py打包, ...
- Java实现飞机大战游戏
飞机大战详细文档 文末有源代码,以及本游戏使用的所有素材,将plane2文件复制在src文件下可以直接运行. 实现效果: 结构设计 角色设计 飞行对象类 FlyObject 战机类 我的飞机 MyPl ...
随机推荐
- html5学习笔记(forms)
forms api 规范 新的输入型控件新的函数和特性 新增input 类型 <input type="tel"> tel 电话号码email 电子邮箱URL 网页ur ...
- Uninstall Office 2016 for Mac
官方原文:https://support.office.com/en-us/article/Uninstall-Office-2016-for-Mac-eefa1199-5b58-43af-8a3d- ...
- 条款42:了解typename的双重含义
typename在很多种情况下与class是完全相同的,例如下面的使用: templame<typename T> ...... template<class T> ..... ...
- auto_ptr类
auto_ptr是一个模板类,用于管理动态内存分配. 请看下面的函数: void remodel (string& str) { string * ps = new string(str); ...
- PHP用mysql_insert_id()函数获得刚插入数据或当前发布文章的ID
向mysql 插入数据时,很多时候我们想知道刚刚插入数据的id,这对我们很有用.下面这篇文章就详细给大家介绍了利用mysql_insert_id()函数获得刚插入数据或当前发布文章的ID,有需要的朋友 ...
- html5视频video积累
又是好几个月没有写东西,还是太懒散了~必须要教育下自己罗~ 这次做了个播放视频的移动H5,之前没有仔细玩过,好好记录下基本知识,还有遇到的一些坑,方便之后再次遇见后进行解决 一.基本 video标签在 ...
- H5实现登录
1.主要就是获取cookie并每次发送请求时都带着:登录请求 2.添加HTTP Cookie管理器 3.登录请求时,加正则表达式提取器 取出返回的cookie ①使用charles抓包查看cookie ...
- 使用jsonp跨域调用百度js实现搜索框智能提示(转)
http://www.cnblogs.com/oppoic/p/baidu_auto_complete.html 项目中常常用到搜索,特别是导航类的网站.自己做关键字搜索不太现实,直接调用百度的是最好 ...
- CodeForces - 650D:Zip-line (LIS & DP)
Vasya has decided to build a zip-line on trees of a nearby forest. He wants the line to be as long a ...
- Jenkins之构建触发器配置(转载)
构建触发器配置,当你在文本框中输入配置的时间后,文本框下方会有时间解释,这样可以很好的看到自己配置的时间对不对. 可以清晰看到我的配置第一个运行时间是周五上午10点执行,第二次是星期六上午10点. ...