Public Member Functions | |
Tetris (int width, int height) | |
The constructor is creating an empty Teris Field. | |
IBlock[][] | getBlockMatrix () |
void | tickEvent () |
Check for collision Move the Stone down check for enough space to continue the game and exit if not. | |
void | keyEvent (int key, boolean state) |
listen to keys (Up, Left, Right, Down) and reset the movement, if there is no Stone | |
void | moveStone () |
This method actually moves the stone if any. | |
void | dropStone () |
Drop a stone if the down key is pressed. | |
String | toString () |
Static Public Attributes | |
static boolean | down |
whether the down key is presssed | |
Private Member Functions | |
void | clearRow (int rowToDelete) |
Delete a row of the field. | |
boolean | isRowFull (int row) |
test a row whether it is full | |
void | clearRows () |
remove all rows that are full | |
void | resetKeys () |
This method is to reset the keys after each collision. | |
Private Attributes | |
IBlock[][] | field |
The playfield represented as Block Array. | |
Score | score |
read the highscore and create a Score Object. | |
IStone | fStone = null |
The Stone that drops down. | |
boolean | left |
whether the left key is presssed | |
boolean | right |
whether the up right is presssed | |
boolean | turn |
whether the up key is presssed | |
Static Private Attributes | |
static final long | serialVersionUID = 7746581299608426600L |
keep the standards |
It has som methods toremove al line to advance to the next step and to react to keys.
game.Tetris.Tetris | ( | int | width, | |
int | height | |||
) |
The constructor is creating an empty Teris Field.
width | of the tetris | |
height | of the tetris |
References game.Tetris.field.
00080 { 00081 // Initialise the score 00082 score = new Score(); 00083 // Initialize the Field 00084 field = new IBlock[height][width]; 00085 // TimerEvent.addTimerListener(new TetrisTimerEventListener()); 00086 for (int row = 0; row < height; row++) 00087 for (int col = 0; col < width; col++) 00088 field[row][col] = new emptyBlock(row, col); 00089 }
void game.Tetris.clearRow | ( | int | rowToDelete | ) | [private] |
Delete a row of the field.
rowToDelete | The row that is to be deleted |
References game.Tetris.field, and game.IBlock.isEmpty().
Referenced by game.Tetris.clearRows().
00097 { 00098 for (int row = rowToDelete - 1; row >= 0; row--) { 00099 for (int col = 0; col < field[row].length; col++) { 00100 IBlock b = field[row][col]; 00101 if (b.isEmpty()) 00102 field[row + 1][col] = new emptyBlock(row + 1, col); 00103 else { 00104 ((Block) b).move(1, 0); 00105 field[row + 1][col] = b; 00106 field[row][col] = new emptyBlock(row, col); 00107 } 00108 00109 } 00110 } 00111 }
boolean game.Tetris.isRowFull | ( | int | row | ) | [private] |
test a row whether it is full
row | the row that isto be tested |
References game.Tetris.field.
Referenced by game.Tetris.clearRows().
00122 { 00123 for (IBlock b : field[row]) 00124 if (b.isEmpty()) 00125 return false; 00126 return true; 00127 }
void game.Tetris.clearRows | ( | ) | [private] |
remove all rows that are full
References game.Tetris.clearRow(), game.Tetris.field, and game.Tetris.isRowFull().
00135 { 00136 int rowCount = 0; 00137 for (int row = 0; row < field.length; row++) { 00138 if (isRowFull(row)) { 00139 clearRow(row); 00140 row--; 00141 rowCount++; 00142 } 00143 } 00144 new DroppEvent(this, rowCount); 00145 }
IBlock [][] game.Tetris.getBlockMatrix | ( | ) |
References game.Tetris.field.
Referenced by engine.draw.drawTetris().
00150 { 00151 return field; 00152 }
void game.Tetris.tickEvent | ( | ) |
Check for collision Move the Stone down check for enough space to continue the game and exit if not.
References game.Tetris.field, game.Tetris.fStone, and game.IStone.moveDown().
00158 { 00159 if (fStone == null) { 00160 fStone = Stones.getStone(this.field, 0, (int) (field[0].length / 2)); 00161 this.clearRows(); 00162 } 00163 if (!fStone.moveDown()) { 00164 fStone = null; 00165 } 00166 if (!(this.field[0][(int) (field[0].length / 2)]).isEmpty()){ 00167 new GameOverEvent(this); 00168 } 00169 }
void game.Tetris.keyEvent | ( | int | key, | |
boolean | state | |||
) |
listen to keys (Up, Left, Right, Down) and reset the movement, if there is no Stone
key | the keycode of the key pressed | |
state | the state the matching key should be set to |
References game.Tetris.down, and game.Tetris.fStone.
Referenced by engine.JavaRenderer.keyPressed(), and engine.JavaRenderer.keyReleased().
00180 { 00181 if (fStone == null) 00182 return; 00183 00184 switch (key) { 00185 case KeyEvent.VK_UP: 00186 this.turn = state; 00187 break; 00188 case KeyEvent.VK_LEFT: 00189 this.left = state; 00190 break; 00191 case KeyEvent.VK_DOWN: 00192 down = state; 00193 break; 00194 case KeyEvent.VK_RIGHT: 00195 this.right = state; 00196 break; 00197 } 00198 this.moveStone(); 00199 }
void game.Tetris.moveStone | ( | ) |
This method actually moves the stone if any.
the direction is stored in the various vars of this class.
References game.Tetris.fStone, game.Tetris.left, game.IStone.moveLeft(), game.IStone.moveRight(), game.Tetris.right, game.IStone.rotate(), and game.Tetris.turn.
00210 { 00211 if (fStone == null) { 00212 this.resetKeys(); 00213 return; 00214 } 00215 if (this.left){ 00216 fStone.moveLeft(); 00217 this.left = false; 00218 } 00219 if (this.right){ 00220 fStone.moveRight(); 00221 this.right = false; 00222 } 00223 if (this.turn) { 00224 fStone.rotate(); 00225 this.turn = false; 00226 } 00227 }
void game.Tetris.dropStone | ( | ) |
Drop a stone if the down key is pressed.
References game.Tetris.down, game.Tetris.fStone, and game.IStone.moveDown().
00232 { 00233 if(fStone == null){ 00234 this.resetKeys(); 00235 return; 00236 } 00237 if (down) 00238 fStone.moveDown(); 00239 }
void game.Tetris.resetKeys | ( | ) | [private] |
This method is to reset the keys after each collision.
Somehow without this there are some strange movement artefacts.
References game.Tetris.down.
00250 { 00251 down = false; 00252 this.turn = false; 00253 this.left = false; 00254 this.right = false; 00255 }
String game.Tetris.toString | ( | ) |
References game.Tetris.field.
00262 { 00263 String res = ""; 00264 for (IBlock[] row : field) { 00265 for (IBlock block : row) { 00266 if (block.isEmpty()) 00267 res += " "; 00268 else 00269 res += "+"; 00270 } 00271 res += '\n'; 00272 } 00273 return res; 00274 }
final long game.Tetris.serialVersionUID = 7746581299608426600L [static, private] |
keep the standards
IBlock [][] game.Tetris.field [private] |
The playfield represented as Block Array.
Referenced by game.Tetris.clearRow(), game.Tetris.clearRows(), game.Tetris.getBlockMatrix(), game.Tetris.isRowFull(), game.Tetris.Tetris(), game.Tetris.tickEvent(), and game.Tetris.toString().
Score game.Tetris.score [private] |
read the highscore and create a Score Object.
IStone game.Tetris.fStone = null [private] |
The Stone that drops down.
Referenced by game.Tetris.dropStone(), game.Tetris.keyEvent(), game.Tetris.moveStone(), and game.Tetris.tickEvent().
boolean game.Tetris.left [private] |
boolean game.Tetris.right [private] |
boolean game.Tetris.turn [private] |
boolean game.Tetris.down [static] |
whether the down key is presssed
Referenced by game.Tetris.dropStone(), game.Tetris.keyEvent(), and game.Tetris.resetKeys().