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 ...
随机推荐
- Compaction介绍
Compaction介绍 Compaction是buffer->flush->merge的Log-Structured Merge-Tree模型的关键操作,主要起到如下几个作用: 1)合并 ...
- 开发者必备,超实用的PHP代码片段!
此前,研发频道曾发布<直接拿来用,10个PHP代码片段>,得到了网友们的一致好评.本文,笔者将继续分享九个超级有用的PHP代码片段.当你在开发网站.应用或者博客时,利用这些代码能为你节省大 ...
- Spring Cloud Ribbon实现客户端负载均衡
1.构建microservice-consumer-movie-ribbon项目,在pom.xml中引入ribbon依赖 在引入Eureka依赖的时候,默认里面含有ribbon依赖 2.添加@Load ...
- rehash过程
步骤 1) 首先创建一个比现有哈希表更大的新哈希表(expand)2) 然后将旧哈希表的所有元素都迁移到新哈希表去(rehash) dictAdd 对字典添加元素的时候, _dictExpandI ...
- C++(零)— 提高程序运行效率
1.尽量减少值传递,多用引用来传递参数. 2.++i和i++引申出的效率问题,使用++i. 3.避免过大的循环,由计算机的硬件决定的. 4.局部变量VS静态变量,尽量使用局部变量. 5.减少除法运算的 ...
- Android开发中java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}
Android开发中java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: java.lang.NullPoi ...
- SQLyog中的计算适合的数据类型
可能使用的数据库工作比较杂吧(机器上有toad.PLSQL Developer.Navicat.SQLyog等).并非是觉得那种都不好用,而是觉得有适合大部分需求的,但也有工具在某一方面特别方便的. ...
- webpack vue-cli 一有空格和分号就报错
webpack vue-cli 一有空格和分号就报错 eslintrc.js 这是ESLint的配置文件,至于为什么用ESLint的话,就是为了自动检查代码,保持一致的代码风格,从而保证代码质量. 这 ...
- VSCode设置中文语言显示
Vscode是一款开源的跨平台编辑器.默认情况下,vscode使用的语言为英文(us),如何将其显示语言修改成中文了? 1)打开vscode工具: 2)使用快捷键组合[Ctrl+Shift+p],在搜 ...
- centos下安装storm
centOS安装ZeroMQ centOS安装ZeroMQ所需组件及工具: yum install gcc yum install gcc-c++ yum install make yum insta ...