《java游戏设计》范例源代码及程序执行结果

源代码在线查看: gameanimation.java

软件大小: 2216 K
上传用户: yhw002
关键词: java 范例 源代码 程序
下载地址: 免注册下载 普通下载 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;   //Applet高度
				      this.AppletHeight = AppletHeight;  //Applet宽度
				      
				      X       = AppletWidth / 2;     //起始X坐标
				      Y       = 0;     //起始Y坐标
				      VX      = -4;     //X轴速度
				      VY      = -3;     //Y轴速度
				      width   = 30;    //Sprite高度
				      height  = 30;    //Sprite宽度
				      visible = true;  //可见
				   }
				
				   public void updateState(Sprite2 s)    //转换Sprite状态的方法
				   {  
				      X = X + VX;      //移动X轴方向
				      Y = Y + VY;      //移动Y轴方向
				
				      //碰撞侦测,若Sprite1和Sprite2相撞的话则改变速度为反方向
				      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-else判断式用来设定Sprite1的边界动作
				      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)   //绘制Sprite本身的方法
				   {
				      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;   //Applet高度
				      this.AppletHeight = AppletHeight;  //Applet宽度
				      
				      //设定Sprite2的位置、速度与大小
				      X       = AppletWidth - width;  
				      Y       = AppletHeight - height; 
				      VX      = 5;
				      VY      = 2;
				      width   = 60;
				      height  = 60;
				      visible = true;
				   }
				
				   public void updateState(Sprite1 s)    //转换Sprite状态的方法
				   {
				      X = X + VX;
				      Y = Y + VY;
				
				      //下面的if-else判断式用来设定Sprite的边界动作
				      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)   //绘制Sprite本身的方法
				   {
				      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;           //Applet的宽度
				      AppletHeight  = getSize().height;          //Applet的高度
				
				      a = new Sprite1(AppletWidth,AppletHeight); //建立Sprite1
				      b = new Sprite2(AppletWidth,AppletHeight); //建立Sprite2
				      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()                    //start()方法  
				   {
				      newThread = new Thread(this);       //建立与启动新线程
				      newThread.start();
				   }
				
				   public void stop()                     //stop()方法
				   {
				      newThread = null;                   //将线程设为null
				   }
				
				   public void paint(Graphics g)
				   {
				      drawOffScreen.drawImage(bk,0,0,AppletWidth,AppletHeight,this);
				
				      a.paintSprite(drawOffScreen, this);       //将Sprite1绘制在次画面中
				      b.paintSprite(drawOffScreen, this);       //将Sprite1绘制在次画面中
				
				      //将次画面贴到主画面上
				      g.drawImage(OffScreen,0,0,this); 
				   }
				
				   public void update(Graphics g)         //update()方法
				   {
				      paint(g);                           //只单纯调用paint()方法
				   }
				
				   public void run()
				   {
				      while(newThread != null)
				      {
				         repaint();                       //重绘图像
				
				         try
				         {
				            Thread.sleep(80);             //暂停80毫秒
				         }
				         catch(InterruptedException E){ }
				
				         a.updateState(b);                //更换Sprite状态
				         b.updateState(a);
				      }
				   }
				}
							

相关资源