01 package examples.multiplayer; 02 03 import fang.*; 04 05 06 /**Demonstrates how to make a multiplayer 07 * game with input from each player. When 08 * running this game as an application, 09 * enter the name/IP address of the server, 10 * or localhost for connecting to this 11 * current computer. 12 * @author Jam Jenkins 13 */ 14 public class Multiplayer extends GameLoop 15 { 16 /**size of the players*/ 17 private static final double SIZE=0.2; 18 /**the array of player sprites*/ 19 private StringSprite[] numbers; 20 21 /**makes the player sprites. This 22 * method can only be called once 23 * startGame has been called because 24 * all players must connect before 25 * knowing how large to make the array. 26 */ 27 public void makeSprites() 28 { 29 numbers=new StringSprite[getNumberOfPlayers()]; 30 for(int i=0; i<numbers.length; i++) 31 { 32 numbers[i]=new StringSprite("Book "+i); 33 numbers[i].setScale(SIZE); 34 } 35 } 36 37 /**adds the player sprites to the canvas*/ 38 public void addSprites() 39 { 40 for(StringSprite sprite: numbers) 41 canvas.addSprite(sprite); 42 } 43 44 /**this method is called once all 45 * players have joined the game*/ 46 public void startGame() 47 { 48 makeSprites(); 49 addSprites(); 50 setHelp("resources/MultiplayerHelp.txt"); 51 } 52 53 /**positions each player where his or 54 * her mouse is 55 * @see fang.FrameAdvancer#advanceFrame()*/ 56 public void advanceFrame(double timeAdvanced) 57 { 58 for(int i=0; i<numbers.length; i++) 59 numbers[i].setLocation(getPlayer(i).getMouse().getLocation()); 60 } 61 62 public static void main(String[] argv) 63 { 64 Multiplayer player=new Multiplayer(); 65 player.players=2; 66 player.playersSelectable=true; 67 player.serverSelectable=true; 68 player.runAsApplication(); 69 } 70 } |