📄 nettalismansprite.java
字号:
import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;import java.io.*;import java.util.*;//法宝,由绿色红色敌人坦克,petSprite new出来 自己不断run,不断检测与玩家的碰撞,被玩家碰撞了,就eated,时间到了就deadpublic class NetTalismanSprite extends Sprite implements Runnable{ public static boolean gameover=false;//游戏结束 private static boolean interrupt;//坦克运动是否暂停 ,加在run中 public static void setInterrupt(boolean interrupt) { NetTalismanSprite.interrupt = interrupt; } //private static Timer timer = new Timer(); private int type;//1蔬菜(加分) 2蕃茄(加船) 3补血(修king) 4星星(射速) 5炸弹(消灭坦克) 6坦克(加生命) private boolean alive=false;//被new则true,被吃则false //实用的引用 private LayerManager layerManager;//结束游戏时,让layer画东西 private int tankIndex;//哪只玩家吃到它 private NetUserSprite[] us; private NetEnemySprite[] es;//所有敌人爆炸 private NetBulletSprite ubs[];//所有玩家的子弹 private NetTentSprite tentSprite;//帐篷 public NetTalismanSprite(Image image,int type,int x,int y,NetBattleCanvas bc,LayerManager layerManager) { super(image,image.getWidth()/2,image.getHeight()); defineReferencePixel(image.getWidth()/4,image.getHeight()/2); setRefPixelPosition(x, y); this.type = type; this.layerManager = layerManager; this.us = bc.getUserSprites(); this.es = bc.getEnemySprites(); this.ubs = bc.getUserBulletSprites(); this.tentSprite = bc.getTentSprite(); setFrameSequence(new int[]{0,1}); //添加到画布 setVisible(true); layerManager.insert(this,0); //自启动 alive = true; new Thread(this).start(); //Timer new DeadThread().start(); } public void run(){ while(!gameover && alive){ this.nextFrame(); for(int i=0;i<us.length;i++) if(this.collidesWith(us[i],false)){//检测玩家的碰撞 eated(); tankIndex = i; break; } try{ Thread.sleep(500); }catch(InterruptedException e){ e.printStackTrace(); } } } public void dead(){//时间到消失 int count=0; while(++count<10){//30秒 try {Thread.sleep(1000); } catch (InterruptedException ex) {ex.printStackTrace();} while(interrupt){//如果暂停则停止计数 try {Thread.sleep(300); } catch (InterruptedException ex) {ex.printStackTrace();} } } count=0; while(++count<10){//6秒 this.setVisible(false); try {Thread.sleep(300); } catch (InterruptedException ex) {ex.printStackTrace();} while(interrupt){//如果暂停则停止计数 if(gameover)return; try {Thread.sleep(300); } catch (InterruptedException ex) {ex.printStackTrace();} } this.setVisible(true); try {Thread.sleep(300); } catch (InterruptedException ex) {ex.printStackTrace();} while(interrupt){//如果暂停则停止计数 if(gameover)return; try {Thread.sleep(300); } catch (InterruptedException ex) {ex.printStackTrace();} } } alive = false; this.setVisible(false); layerManager.remove(this); } public void eated(){//被吃 if(type == 6)Sound.instance.eatTankPlayer(); else Sound.instance.eatPlayer(); alive = false; this.setVisible(false); layerManager.remove(this); //玩家吃了它,玩家要改变什么 if(type == 1){//加分 红十字 draw1(this.getRefPixelX(), this.getRefPixelY()); } else if(type == 2){//加船 船 us[tankIndex].setHasShip(true); } else if(type == 3){//保护层 this.us[tankIndex].addProtect(); } else if(type == 4){//速度 星星 if(this.us[tankIndex].SPEED == 1)this.us[tankIndex].SPEED ++; else {//加分 draw1(this.getRefPixelX(), this.getRefPixelY()); } } else if(type == 5){//炸弹 炸弹 for(int i=0;i<es.length;i++){ if(es[i].getHP()>0) es[i].explodes(); } } else if(type == 6){//加生命 坦克 this.us[tankIndex].setLifes(us[tankIndex].getLifes()+1); draw3(this.getRefPixelX(), this.getRefPixelY()); } else if(type == 7){//加帐篷 if(!this.tentSprite.isVisible()){ //添加到画布 this.tentSprite.setVisible(true); layerManager.insert(this.tentSprite,0); }else {//加分 draw1(this.getRefPixelX(), this.getRefPixelY()); } }else if(type == 8){//加蓝色子弹 this.ubs[0].setFrame(1); this.ubs[0].SpeedUp(); } } private class DeadThread extends Thread{ public void run(){ dead(); } } public void draw1(int x,int y){//加分 Image image=null; try{ image = Image.createImage("/tank/addScore.png"); }catch(IOException e){ e.printStackTrace(); } Sprite s = new Sprite(image,image.getWidth()/3,image.getHeight()); s.defineReferencePixel(image.getWidth()/6,image.getHeight()/2); s.setRefPixelPosition(x,y); layerManager.insert(s, 0); s.setVisible(true); int count=0; while(++count<3){ try {Thread.sleep(300); }catch (InterruptedException e) {e.printStackTrace();} s.nextFrame(); while(interrupt){//如果暂停,效果仍存在 try {Thread.sleep(300); }catch (InterruptedException e) {e.printStackTrace();} } } s.setVisible(false); layerManager.remove(s); } public void draw2(int x,int y){//加速度 } public void draw3(int x,int y){//加生命 }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -