swing 飞机大战 四 生成敌人飞机和敌人子弹
来源:互联网 发布:mac桌面双桌面 编辑:程序博客网 时间:2024/04/23 19:15
敌人飞机类,因为后面还想做些关卡弄不同的飞机,所以加了个飞机类型,线程根据不同的飞机类型类控制移动的规则
package Game;import java.util.Vector;import javax.swing.ImageIcon;public class FoeFly extends Fly implements Runnable{public Vector<FoeFly> foeFlyArr;public ImageIcon img;public int type; //敌人飞机类型public FoeFly(){foeFlyArr = new Vector<>(); //实例化敌人飞机集合}public FoeFly(int x, int y, int hp, boolean doom, int type, String path){super(x,y,hp,doom);this.type = type;img = new ImageIcon(path);}@Overridepublic void run() { //敌人飞机移动线程while (true){try {Thread.sleep(8);} catch (InterruptedException e) {e.printStackTrace();}for (int i=0; i<foeFlyArr.size(); i++){if (foeFlyArr.get(i).type == 1){ //类型一飞机,直线向下移动if (foeFlyArr.get(i).y > 600){foeFlyArr.remove(i);i--;continue;}foeFlyArr.get(i).y += 1;}}}}}通过一个线程类来创造敌人飞机
package pass;import Game.FoeFly;/** * * @author Administrator *关卡1的创建飞机线程 */public class ProduceNo1 implements Runnable{FoeFly foefly;public ProduceNo1(FoeFly foefly) {this.foefly = foefly;}@Overridepublic void run() {while (true){try {Thread.sleep(2000);//间隔两秒生成一架敌人飞机} catch (InterruptedException e) {// TODO 自动生成的 catch 块e.printStackTrace();}int x = (int)Math.round(Math.random()*350);int y = (int)Math.round(Math.random()*(-20));foefly.foeFlyArr.add(new FoeFly(x,y,40,true,1,"img/Foefly-1.png"));}}}
敌人子弹通过线程创建出来(在敌人飞机位置生成)
package Game;/** * * @author Administrator *生成敌人子弹类线程 */public class Pfb implements Runnable{FoeFly foefly;FoeBomb foebomb;public Pfb(FoeFly foefly, FoeBomb foebomb){this.foefly = foefly;this.foebomb = foebomb;}@Overridepublic void run() {while (true){//给每个子弹类在敌人飞机位置生成 子弹for (int i=0 ;i<foefly.foeFlyArr.size(); i++){foebomb.bombarr.add(new FoeBomb(foefly.foeFlyArr.get(i).x+25, foefly.foeFlyArr.get(i).y+50, true,"img/drzd-1.png"));}try {Thread.sleep(1500);} catch (InterruptedException e) {e.printStackTrace();}}}}敌人子弹类
package Game;import java.util.Vector;import javax.swing.ImageIcon;/** * * @author Administrator *敌人子弹线程 */public class FoeBomb extends Bomb implements Runnable{public FoeBomb(){bombarr = new Vector<>();}public FoeBomb(int x, int y, boolean vis, String path) {super(x, y, vis);img = new ImageIcon(path);}@Overridepublic void run() {while (true){try {Thread.sleep(7);} catch (InterruptedException e) {e.printStackTrace();}for (int i=0; i<bombarr.size(); i++){if (bombarr.get(i).y > 600){//超出边界bombarr.remove(i);i--;continue;}bombarr.get(i).y += 1;}}}}
效果图
阅读全文
0 0
- swing 飞机大战 四 生成敌人飞机和敌人子弹
- swing 飞机大战 五 击落敌人飞机
- 飞机大战03之飞机发射子弹,添加敌人
- pygame编写飞机大战(7)-敌人飞机类
- swing 飞机大战 三 自己飞机的移动和发射子弹
- 飞机大战-子弹的实现
- 项目飞机大战01:主要实现背景的移动,飞机的动画和触控,子弹的生成和发射
- 基于cocos2dx的飞机大战学习[四]-添加英雄子弹
- unity学习之飞机大战制作 关键点(三) 设置子弹的生成和移动
- 飞机大战(四)
- swing 飞机大战 开始准备
- 飞机大战之三:添加子弹
- pygame编写飞机大战(5)-子弹类
- cocos2d-x 3.2 |飞机大战:飞机与子弹
- 飞机大战开发05按角度发射的子弹和拖尾子弹的实现
- swing飞机大战 一 开始界面和选关界面
- 豪华版飞机大战系列(四)
- java写飞机大战四
- 监督学习——决策树
- input type="file"文件上传到后台读取
- 线性表之顺序存储结构
- 特征向量的几何意义
- Top 6 Ethical Issues in Artificial Intelligence
- swing 飞机大战 四 生成敌人飞机和敌人子弹
- 关于浏览器和微信顽固缓存的解决方法
- 文章标题
- 汇编代码,传递指针给函数
- Markdown
- IDEA快捷键及小技巧
- Python操作MySQL-(2)连接对象和游标对象
- 【Struts2框架学习】
- 树的子结构和拓扑相同的子树