scg/ch09/Caterpillar

From FANG

Jump to: navigation, search

01 package scg.ch09;
02 
03 import fang2.core.*;
04 import fang2.sprites.*;
05 import fang2.transformers.*;
06 
07 /**
08  * This is a shell of a game with a caterpillar that follows the mouse.
09  *
10  @author  Jam Jenkins
11  */
12 public class Caterpillar
13   extends Game {
14   /** an invisible sprite the caterpillar's head follows */
15   private Sprite follow;
16 
17   /**
18    * makes the follow sprite locate where the mouse is
19    */
20   @Override
21   public void advance() {
22     follow.setLocation(getMouse2D());
23   }
24 
25   /**
26    * makes and adds the caterpillar and invisible sprite
27    */
28   @Override
29   public void setup() {
30     // follow is invisible because it is not added
31     follow = new OvalSprite(0.010.01);
32     follow.setColor(getColor("Yellow"));
33     follow.setLocation(0.10.1);
34 
35     // the first segment tracks follow
36     Sprite previous = follow;
37     // make 10 segments each following each other
38     for (int = 0; k < 10; k++{
39       Sprite segment = new OvalSprite(0.10.1);
40       segment.setLocation(0.5 + (0.05 * k)0.5);
41       segment.setColor(getColor("Green"));
42       AttractTransformer attractor = new AttractTransformer(0.2);
43       attractor.setTarget(previous);
44       // make sure the segments don't get too close
45       attractor.setMinDistance(0.05);
46       segment.addTransformer(attractor);
47       addSprite(segment);
48       // the current segment should be followed next
49       previous = segment;
50     }
51   }
52 }
53 
54 //Uploaded on Mon Mar 29 21:41:25 EDT 2010


Download/View scg/ch09/Caterpillar.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games