examples/turnbased/TurnBased

From FANG

Jump to: navigation, search

001 package examples.turnbased;
002 
003 import fang.*;
004 
005 /**
006  * example of a turnbased game using the FANG engine
007  @author Wade and Mike
008  @author Jam Jenkins (modified due to FANG Engine updates)
009  */
010 public class TurnBased extends Game
011 {
012    /**index of player whose turn it currently is*/
013    private int turn;
014    /**indicates whose turn it is*/
015    private StringSprite turnSprite;
016    /**indicates whether the player has clicked*/
017    private StringSprite waitSprite;
018    /**indicates your player number*/
019    private StringSprite playerSprite;
020 
021    public TurnBased()
022    {
023       setNumberOfPlayers(2);
024    }
025 
026    /**
027     * creates the sprites and aligns them to the correct position
028     */
029    private void makeSprites()
030    {
031       turn = 0//defaults the turn number to player0
032 
033       turnSprite = new StringSprite("It is "+getPlayer(turn).getName()+"'s turn.");
034       turnSprite.centerJustify();
035       turnSprite.topJustify();
036       turnSprite.setLocation(.5.1);
037       turnSprite.setScale(.75);
038 
039       waitSprite = new StringSprite("waiting for "+getPlayer(turn).getName()+". . .");
040       waitSprite.centerJustify();
041       waitSprite.setLocation(.5,.5);
042       waitSprite.setScale(.75);
043 
044       playerSprite = new StringSprite(getPlayer(getID()).getName());
045       playerSprite.centerJustify();
046       playerSprite.bottomJustify();
047       playerSprite.setLocation(.5,.9);
048       playerSprite.setScale(.5);
049 
050       updateText()//update incase you are player0
051    }
052 
053    /**
054     * begins the game
055     */
056    public void setup()
057    {
058       setHelp("resources/TurnBasedHelp.txt");
059       makeSprites();
060       addSprites();
061    }
062 
063    /**
064     * adds sprites to the game
065     */
066    private void addSprites()
067    {
068       addSprite(turnSprite);
069       addSprite(waitSprite);
070       addSprite(playerSprite);
071    }
072 
073    /**
074     * advances the game one frame
075     */
076    public void advance()
077    {
078       //if the correct player has clicked their mouse
079       if(getClick2D(turn)!=null)
080       {
081          turn = (turn + 1)%getNumberOfPlayers();
082          updateText();
083       }
084    }
085 
086    /**
087     * updates the texts to display the correct information
088     */
089    private void updateText()
090    {
091       if(turn == getID()) //if its your turn
092       {
093          turnSprite.setText("It is your turn.");
094          waitSprite.setText("click to continue.");
095       }
096       else //if its someone elses turn
097       {
098          turnSprite.setText("It is "+getPlayer(turn).getName()+"'s turn.");
099          waitSprite.setText("waiting for "+getPlayer(turn).getName()+". . .");
100       }
101    }
102 
103    public static void main(String[] args)
104    {
105       TurnBased t = new TurnBased();
106       t.runAsApplication();
107    }
108 }


Download/View examples/turnbased/TurnBased.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