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

📄 robot.java

📁 java learn PPT java learn PPT java learn PPT java learn PPT
💻 JAVA
字号:
     import java.awt.*;

     // 创建一个可以按照指定的方向移动并可以使用武器开火的简单机器人
     public class Robot extends Actor2D 
     {
          // 和当前的动画关联的索引
          protected int currAnimIndex;

          // 为设计动画保存前面的动画
          protected int prevAnimIndex;

          // Robot的开火状态
          public final static int SHOOTING = 8;

          // 用来告诉机器人朝哪个方向移动 
          public final static int DIR_NORTH = 0;
          public final static int DIR_SOUTH = 1;
          public final static int DIR_EAST  = 2;
          public final static int DIR_WEST  = 3;

          // 用给定的角色组创建一个新的Robot
          public Robot(ActorGroup2D grp)
          {
               super(grp);

               vel.setX(5);
               vel.setY(5);

               animWait = 3;

               currAnimIndex = 0;
               prevAnimIndex = 0;

               currAnimation = group.getAnimationStrip(RobotGroup.WALKING_SOUTH);
               
               frameWidth  = currAnimation.getFrameWidth();
               frameHeight = currAnimation.getFrameHeight();
          }

          // 更新机器人的位置,如果它在射击的话让它动起来
          public void update()
          {
               if(hasState(SHOOTING))
               {
                    animate();
               }

               xform.setToTranslation(pos.getX(), pos.getY()); 

               updateBounds();
               checkBounds();
          }

          // 让机器人射击直到stopShooting方法被调用   
          public void startShooting()
          {
               prevAnimIndex = currAnimIndex;
               if((currAnimIndex % 2) == 0) 
               {
                    currAnimIndex++;
               }
               currAnimation = group.getAnimationStrip(currAnimIndex);
               currAnimation.reset(); 
               setState(SHOOTING);
          }

          // 停止射击并回到射击前的画面 
          public void stopShooting()
          {
               currAnimIndex = prevAnimIndex;
               currAnimation = group.getAnimationStrip(currAnimIndex);
               currAnimation.reset(); 
               resetState(SHOOTING);
          }

          // 根据指定的方向,让机器人动起来
          public void move(int dir)
          {
               // 防止过多的射击
               resetState(SHOOTING);

               switch(dir)
               {
                    case DIR_NORTH:
                         if(currAnimIndex != RobotGroup.WALKING_NORTH)
                         {
                              prevAnimIndex = currAnimIndex;
                              currAnimation = group.getAnimationStrip(RobotGroup.WALKING_NORTH);
                              currAnimIndex = RobotGroup.WALKING_NORTH;
                              currAnimation.reset(); 
                         }
                         else
                         {
                              animate();
                              pos.translate(0, -vel.getY());
                         }
                         break;

                    case DIR_SOUTH:
                         if(currAnimIndex != RobotGroup.WALKING_SOUTH)
                         {
                              prevAnimIndex = currAnimIndex;
                              currAnimation = group.getAnimationStrip(RobotGroup.WALKING_SOUTH);
                              currAnimIndex = RobotGroup.WALKING_SOUTH;
                              currAnimation.reset(); 
                         }
                         else
                         {
                              animate();
                              pos.translate(0, vel.getY());
                         }
                         break;

                    case DIR_WEST:
                         if(currAnimIndex != RobotGroup.WALKING_WEST)
                         {
                              prevAnimIndex = currAnimIndex;
                              currAnimation = group.getAnimationStrip(RobotGroup.WALKING_WEST);
                              currAnimIndex = RobotGroup.WALKING_WEST;
                              currAnimation.reset(); 
                         }
                         else
                         {
                              animate();
                              pos.translate(-vel.getX(), 0);
                         }
                         break;

                    case DIR_EAST:
                         if(currAnimIndex != RobotGroup.WALKING_EAST)
                         {
                              prevAnimIndex = currAnimIndex;
                              currAnimation = group.getAnimationStrip(RobotGroup.WALKING_EAST);
                              currAnimIndex = RobotGroup.WALKING_EAST;
                              currAnimation.reset(); 
                         }
                         else
                         {
                              animate();
                              pos.translate(vel.getX(), 0);
                         }
                         break;

                    default:
                         break;
               } 
          }

     }    // Robot      

⌨️ 快捷键说明

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