scg/ch06/PongPaddle

From FANG

Jump to: navigation, search

01 package scg.ch06;
02 
03 import fang2.core.Game;
04 import fang2.sprites.RectangleSprite;
05 
06 /**
07  * The PongPaddle, a paddle for playing pong. This class uses {@link
08  * fang2.core.Game#getCurrentGame() Game.getCurrentGame} to get the current
09  * game so that it can provide access to the player's key presses.
10  */
11 public class PongPaddle
12   extends RectangleSprite {
13   /**
14    * Velocity, in screens/second, with which the paddle moves in the x
15    * and y directions; the velocity only applies when the appropriate
16    * arrow keys are pressed.
17    */
18   double deltaX;
19   double deltaY;
20 
21   /**
22    * Construct a new PongPaddle instance. The size and initial velocity
23    * are specified as parameters to the constructor.
24    *
25    @param  width   width, in screens, of the paddle
26    @param  height  height, in screens, of the paddle
27    @param  deltaX  horizontal velocity, in screens per second
28    @param  deltaY  vertical velocity, in screens per second
29    */
30   public PongPaddle(double width, double height, double deltaX,
31     double deltaY{
32     super(width, height);
33     this.deltaX = deltaX;// scope hole
34     this.deltaY = deltaY;// scope hole
35   }
36 
37   /**
38    * Advance the paddle one frame. The paddle will move according to its
39    * velocity (as set in the constructor or {@link #setVelocity}if the
40    * appropriate arrow key is pressed. That is, if the up arrow is
41    * pressed, the paddle will move with the negative {@code deltaY}
42    * velocity; if the down arrow is pressed, it will move with the
43    * positive {@code deltaY}.
44    *
45    @param  deltaT  time elapsed since last call to advance
46    */
47   public void advance(double deltaT{
48     if (Game.getCurrentGame().upPressed()) {
49       translateY(-deltaY * deltaT);
50     }
51     if (Game.getCurrentGame().downPressed()) {
52       translateY(deltaY * deltaT);
53     }
54     if (Game.getCurrentGame().leftPressed()) {
55       translateX(-deltaX * deltaT);
56     }
57     if (Game.getCurrentGame().rightPressed()) {
58       translateX(deltaX * deltaT);
59     }
60     bounceOffEdges();
61   }
62 
63   /**
64    * "Bounce" the paddle off of the edges. While there might be
65    * reasonable gameplay in actually bouncing the paddle, in this game
66    * all that happens is that if the paddle moves off the screen in any
67    * direction, it is pushed back onto the screen. The push is done by
68    * moving the location the minimal amount to put all of the paddle on
69    * the screen.
70    */
71   public void bounceOffEdges() {
72     if (getMinY() 0.0{
73       translateY(0.0 - getMinY());
74     }
75     if (getMaxY() 1.0{
76       translateY(1.0 - getMaxY());
77     }
78     if (getMinX() 0.0{
79       translateX(0.0 - getMinX());
80     }
81     if (getMaxX() 1.0{
82       translateX(1.0 - getMaxX());
83     }
84   }
85 
86   /**
87    Set the current velocity of the paddle
88    *
89    @param  deltaX  the new horizontal velocity
90    @param  deltaY  the new vertical velocity
91    */
92   public void setVelocity(double deltaX, double deltaY{
93     this.deltaX = deltaX;
94     this.deltaY = deltaY;
95   }
96 }
97 
98 //Uploaded on Mon Mar 29 21:41:41 EDT 2010


Download/View scg/ch06/PongPaddle.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