scg/ch07/ColoredRectanglesMP

From FANG

Jump to: navigation, search

01 package scg.ch07;
02 
03 import fang2.attributes.Location2D;
04 import fang2.core.Game;
05 import fang2.sprites.RectangleSprite;
06 
07 /**
08  * Demonstration of multi-player game techniques. Constructor sets
09  * number of required players to 2. When 2 players have joined, game can
10  * be started. Game looks for mouse clicks and draws random rectangles.
11  * Note that rectangles are the same on both screens.
12  */
13 public class ColoredRectanglesMP
14   extends Game {
15   /**
16    * Construct a new ColoredRectangleMP object; initialize the Game part
17    * and then set the player count to 2 (make it multi-player).
18    */
19   public ColoredRectanglesMP() {
20     super();
21     setNumberOfPlayers(2);
22     players = 2;
23   }
24 
25   /**
26    * Advance one frame: iterate through all players looking for mouse
27    * clicks. If there is a mouse click, create a random rectangle
28    * centered at click location.
29    */
30   @Override
31   public void advance(double dT{
32     for (int = 0; p != getNumberOfPlayers()++p{
33       Location2D rectangleLocation = getClick2D(p);
34 
35       if (rectangleLocation != null{// was mouse(p) clicked?
36         RectangleSprite rectangle = new RectangleSprite(randomDouble(),
37             randomDouble());
38         rectangle.setColor(randomColor());
39         rectangle.setLocation(rectangleLocation);
40         addSprite(rectangle);
41       }
42     }
43   }
44 }
45 
46 //Uploaded on Mon Mar 29 21:40:35 EDT 2010


Download/View scg/ch07/ColoredRectanglesMP.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