⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 badsprite.java

📁 一个多线程的JAVA多线程游戏
💻 JAVA
字号:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.util.Random;

public class BadSprite  {
	int floorW,xp,yp;
	int[][] maze;
	char direction = 'R'; 
	Image        r_down,r_up,r_left,r_right,OffScreen,shot,bs_tank,explode;
	Thread       newThread;
	Graphics     drawOffScreen; 
	MediaTracker MT;
	Sprite s,s1; 
	Random  R;
	int     seed = 100;  
	boolean is_shot = false,is_live = true,is_rtank_live = true,is_rshot_go = true,dail = false;
	Applet Game; 
	BulletSprite b ,b1;
	
	public BadSprite(Image img1,Image img2,Image img3,Image img4,Image img5,Image img6,
			     int[][] maze,int xp,int yp,Sprite s1,BulletSprite b1 ,Applet Game){
		this.maze = maze;
	    r_up = img1;
	    r_left = img2;
	    r_down = img3;
	    r_right=img4;        
	    shot =img5;
	    explode = img6;
	    this.xp = xp;
	    this.yp = yp;
	    this.b1 = b1;
	    this.s1 = s1;
	    this.Game = Game;
	    init();
	}
	
	public void init(){
		
    R = new Random();
    floorW = r_left.getWidth(Game);
    s = new Sprite(floorW*xp, floorW*yp, floorW, floorW,yp,xp,maze);
    bs_tank = r_right;
    b = new BulletSprite(shot,Game,maze);
    b.updateState();
    newThread = new Thread();
	}
		
    public void draw_btank(Graphics g){
    	move();
    	if(is_live){
		g.drawImage(bs_tank,s.LcX,s.LcY, Game);
		b.paintSprite(g);
    	}else if(!dail){
    		g.drawImage(explode,s.LcX,s.LcY, Game);
    		g.drawImage(explode,s.LcX,s.LcY, Game);
    		try {
	             Thread.sleep(500);
	          }catch (InterruptedException e){}
    		g.drawImage(explode,s.LcX,s.LcY, Game);
    		g.drawImage(explode,s.LcX,s.LcY, Game);
    		dail = true;
    	}
    }
   
    public void move(){  
    	if(is_live){
    	if(R.nextInt(seed+1) < 50)
    	   is_shot = true;
    	
        if(!s.isPassed(s.indexI, s.indexJ,direction)||R.nextInt(seed) < 20){    	  
    	  direct();
        }
      switch (direction){
    	 case 'U':
    		  s.moveUp();
              break;
	     case 'D':
	    	 s.moveDown();
	         break;
	     case 'L':
	    	 s.moveLeft();
	         break;
	     case 'R':
	    	 s.moveRight();
   	         break;
          }
      b.updateState();
      if(is_shot && b.canVisible() == false && b.canMove() == false){
   	             b.setdirection(direction);
   	             b.setVisible(true);
          	     b.setMove(true);
   	             b.setLocation(s.LcX + (floorW / 2),s.LcY+(floorW / 2));              	         
	  }else if (b.X == b1.X && b.Y == b1.Y ){
		   is_shot = false;
		   b.setVisible(false);
   	       b.setMove(false);
   	       is_rshot_go = false;
	  }
      
      if(b1.X < s.LcX + (floorW ) && b1.X > s.LcX && b1.Y >s.LcY && b1.Y < s.LcY + floorW){
    	  is_live = false;
      }
      if(b.X < s1.LcX + (floorW ) && b.X > s1.LcX && b.Y >s1.LcY && b.Y < s1.LcY + floorW){
    	  is_rtank_live = false;
      }
      
      is_shot = false;
     }
    }
    
    public void direct(){
    	if(R.nextInt(seed) < 20){
            direction = 'R'; 
            bs_tank = r_right;
      	}else if(R.nextInt(seed) < 40){
      		direction = 'L';
      		bs_tank = r_left;
      	}else if(R.nextInt(seed)< 60){
      		direction = 'U';       
      		bs_tank = r_up;
      	}else if(R.nextInt(seed)< 80){
      			direction = 'D';       			
      			bs_tank = r_down;
      	}else {
      		is_shot = true; 	  
      	}
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -