Java2 game programming study notes (10-2)

   10.2.3 category expansion Actor2D  
  In order to create a realistic Actor2D vivid demonstration of procedures need to create three categories: Actor2D definition of the role of the sub-class, loading definition and image frame animation style ActorGroup2D object, all bringing together all the main events and drive cycle The Applet class.    Therefore, after a short stay, next to a user to control the robot moving animation. 
  1.Robot category 
  Robot this category inherited from the Actor2D category, which defines an east, south, west and north direction of the four robots Amid this four direction, it can also use its weapons fire.    Applet main categories can be defined and dealt with control mechanisms.    In this category can only acceptable definition of robot behavior. 
  Below check the Robot category code, if it is confusing, it can turn around its reference to the father of finding out the circumstances surrounding the source code. 
  Import java.awt .*; 
  / / Create a designated according to the direction of movement and the use of weapons can be fired at a simple robot 
  Public class Robot extends Actor2D ( 
   / / And the current animation associated Index  
   Protected int currAnimIndex;  
   / / Animation design for the front of the preservation animation  
   Protected int prevAnimIndex;  
   / / Robot opened fire on the state  
   Public final static int SHOOTING = 8;  
   / / Used to tell the direction mobile robot  
   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;  
   / / Used to be the role of the creation of a new group 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 (); 
  ) 
  / / Let robot firing method is called until stopShooting 
  Public void startShooting () ( 
  PrevAnimIndex = currAnimIndex; 
  If (currAnimIndex% 2 == 0) ( 
  CurrAnimIndex + +; 
  ) 
  CurrAnimation = group.getAnimationStrip (currAnimIndex); 
  CurrAnimation.reset (); 
  SetState (SHOOTING); 
  ) 
  / / Stop shooting and return to the screen before shooting 
  Public void stopShooting () ( 
  CurrAnimIndex = prevAnimIndex; 
  CurrAnimation = group.getAnimationStrip (currAnimIndex); 
  CurrAnimation.reset (); 
  ResetState (SHOOTING); 
  ) 
  / / Designated under the direction of, let robot to move. 
  Public void move (int dir) ( 
  / / Prevent excessive shooting 
  ResetState (SHOOTING); 
  Switch (dir) ( 
  Case DIR_NORTH: 
  If (currAnimIndex! = RobotGroup.WALKING_NORTH) ( 
  PrevAnimIndex = currAnimIndex; 
  Group.getAnimationStrip currAnimation = ( 
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; 
  Group.getAnimationStrip currAnimation = ( 
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; 
  Group.getAnimationStrip currAnimation = ( 
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; 
  Group.getAnimationStrip currAnimation = ( 
RobotGroup.WALKING_EAST);
  CurrAnimIndex = RobotGroup.WALKING_EAST; 
  CurrAnimation.reset (); 
  Else () 
  Animate (); 
  Pos.translate (vel.getX (), 0); 
  ) 
  Break; 
  Default: 
  Break; 
  ) 
  ) 
  ) / / Robot 
  Robot category, it should be noted that static final SHOOTING attribute the use to populate it here 8 (2 3-th power), so that it will not interfere with or Actor2D coverage of the state variables.    If you need a refresher in how to conduct operations, you can see Chapter 2 
  Can now continue with the second step: creating a RobotGroup category.    This class of the eight animation series loaded, one walk in all directions, is a fire in all directions.    Robot category then it can be under the direction of the face of the current choice of animation.    If readers have greater interest in this chapter can look at the practice, a practice that in a cycle of demand in all the four loading animations come here only for the purpose of presentation in the cycle have not done so . 
  Import java.applet .*; 
  Public class RobotGroup extends ActorGroup2D ( 
  / / Pre-defined animation sequence 
  Public static final int WALKING_NORTH = 0; 
  Public static final int SHOOTING_NORTH = 1; 
  Public static final int WALKING_SOUTH = 2; 
  Public static final int SHOOTING_SOUTH = 3; 
  Public static final int WALKING_EAST = 4; 
  Public static final int SHOOTING_EAST = 5; 
  Public static final int WALKING_WEST = 6; 
  Public static final int SHOOTING_WEST = 7; 
  / / Create a new object RobotGroup 
  Public RobotGroup () ( 
  Super (); 
  Animations = new AnimationStrip [8]; 
  ) 
  / / Initialization eight animation sequence 
  Public void init (Applet a) ( 
  ImageLoader loader; 
  Int i; 
  / / North 
  Loader = new ImageLoader (a, "robot_north.gif", true); 
  Animations [WALKING_NORTH] = new AnimationStrip (); 
  For (i = 0; i <4; i + +) ( 
  Animations [WALKING_NORTH]. AddFrame ( 
  Loader.extractCell ((i * 72) + (i +1), 1,72,80)); 
  ) 
  Animations [WALKING_NORTH]. SetAnimator (new Animator.Looped ()); 
  Animations [SHOOTING_NORTH] = new AnimationStrip (); 
  For (i = 0; i <2; i + +) ( 
  Animations [SHOOTING_NORTH]. AddFrame ( 
  Loader.extractCell ((i * 72) + (i +1), 82,72,80)); 
  ) 
  Animations [SHOOTING_NORTH]. SetAnimator (new Animator.Looped ()); 
  / / South 
  Loader = new ImageLoader (a, "robot_south.gif", true); 
  Animations [WALKING_SOUTH] = new AnimationStrip (); 
  For (i = 0; i <4; i + +) ( 
  Animations [WALKING_SOUTH]. AddFrame ( 
  Loader.extractCell ((i * 72) + (i +1), 1,72,80)); 
  ) 
  Animations [WALKING_SOUTH]. SetAnimator (new Animator.Looped ()); 
  Animations [SHOOTING_SOUTH] = new AnimationStrip (); 
  For (i = 0; i <2; i + +) ( 
  Animations [SHOOTING_SOUTH]. AddFrame ( 
  Loader.extractCell ((i * 72) + (i +1), 82,72,80)); 
  ) 
  Animations [SHOOTING_SOUTH]. SetAnimator (new Animator.Looped ()); 
  / / East 
  Loader = new ImageLoader (a, "robot_east.gif", true); 
  Animations [WALKING_EAST] = new AnimationStrip (); 
  For (i = 0; i <4; i + +) ( 
  Animations [WALKING_EAST]. AddFrame ( 
  Loader.extractCell ((i * 72) + (i +1), 1,72,80)); 
  ) 
  Animations [WALKING_EAST]. SetAnimator (new Animator.Looped ()); 
  Animations [SHOOTING_EAST] = new AnimationStrip (); 
  For (i = 0; i <2; i + +) ( 
  Animations [SHOOTING_EAST]. AddFrame ( 
  Loader.extractCell ((i * 72) + (i +1), 82,72,80)); 
  ) 
  Animations [SHOOTING_EAST]. SetAnimator (new Animator.Looped ()); 
  / / West 
  Loader = new ImageLoader (a, "robot_west.gif", true); 
  Animations [WALKING_WEST] = new AnimationStrip (); 
  For (i = 0; i <4; i + +) ( 
  Animations [WALKING_WEST]. AddFrame ( 
  Loader.extractCell ((i * 72) + (i +1), 1,72,80)); 
  ) 
  Animations [WALKING_WEST]. SetAnimator (new Animator.Looped ()); 
  Animations [SHOOTING_WEST] = new AnimationStrip (); 
  For (i = 0; i <2; i + +) ( 
  Animations [SHOOTING_WEST]. AddFrame ( 
  Loader.extractCell ((i * 72) + (i +1), 82,72,80)); 
  ) 
  Animations [SHOOTING_WEST]. SetAnimator (new Animator.Looped ()); 
  ) 
  ) 
  The final task is to create a robot Applet to accommodate cycle time and drive.    ActorTest before in the category by category Zuopudian RobotAdater, RobotAdapter category will monitor key is pressed and release time.    The book provides an external view of a place in the robot moves are dealing with a relatively good method, this applet adapter can be registered so that we can deal with this incident and the keyboard mobile robot.    Here also joined the creation of a section of the tiled background rendering code. 
  Import java.applet .*; 
  Import java.awt .*; 
  Import java.awt.event .*; 
  Import java.awt.image .*; 
  Import java.awt.geom .*; 
  Import java.util .*; 
  / / Control Robot Object Adapter category 
  (Class RobotAdapter extends KeyAdapter 
  Private Robot robot; 
  Public RobotAdapter (Robot r) ( 
  Robot = r; 
  ) 
  / / Let fire or mobile robot 
  Public void keyPressed (KeyEvent e) ( 
  Robot.resetState (Robot.SHOOTING); 
  Switch (e.getKeyCode ()) ( 
  Case KeyEvent.VK_SPACE: 
  Robot.startShooting (); 
  Break; 
  Case KeyEvent.VK_UP: 
  Robot.move (Robot.DIR_NORTH); 
  Break; 
  Case KeyEvent.VK_DOWN: 
  Robot.move (Robot.DIR_SOUTH); 
  Break; 
  Case KeyEvent.VK_LEFT: 
  Robot.move (Robot.DIR_WEST); 
  Break; 
  Case KeyEvent.VK_RIGHT: 
  Robot.move (Robot.DIR_EAST); 
  Break; 
  Default: 
  Break; 
  ) 
  ) 
  / / If SPACEBAR loosened, allowing the robot to stop shooting 
  Public void keyReleased (KeyEvent e) ( 
  If (e.getKeyCode () == KeyEvent.VK_SPACE) ( 
  Robot.stopShooting (); 
  ) 
  ) 
  ) / / RobotAdapter 
  Below are ActorTest: 
  Import java.applet .*; 
  Import java.awt.event .*; 
  Import java.awt .*; 
  Import java.awt.image .*; 
  Import java.awt.geom .*; 
  Import java.util .*; 
  Public class ActorTest 
  Extends Applet implements Runnable ( 
  / / Animation thread 
  Private Thread animation; 
  / / Buffer screen, drawing 
  / / Moran: This category is created in Chapter 9, you need to copy it from the class document 
  Private BufferedGraphics offscreen; 
  / / Draw tiled background Paint 
  Paint paint; 
  / / Fill the background geometry 
  Private Rectangle2D floor; 
  / / The robot can move 
  Private Robot robot; 
  Public void init () ( 
  / / Create RobotGroup 
  RobotGroup group = new RobotGroup (); 
  Group.init (this); 
  / / Set Robot form the border for border 
  Group.MIN_X_POS = 0; 
  Group.MIN_Y_POS = 0; 
  Group.MAX_X_POS = getSize (). Width; 
  Group.MAX_Y_POS = getSize (). Height; 
  / / Create a screen in the middle Robot 
  Robot = new Robot (group); 
  Robot.setPos ((getSize (). Width - robot.getWidth ()) / 2, 
  (GetSize (). Height - robot.getHeight ()) / 2); 
  / / Registration of a new Mobile Robot RobotAdapter to receive instructions 
  AddKeyListener (new RobotAdapter (robot)); 
  / / Create background Paint 
  CreatePaint (); 
  Offscreen = new BufferedGraphics (this); 
  AnimationStrip.observer = this; 
  Animation = new Thread (this); 
  ) / / Init 
  / / Create a tiled background Paint 
  Private void createPaint () ( 
  Image image = getImage (getDocumentBase (), "stile.gif"); 
  While (image.getWidth (this) <= 0); 
  / / Use of the image width and high creation of a new BufferedImage 
  BufferedImage bi = new BufferedImage ( 
  Image.getWidth (this), 
  Image.getHeight (this), 
BufferedImage.TYPE_INT_RGB);
  / / Get the Graphics2D BufferedImage containers and the mapping of the original image 
  ((Graphics2D) bi.getGraphics ()). DrawImage (image, new AffineTransform (), this); 
  / / Size of the image to create the light rectangular 
  New Rectangle2D.Double floor = (0,0, getSize (). Width, getSize (). Height); 
  / / Set the paint 
  Paint = new TexturPaint (bi, new Rectangle ( 
  0,0, image.getWidth (this), image.getHeight (this))); 
  ) 
  Public void start () ( 
  / / Start animation thread 
  Animation.start (); 
  ) 
  Public void stop () ( 
  Animation = null; 
  ) 
  Public void run () ( 
  Thread t = Thread.currentThread (); 
  While (t == animation) ( 
  Try ( 
  Thread.sleep (10); 
  ) Catch (Exception e) ( 
  E.printStackTrace (); 
  Break; 
  ) 
  Repaint (); 
  ) 
  ) / / Run 
  Public void update (Graphics g) ( 
  Robot.update (); 
  Print (g); 
  ) 
  Public void paint (Graphics g) ( 
  Graphics2D bg = (Graphics2D) offscreen.getValidGraphics (); 
  / / Set the background filled with paint and 
  Bg.setPaint (paint); 
  Bg.fill (floor); 
  / / Mapping robot 
  Robot.paint (bg); 
  / / Draw on the screen in the form of images 
  G.drawImage (offscreen.getBuffer (), 0,0, this); 
  ) / / Paint 
  ) / / ActorTest 
  ActorTest applet to run their own to see Robot class in the end that we can do. 
  Before the end of this chapter, then Actor2D of rapid expansion for StaticActor category. 
  2.StaticActor category 
  Readers may have thought in the game do not realize or other background objects campaign, this need not be a waste of time to achieve the static objects.    Actor2D consider expanding category for StaticActor category. 
  StaticActorGroup category and compared StaticActor category will contain a single frame animation of objects to provide a rapid containers.    Here is the code, readers can create their own testing procedures. 
  Import java.awt .*; 
  Public class StaticActor extends Actor2D ( 
  Public StaticActor (ActorGroup2D grp) ( 
  Super (grp); 
  / / 0 at the first of animation 
  Group.getAnimationStrip currAnimation = (0); 
  FrameWidth = currAnimation.getFrameWidth (); 
  FrameHeight = currAnimation.getFrameHeight (); 
  ) 
  ) 
  These two categories quite simple, they describe a single-frame animation and scene of a mobile capability and rotating objects.    Add in the game in static images, the use of these classes are very convenient.    On this topic in Chapter 11 can see more content. 
  Note that we do not need to StaticActor derived class and subclass StaticActorGroup - only need to use the correct parameters of their examples.    If the need for any specific transformation or other updates can be derived subclass, or applet to create category called its methodology. 
  10.3 Aggregate 
  Of this chapter as not to create Java games on the use of the official papers entities, but these are just used to quickly and easily create an official papers games entities, but these are just used to quickly and easily create a way of the game.    Some of the code has not been optimized, because in this way the idea clearer.    According readers may need to make any changes to the code, add or delete.    Game programming is a great challenge to improve the existing code for faster, more precise and better code. 
  Here spent only a few minutes to get a ActorTest applet and run it up and running version.    The background of all the major work by Actor2D category and class provides support for custom object definition of specific acts, in fact, already become an easy task.    This chapter provides a lot of building material, and readers may need to re-read through once again to ensure that all the content in their minds to fully grasp.    If that mastered the content of what can be derived subclass, or applet to create category called its methodology. 
  10.4 practice 
  Animator revised 10.1 category, the use of objects rather than get ListIterator methods.    For all known Animator subclass, whether ListIterator than get a good method?    For contain a lot of animation frames list, use ListIterator targeted approach than get savings time? 
  10.2RobotGroup class init method it in a single cycle of loading all four groups animation sequence.    Readers may need to file name as a String object storage array, all this can be another implementation of the law. 
  10.3 creation of a synchronized Robot object, at the beginning of each screen in a different location each Robot should have its own monitor button RobotAdapter object, the two robots should be depending on the button response and also a synchronized manner Mobile, but at different screen locations. 
  10.4 a better practice, the creation of a applet, consists of two robots, a controlled RobotAdapter control, and the other Robot from a separate control mechanism to control the computer, add bullets and some artificial intelligence, will have a one pairs a robot fighting game.    This practice can also free play, look at what we can do and how. 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply