Source Tetris
Source Tetris
Source Tetris
package tetris;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
/**
*/
public Tetris() {
initUI () ; }
add(model);
setSize(200, 400);
setTitle ("Tetris");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
return statusbar;
}
public static void main (String []args){
SwingUtilities.invokeLater(new Runnable(){
@Override
game.setVisible (true);
});
Btetris
package tetris;
import java.util.Random;
setShape (Tetrominoes.NoShape);
pieceShape = shape;
int x = Math.abs(r.nextInt()) % 7 + 1;
setShape (values[x]);
m = Math.min(m, coords[i][0]);
return m;
}
return m;
if (pieceShape == Tetrominoes.SquareShape)
return this;
result.pieceShape = pieceShape;
result.setX(i, y(i));
result.setY(i, -x(i));
return result;
if (pieceShape == Tetrominoes.SquareShape)
return this;
result.pieceShape = pieceShape;
result.setX(i, -y(i));
result.setY(i, x(i));
return result;
}
Model
package tetris;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import tetris.btetris.Tetrominoes;
implements ActionListener {
initBoard(parent);
setFocusable(true);
timer.start();
statusbar = parent.getStatusBar();
clearBoard ();
@Override
if (isFallingFinished){
isFallingFinished = false;
newPiece();
} else {
oneLineDown();
if (isPaused)
return;
isStarted = true;
isFallingFinished = false;
numLinesRemoved = 0;
clearBoard();
newPiece();
timer.start();
if (!isStarted)
return;
isPaused = !isPaused;
if (isPaused){
timer.stop();
statusbar.setText("paused");
} else {timer.start();
statusbar.setText(String.valueOf(numLinesRemoved));
repaint();
drawSquare(g, 0 + j * squareWidth(),
if (curPiece.getShape() != Tetrominoes.NoShape){
curPiece.getShape());
super.paintComponent(g);
doDrawing(g);
break;
--newY;
pieceDropped();
}
pieceDropped ();
board[i] = Tetrominoes.NoShape;
removeFullLines();
if (!isFallingFinished)
newPiece();
curX = BoardWidth / 2 + 1;
curPiece.setShape(Tetrominoes.NoShape);
timer.stop();
isStarted = false;
statusbar.setText("game over");
return false;
if (shapeAt(x,y)!= Tetrominoes.NoShape)
return false;
curPiece= newPiece;
curX = newX;
curY = newY;
repaint();
return true;
int numFullLines = 0;
for(int i = BoardHeight - 1; i >= 0; --i){
lineIsFull = false;
break;
if (lineIsFull){
++numFullLines;
numLinesRemoved += numFullLines;
statusbar.setText(String.valueOf(numLinesRemoved));
isFallingFinished = true;
curPiece.setShape(Tetrominoes.NoShape);
repaint();
Color colors [] = { new Color (0, 0, 0), new Color (204, 102,
102),
new Color (102, 204, 102), new Color (102, 102, 204),
new Color (204, 204, 102), new Color (204, 102, 204),
new Color (102, 204, 204), new Color (218, 170, 0)
};
g.setColor(color);
g.setColor(color.brighter());
g.setColor(color.darker());
g.drawLine(x + 1, y + squareHeight() - 1,
x + squareWidth() - 1, y + 1);
@Override
if (curPiece.getShape() == Tetrominoes.NoShape){
return;
switch (keycode){
case 'p':
pause();
case KeyEvent.VK_LEFT:
break;
case KeyEvent.VK_RIGHT:
break;
case KeyEvent.VK_DOWN:
break;
case KeyEvent.VK_UP:
break;
case KeyEvent.VK_SPACE:
dropDown();
break;
case 'd':
oneLineDown();
break;
case 'D':
oneLineDown();
break;