|
001 packagepackage is used to name the directory or folder a class is in scg.ch13.core;
002
003 importimport means to make the classes and/or packages available in this program java.awt.event.KeyEvent;
004
005 importimport means to make the classes and/or packages available in this program fang2.core.Game;
006 importimport means to make the classes and/or packages available in this program fang2.sprites.StringSprite;
007
008 importimport means to make the classes and/or packages available in this program scg.ch13.highscore.HighScoreLevel;
009
010 /**
011 * The {open braces start code blocks and must be matched with a close brace@link BlockDrop}close braces end code blocks and must match an earlier open brace game. Creates board, score, and next piece
012 * sprites and then just pumps the board sprite (calls its advance
013 * method).
014 */
015 @SuppressWarnings("serial")
016 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects BlockDrop
017 extendsextends means to customize or extend the functionality of a class Game {open braces start code blocks and must be matched with a close brace
018 /** delay in seconds between down movement of blocks at level 1 */
019 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) doubledouble is the type for numbers that can contain decimal fractions INITIAL_MOVE_DELAY =this assignment operator makes the left side equal to the right side 0.25;
020
021 /** number of lines to clear to go up one level */
022 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer LINES_PER_LEVEL =this assignment operator makes the left side equal to the right side 10;
023
024 /** how much does the delay speed up at each level */
025 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) doubledouble is the type for numbers that can contain decimal fractions SPEED_UP_FACTOR =this assignment operator makes the left side equal to the right side 1.05;
026
027 /** the board where the game is played rules live there */
028 privateprivate is used to restrict access to the current class only Board board;
029
030 /** display the current level */
031 privateprivate is used to restrict access to the current class only StringSprite displayLevel;
032
033 /** what is the current level? */
034 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer level;
035
036 /** countdown timer forfor is a looping structure for repeatedly executing a block of code moving the block */
037 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions moveClock;
038
039 /** the time between block moves */
040 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions moveDelay;
041
042 /** box showing the next piece */
043 privateprivate is used to restrict access to the current class only NextPieceBox nextPiece;
044
045 /** sprite keeping and showing the score */
046 privateprivate is used to restrict access to the current class only ScoreSprite score;
047
048 /**
049 * Create newnew is used to create objects by calling the constructor game starting at level 1.
050 */
051 publicpublic is used to indicate unrestricted access (any other class can have access) BlockDrop() {open braces start code blocks and must be matched with a close brace
052 thisthis means the current object (the implicit parameter)(1);
053 }close braces end code blocks and must match an earlier open brace
054
055 /**
056 * Create newnew is used to create objects by calling the constructor game starting at the given level
057 *
058 * @paramthis is the Javadoc tag for documenting the purpose of parameters startLevel level number to start at.
059 */
060 publicpublic is used to indicate unrestricted access (any other class can have access) BlockDrop(intint is the type for whole numbers and it is short for integer startLevel) {open braces start code blocks and must be matched with a close brace
061 moveDelay =this assignment operator makes the left side equal to the right side INITIAL_MOVE_DELAY;
062 level =this assignment operator makes the left side equal to the right side 0;
063 whilewhile is a looping structure for executing code repeatedly (level !=this is the not equals operator which evaluates to true if both sides are different startLevel) {open braces start code blocks and must be matched with a close brace
064 nextLevel();
065 }close braces end code blocks and must match an earlier open brace
066 }close braces end code blocks and must match an earlier open brace
067
068 /**
069 * Advance to the next frame.
070 *
071 * @paramthis is the Javadoc tag for documenting the purpose of parameters dT the time since the last advance
072 */
073 @Override
074 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value advance(doubledouble is the type for numbers that can contain decimal fractions dT) {open braces start code blocks and must be matched with a close brace
075 intint is the type for whole numbers and it is short for integer oldScore =this assignment operator makes the left side equal to the right side score.getScore();
076
077 moveClock -=this decreases the variable on the left by the value on the right dT;
078 ifif executes the next statement only if the condition in parenthesis evaluates to true (moveClock <=this evaluates to true if the left side is not more than the right side 0.0) {open braces start code blocks and must be matched with a close brace
079 updateBoard();
080 moveClock +=this increases the variable on the left by the value on the right moveDelay;
081 }close braces end code blocks and must match an earlier open brace
082
083 checkKeyPressed();
084
085 intint is the type for whole numbers and it is short for integer newScore =this assignment operator makes the left side equal to the right side score.getScore();
086 updateLevel(oldScore, newScore);
087 }close braces end code blocks and must match an earlier open brace
088
089 /**
090 * Setup the game by adding all the sprites.
091 */
092 @Override
093 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setup() {open braces start code blocks and must be matched with a close brace
094 score =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ScoreSprite();
095 score.setScale(0.10);
096 score.setLocation(0.20, 0.10);
097 addSprite(score);
098
099 displayLevel =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite();
100 displayLevel.setScale(0.10);
101 displayLevel.setColor(getColor("green"));
102 displayLevel.setLocation(0.10, 0.10);
103 displayLevel.setText(Integer.toString(level));
104 addSprite(displayLevel);
105
106 board =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Board(score, 30, 10);
107 board.setLocation(0.5, 0.5);
108 board.setScale(0.9);
109 addSprite(board);
110
111 // scale nextPiece to match the piece scale of the board
112 doubledouble is the type for numbers that can contain decimal fractions nextPieceSize =this assignment operator makes the left side equal to the right side board.getBlockSize() * board.getScale();
113 nextPiece =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor NextPieceBox(nextPieceSize);
114 nextPiece.setLocation(0.8, 0.5);
115 nextPiece.nextPiece();
116 addSprite(nextPiece);
117
118 board.add(nextPiece.getPiece());
119 nextPiece.nextPiece();
120 }close braces end code blocks and must match an earlier open brace
121
122 /**
123 * Check ifif executes the next statement only if the condition in parenthesis evaluates to true a key was pressed; ifif executes the next statement only if the condition in parenthesis evaluates to true it was, handle it. Moved out of
124 * {open braces start code blocks and must be matched with a close brace@link #advance(doubledouble is the type for numbers that can contain decimal fractions)}close braces end code blocks and must match an earlier open brace to simplify reading that method.
125 */
126 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value checkKeyPressed() {open braces start code blocks and must be matched with a close brace
127 ifif executes the next statement only if the condition in parenthesis evaluates to true (keyPressed()) {open braces start code blocks and must be matched with a close brace
128 charchar is the type for a single letter or symbol and it is short for character key =this assignment operator makes the left side equal to the right side getKeyPressed();
129 ifif executes the next statement only if the condition in parenthesis evaluates to true ((key ==this is the comparison operator which evaluates to true if both sides are the same KeyEvent.VK_LEFT) ||this is boolean or, meaning if either or both are true then the result is true (key ==this is the comparison operator which evaluates to true if both sides are the same KeyEvent.VK_KP_LEFT)) {open braces start code blocks and must be matched with a close brace
130 board.moveLeftIfPossible();
131 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true ((key ==this is the comparison operator which evaluates to true if both sides are the same KeyEvent.VK_RIGHT) ||this is boolean or, meaning if either or both are true then the result is true
132 (key ==this is the comparison operator which evaluates to true if both sides are the same KeyEvent.VK_KP_RIGHT)) {open braces start code blocks and must be matched with a close brace
133 board.moveRightIfPossible();
134 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true ((key ==this is the comparison operator which evaluates to true if both sides are the same KeyEvent.VK_DOWN) ||this is boolean or, meaning if either or both are true then the result is true
135 (key ==this is the comparison operator which evaluates to true if both sides are the same KeyEvent.VK_KP_DOWN)) {open braces start code blocks and must be matched with a close brace
136 board.moveDownIfPossible();
137 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true ((key ==this is the comparison operator which evaluates to true if both sides are the same KeyEvent.VK_UP) ||this is boolean or, meaning if either or both are true then the result is true
138 (key ==this is the comparison operator which evaluates to true if both sides are the same KeyEvent.VK_KP_UP)) {open braces start code blocks and must be matched with a close brace
139 board.rotateCCWIfPossible();
140 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true ((key ==this is the comparison operator which evaluates to true if both sides are the same ' ')) {open braces start code blocks and must be matched with a close brace
141 board.drop();
142 board.add(nextPiece.getPiece());
143 nextPiece.nextPiece();
144 }close braces end code blocks and must match an earlier open brace
145 }close braces end code blocks and must match an earlier open brace
146 }close braces end code blocks and must match an earlier open brace
147
148 /**
149 * Actually end the current game. Check ifif executes the next statement only if the condition in parenthesis evaluates to true we have a newnew is used to create objects by calling the constructor high score
150 * and handle that ifif executes the next statement only if the condition in parenthesis evaluates to true necessary. In any casecase is used with switch for multiple alternatives (like if/else if...), create a {open braces start code blocks and must be matched with a close brace@link
151 * HighScoreLevel}close braces end code blocks and must match an earlier open brace to show the high scores (and let the player start
152 * over ifif executes the next statement only if the condition in parenthesis evaluates to true they wish)
153 */
154 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value endThisGame() {open braces start code blocks and must be matched with a close brace
155 HighScoreLevel attract =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor HighScoreLevel(score.getScore());
156 addGame(attract);
157 finishGame();
158 }close braces end code blocks and must match an earlier open brace
159
160 /**
161 * Move to the next level. Speed up, too.
162 */
163 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value nextLevel() {open braces start code blocks and must be matched with a close brace
164 ++this is the increment operator, which increases the variable by 1level;
165 moveDelay /=this divides the variable on the left by the value on the right and stores the result in the variable SPEED_UP_FACTOR;
166 ifif executes the next statement only if the condition in parenthesis evaluates to true (displayLevel !=this is the not equals operator which evaluates to true if both sides are different nullnull is the value used to refer to a non-existant object) {open braces start code blocks and must be matched with a close brace
167 displayLevel.setText(Integer.toString(level));
168 }close braces end code blocks and must match an earlier open brace
169 }close braces end code blocks and must match an earlier open brace
170
171 /**
172 * Have the board trytry is for executing a code block that may experience exceptions (errors) to move the current piece down. If it can't then
173 * the piece was frozen (by the board). Check ifif executes the next statement only if the condition in parenthesis evaluates to true it ended the game. If
174 * it didn't, get a piece from the next piece generator and have it
175 * make a newnew is used to create objects by calling the constructor piece. As longlong is the type for whole numbers (they have a larger range than int) as the game has not ended, reset the move
176 * clock.
177 */
178 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value updateBoard() {open braces start code blocks and must be matched with a close brace
179 ifif executes the next statement only if the condition in parenthesis evaluates to true (!this is the not operator, which changes true to false and false to trueboard.moveDownIfPossible()) {open braces start code blocks and must be matched with a close brace
180 ifif executes the next statement only if the condition in parenthesis evaluates to true (board.isGameOver()) {open braces start code blocks and must be matched with a close brace
181 endThisGame();
182 returnreturn means to provide the result of the method and/or cease execution of the method immediately;
183 }close braces end code blocks and must match an earlier open brace
184 board.add(nextPiece.getPiece());
185 nextPiece.nextPiece();
186 }close braces end code blocks and must match an earlier open brace
187 }close braces end code blocks and must match an earlier open brace
188
189 /**
190 * Update the displayed level on the screen. The level is the number
191 * of times {open braces start code blocks and must be matched with a close brace@link #LINES_PER_LEVEL}close braces end code blocks and must match an earlier open brace rows have been cleared since the
192 * game started (plus the starting level). The level has changed ifif executes the next statement only if the condition in parenthesis evaluates to true
193 * the integer quotient of the old score and the integer quotient of
194 * the newnew is used to create objects by calling the constructor score, each divided by the lines per level, are different.
195 * If they are different, then the {open braces start code blocks and must be matched with a close brace@link #nextLevel()}close braces end code blocks and must match an earlier open brace method is
196 * called. It increments the level and speeds up the game.
197 *
198 * @paramthis is the Javadoc tag for documenting the purpose of parameters oldScore score before the current play
199 * @paramthis is the Javadoc tag for documenting the purpose of parameters newScore score after the current play
200 */
201 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value updateLevel(intint is the type for whole numbers and it is short for integer oldScore, intint is the type for whole numbers and it is short for integer newScore) {open braces start code blocks and must be matched with a close brace
202 intint is the type for whole numbers and it is short for integer oldScoreLevel =this assignment operator makes the left side equal to the right side oldScore / LINES_PER_LEVEL;
203 intint is the type for whole numbers and it is short for integer newScoreLevel =this assignment operator makes the left side equal to the right side newScore / LINES_PER_LEVEL;
204 whilewhile is a looping structure for executing code repeatedly (newScoreLevel > oldScoreLevel) {open braces start code blocks and must be matched with a close brace
205 nextLevel();
206 ++this is the increment operator, which increases the variable by 1oldScoreLevel;
207 }close braces end code blocks and must match an earlier open brace
208 }close braces end code blocks and must match an earlier open brace
209 }close braces end code blocks and must match an earlier open brace
210
211 //Uploaded on Mon Mar 29 21:41:13 EDT 2010
|