scg/ch08/BoxParade

From FANG

Jump to: navigation, search

01 package scg.ch08;
02 
03 import java.util.ArrayList;
04 
05 import fang2.core.Game;
06 import fang2.sprites.RectangleSprite;
07 
08 /**
09  * Randomly place some number of rectangle sprites. Then move them
10  * upward at a fixed rate, looping them around off the top of the screen
11  * to the bottom.
12  */
13 public class BoxParade
14   extends Game {
15   /** The collection of RectangleSprites */
16   ArrayList<RectangleSprite> boxes;
17 
18   /**
19    * 10 randomly colored and placed rectangles on the screen
20    */
21   @Override
22   public void setup() {
23     // Make sure you initialize the collection!
24     boxes = new ArrayList<RectangleSprite>();
25 
26     for (int = 0; i != 10++i{
27       RectangleSprite curr = new RectangleSprite(0.10.1);
28       curr.setLocation(randomDouble(), randomDouble());
29       curr.setColor(randomColor());
30       addSprite(curr);
31       boxes.add(curr);
32     }
33   }
34 
35   /**
36    * Move all the rectangles upward at a fixed speed.
37    */
38   @Override
39   public void advance(double dT{
40     for (int = 0; i != boxes.size()++i{
41       boxes.get(i).translateY(-0.5 * dT);// move up
42       if (boxes.get(i).getY() 0.0{// loop around at top
43         boxes.get(i).setY(1.0);
44       }
45     }
46   }
47 }
48 
49 //Uploaded on Mon Mar 29 21:41:10 EDT 2010


Download/View scg/ch08/BoxParade.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