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

📄 gameanimation.java

📁 华容道源程序代码。非常简单。非常实用。快来看看吧
💻 JAVA
字号:
// 程序:角色碰撞实例
// 范例文件: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);
      }
   }
}

⌨️ 快捷键说明

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