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

📄 maze1.java

📁 一个多线程的JAVA多线程游戏
💻 JAVA
字号:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

import javax.swing.JOptionPane;

public class Maze1 extends Applet 
             implements KeyListener, Runnable
{
   int          ImageWidth,ImageHeight,ImageX,ImageY,AppletWidth,
                AppletHeight,floorW,key;
   int sx;  
   char direction ; 
   Image        t_down,t_up,t_left,t_right,o_down,o_up,o_left,o_right,r_down,explode,
               r_up,r_left,r_right,floor,block1,block2,OffScreen,shot1,shot,bs_tank;
   Thread       newThread;
   Graphics     drawOffScreen; 
   MediaTracker MT;
    
   boolean is_shot = false;
   Sprite s;
   BulletSprite b ;
   BadSprite bad_tank4,bad_tank1,bad_tank2,bad_tank3;
 
   int[][] maze = { 
   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
   {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
   {2,0,0,0,0,0,2,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,2},
   {2,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,1,1,0,0,0,0,0,2},
   {2,0,0,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,2},
   {2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
   {2,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,2,2,0,0,2},
   {2,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,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1,1}
   };
//
   public void init()
   {
      addKeyListener(this);   

      setBackground(Color.white);  
      this.setSize(1258,790);
      AppletWidth  = getSize().width;  
      AppletHeight = getSize().height; 
      sx = 0;

      MT           = new MediaTracker(this);
      r_up = getImage(getDocumentBase(),"image/r_up.gif");
      r_left = getImage(getDocumentBase(),"image/r_left.gif");
      r_right = getImage(getDocumentBase(),"image/r_right.gif");
      r_down = getImage(getDocumentBase(),"image/r_down.gif");
      
      o_up = getImage(getDocumentBase(),"image/o_up.jpg");
      o_left = getImage(getDocumentBase(),"image/o_left.jpg");
      o_right = getImage(getDocumentBase(),"image/o_right.jpg");
      o_down = getImage(getDocumentBase(),"image/o_down.jpg");
      t_up = getImage(getDocumentBase(),"image/t_up.gif");
      t_left = getImage(getDocumentBase(),"image/t_left.gif");
      t_right = getImage(getDocumentBase(),"image/t_right.gif");
      t_down = getImage(getDocumentBase(),"image/t_down.gif");
      
      explode = getImage(getDocumentBase(),"image/explode.gif");
      
      floor = getImage(getDocumentBase(),"image/FLOOR.jpg"); 
      block1 = getImage(getDocumentBase(),"image/quan.JPG");
      block2 = getImage(getDocumentBase(),"image/quan3.JPG");
      
      shot = getImage(getDocumentBase(),"image/shot.gif");
      shot1 = getImage(getDocumentBase(),"image/shot1.gif");
      MT.addImage(r_up,0);
      MT.addImage(r_down,0);
      MT.addImage(r_left,0);
      MT.addImage(r_right,0);
      MT.addImage(o_up,0);
      MT.addImage(o_down,0);
      MT.addImage(o_left,0);
      MT.addImage(o_right,0);
      MT.addImage(t_up,0);
      MT.addImage(t_down,0);
      MT.addImage(t_left,0);
      MT.addImage(t_right,0);
      MT.addImage(floor,0);
      MT.addImage(block1,0);
      MT.addImage(block2,0);
     
      try
      {
         showStatus("图像载入中(Loading Images)...");
         MT.waitForAll();
      }
      catch(InterruptedException E){ } 
      
      OffScreen     = createImage(AppletWidth,AppletHeight);
      drawOffScreen = OffScreen.getGraphics();

      floorW = floor.getWidth(this);
      ImageWidth    = floorW;   
      ImageHeight   = floorW;    
      ImageX        = floorW;  
      ImageY        = floorW; 
      bs_tank = r_up;
      direction = 'U';


      b = new BulletSprite(shot,this,maze);
      s = new Sprite(ImageX*17, ImageY*8, ImageWidth, ImageHeight,8,17,maze);
      bad_tank4 = new BadSprite(o_up,o_left,o_down,o_right,shot,explode,maze,1,20,s,b,this);
      bad_tank1 = new BadSprite(t_up,t_left,t_down,t_right,shot,explode,maze,33,1,s,b,this);
      bad_tank2 = new BadSprite(o_up,o_left,o_down,o_right,shot,explode,maze,1,1,s,b,this);
      bad_tank3 = new BadSprite(t_up,t_left,t_down,t_right,shot,explode,maze,33,20,s,b,this);
   }

   public void start()
   {
      newThread = new Thread(this);
      newThread.start();
	  
   }

   public void stop()
   {
      newThread = null;
   }

   public void update(Graphics g)    
   {
      paint(g);              
   }

   public void paint(Graphics g)
   {
      g.drawImage(OffScreen,0,0,this);
      b.paintSprite(g);
      bad_tank1.draw_btank(g);
      bad_tank2.draw_btank(g);
      bad_tank3.draw_btank(g);
      bad_tank4.draw_btank(g);
   }

    public void run()
    {
        while(true)
        {  
           drawOffScreen.clearRect(0,0,AppletWidth,AppletHeight);

           for(int i=0; i <= AppletHeight; i+= floorW)
              for(int j=0; j <= AppletWidth; j+=floorW)
                 drawOffScreen.drawImage(floor,j,i,j+floorW,i+floorW,
                     0,0,floorW,floorW,this);

           for(int i=0; i < maze.length ; i++)
              for(int j=0; j < maze[i].length; j++)
                  if(maze[i][j] == 1) 
                     drawOffScreen.drawImage(block1,j*floorW,i*floorW, this); 
                  else if(maze[i][j] == 2) 
                     drawOffScreen.drawImage(block2,j*floorW,i*floorW, this);
        

           draw_tank(bs_tank);
           b.updateState();
           if(is_shot && b.canVisible() == false && b.canMove() == false )
               	      {
        	             b.setdirection(direction);
        	             b.setVisible(true);
               	         b.setMove(true);
        	             b.setLocation(s.LcX + (ImageWidth / 2),s.LcY+(ImageWidth / 2));              	         
               	      }

           is_shot = false;
           
           repaint();
           sx += s.SizeW;
           if(sx > s.SizeW*2)
              sx = 0;

         try {
             Thread.sleep(160);
          }
           catch (InterruptedException e)
          {

          }
        }
    }
	public void draw_tank(Image img) {		
		if(bad_tank1.is_rtank_live && bad_tank2.is_rtank_live && bad_tank3.is_rtank_live && bad_tank4.is_rtank_live){
		drawOffScreen.drawImage(img,s.LcX,s.LcY, this);
		}else {
			drawOffScreen.drawImage(explode,s.LcX,s.LcY, this);
			drawOffScreen.drawImage(explode,s.LcX,s.LcY, this);
			try {
	             Thread.sleep(500);
	          }catch (InterruptedException e){}
			drawOffScreen.drawImage(explode,s.LcX,s.LcY, this);
			drawOffScreen.drawImage(explode,s.LcX,s.LcY, this);
			JOptionPane.showMessageDialog(null, "游戏结束,88!", "提示信息",
	                JOptionPane.YES_OPTION);
            System.exit(0);		
		}
		if(!bad_tank1.is_live &&! bad_tank2.is_live && !bad_tank3.is_live && !bad_tank4.is_live){
			
		JOptionPane.showMessageDialog(null, "恭喜你!过关了,你成为了芳氏坦克的第一人!!!!", "提示信息",
                JOptionPane.YES_OPTION);
        System.exit(0);
		}
	}
   public void keyTyped(KeyEvent e)  { }

   public void keyPressed(KeyEvent e)
   {
      key = e.getKeyCode();
   
      if(key == KeyEvent.VK_RIGHT) 
      {
          direction = 'R'; 
          s.moveRight(); 
          bs_tank = r_right;
      }
      else if(key == KeyEvent.VK_LEFT)
      {
          direction = 'L';
          s.moveLeft();
          bs_tank = r_left;
      }
      else if(key == KeyEvent.VK_UP)
      {
         direction = 'U';       
         s.moveUp();
         bs_tank = r_up;
      }
      else if(key == KeyEvent.VK_DOWN) 
      {
         direction = 'D';   
         
         s.moveDown();

         bs_tank = r_down;
      }
      
      else if(key == KeyEvent.VK_SPACE ) 
      {
    	 is_shot = true;
    	  
    }
  }
      

   public void keyReleased(KeyEvent e) {}
}

⌨️ 快捷键说明

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