scg/ch01/spacesprites/ScoreSprite

From FANG

Jump to: navigation, search

01 package scg.ch01.spacesprites;
02 
03 import java.awt.Color;
04 
05 import fang2.core.Game;
06 import fang2.sprites.StringSprite;
07 
08 /**
09  * Keep and display score. The {@link ScoreSprite} is based on the
10  * version presented in Chapter09. The score in this version is an
11  * integer.
12  */
13 public class ScoreSprite
14   extends StringSprite {
15   /** scale of the object on the screen */
16   public final static double DEFAULT_SCORE_SCALE = 0.1;
17   /** color to use for the score */
18   public final static Color DEFAULT_SCORE_COLOR 
19     = Game.getCurrentGame().getColor("yellow");
20   
21   /** current score */
22   private int score;
23 
24   /**
25    * Default constructor: 0 is initial score.
26    */
27   public ScoreSprite() {
28     this(0);
29   }
30 
31   /**
32    * Create new ScoreSprite with the given score.
33    *
34    @param  score  initial score
35    */
36   public ScoreSprite(int score{
37     setScore(score);
38     setScale(DEFAULT_SCORE_SCALE);
39     setColor(DEFAULT_SCORE_COLOR);
40     setLocation(getScale()/2, getScale()/2);
41   }
42 
43   /**
44    * Implementation method: display the currently recorded score.
45    */
46   private void fixTextDisplay() {
47     setText(Integer.toString(score));
48   }
49 
50   public void decrement() {
51     decrement(1);
52   }
53 
54   /**
55    * Decrement score by n
56    *
57    @param  n  number to subtract from score.
58    */
59   public void decrement(int n{
60     increment(-n);
61   }
62 
63   /**
64    * Get the current score.
65    *
66    @return  the current score
67    */
68   public int getScore() {
69     return score;
70   }
71 
72   /**
73    * Increment the score by 1.
74    */
75   public void increment() {
76     increment(1);
77   }
78 
79   /**
80    * Increment the score by the given value.
81    *
82    @param  n  number to add to the current score
83    */
84   public void increment(int n{
85     setScore(score + n);
86   }
87 
88   public void setScore(int score{
89     Game game = Game.getCurrentGame();
90     if (!game.containsSprite(this)) game.addSprite(this);
91     this.score = score;
92     fixTextDisplay();
93   }
94 }
95 
96 //Uploaded on Mon Mar 29 21:41:40 EDT 2010


Download/View scg/ch01/spacesprites/ScoreSprite.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