JAVA2游戏程序设计源码 摘自 源码中国 www.Cn700.com

源代码在线查看: gameanimation.java

软件大小: 2149 K
上传用户: dxhh
关键词: JAVA2 700 www com
下载地址: 免注册下载 普通下载 VIP

相关代码

				// 程序:角色碰撞实例
				// 范例文件:GameAnimation.java
				
				import java.awt.*;
				import java.applet.*;
				
				class Sprite1
				{
				   public  int     X,Y,width,height,VX,VY;
				
				   int     AppletWidth,AppletHeight;
				   boolean visible;
				   Image UFO; 
				
				   public Sprite1(int AppletWidth,int AppletHeight)
				   {
				      this.AppletWidth  = AppletWidth;  
				      this.AppletHeight = AppletHeight;  
				      
				      X       = AppletWidth / 2;  
				      Y       = 0;   
				      VX      = -4; 
				      VY      = -3;   
				      width   = 30;   
				      height  = 30;  
				      visible = true; 
				   }
				
				   public void updateState(Sprite2 s)   
				   {  
				      X = X + VX; 
				      Y = Y + VY; 
				
				
				      if((X + width >= s.X) && (Y + height >= s.Y) && 
				         (s.X + s.width >= X) && (s.Y + s.height >= Y))
				      {
				         VX   = -VX;
				         VY   = -VY;
				         s.VX = -s.VX;
				         s.VY = -s.VY;
				      }
				
				
				      if(X < 0) 
				      {
				         X  = 0;
				         VX = -VX;
				      }
				      else if(X > AppletWidth - width) 
				      {
				         X  = AppletWidth - width; 
				         VX = -VX;
				      }
				
				      if(Y < 0)
				      {
				         Y  = 0;
				         VY = -VY;
				      }
				      else if(Y > AppletHeight - height)
				      {
				         Y  = AppletHeight - height;
				         VY = -VY;
				      }
				   }
				
				   public void paintSprite(Graphics g, Applet Game)  
				   {
				      if(visible)
				         g.drawImage(UFO,X,Y,width,height,Game);
				   }
				}
				
				class Sprite2
				{
				   public  int     X,Y,width,height,VX,VY;
				
				   int     AppletWidth,AppletHeight;
				   boolean visible;
				   Image beast; 
				  
				   public Sprite2(int AppletWidth,int AppletHeight)
				   {
				      this.AppletWidth  = AppletWidth;  
				      this.AppletHeight = AppletHeight; 
				      
				
				      X       = AppletWidth - width;  
				      Y       = AppletHeight - height; 
				      VX      = 5;
				      VY      = 2;
				      width   = 60;
				      height  = 60;
				      visible = true;
				   }
				
				   public void updateState(Sprite1 s)   
				   {
				      X = X + VX;
				      Y = Y + VY;
				
				
				      if(X + width < 0) 
				      {
				         X = AppletWidth;
				      }
				      else if(X > AppletWidth) 
				      {
				         X = -width; 
				      }
				
				      if(Y < 0)
				      {
				         Y  = 0;
				         VY = -VY;
				      }
				      else if(Y > AppletHeight - height)
				      {
				         Y  = AppletHeight - height;
				         VY = -VY;
				      }
				   }
				
				   public void paintSprite(Graphics g, Applet Game) 
				   {
				      if(visible)
				         g.drawImage(beast,X,Y,width,height,Game);
				   }
				}
				
				public class GameAnimation extends Applet implements Runnable
				{
				   int      AppletWidth,AppletHeight;
				   Image    OffScreen, bk;  
				   Thread   newThread;                  
				   Graphics drawOffScreen;              
				
				   Sprite1 a;  
				   Sprite2 b;
				
				   public void init()
				   {
				      setBackground(Color.white);
				
				      AppletWidth   = getSize().width;        
				      AppletHeight  = getSize().height;       
				
				      a = new Sprite1(AppletWidth,AppletHeight); 
				      b = new Sprite2(AppletWidth,AppletHeight); 
				      a.UFO = getImage(getDocumentBase(),"Images/6.gif");
				      b.beast = getImage(getDocumentBase(),"Images/1.gif");
				
				
				      bk = getImage(getDocumentBase(),"Images/009.jpg");
				
				
				      OffScreen     = createImage(AppletWidth,AppletHeight); 
				      drawOffScreen = OffScreen.getGraphics();   
				   }
				
				   public void start()               
				   {
				      newThread = new Thread(this);    
				      newThread.start();
				   }
				
				   public void stop()              
				   {
				      newThread = null;          
				   }
				
				   public void paint(Graphics g)
				   {
				      drawOffScreen.drawImage(bk,0,0,AppletWidth,AppletHeight,this);
				
				      a.paintSprite(drawOffScreen, this);    
				      b.paintSprite(drawOffScreen, this);   
				
				
				      g.drawImage(OffScreen,0,0,this); 
				   }
				
				   public void update(Graphics g)       
				   {
				      paint(g);                       
				   }
				
				   public void run()
				   {
				      while(newThread != null)
				      {
				         repaint();              
				
				         try
				         {
				            Thread.sleep(80);     
				         }
				         catch(InterruptedException E){ }
				
				         a.updateState(b);         
				         b.updateState(a);
				      }
				   }
				}			

相关资源