// Tetris main program skeleton code // Hao Jiang, April 2, 2008, at Boston College import java.awt.Color; public class Tetris implements DrawListener { private final int BW = 10; private final int BH = 20; private Draw draw = new Draw(300, 600); private int[][] board = new int[BH][BW]; private Tetromino tetrad; // SRS style //private Color[] tileColor = { // Color.cyan, Color.blue, Color.orange, Color.yellow, // Color.green, Color.pink, Color.red }; // Microsoft Color Style private Color[] tileColor = { Color.red, Color.magenta, Color.yellow, Color.cyan, Color.blue, Color.gray, Color.green }; public Tetris() { draw.addListener(this); draw.setXscale(0, BW); draw.setYscale(0, BH); } public void createNewTetrad() { int k = (int)(Math.random() * 7) + 1; tetrad = new Tetromino(k, board); } public void keyTyped(char c) { if (c == 'i') // up { tetrad.rotate(); } else if (c == 'j') // left { tetrad.moveLeft(); } else if (c == 'k') // down { tetrad.moveDown(); } else if (c == 'l') // right { tetrad.moveRight(); } } public void mousePressed (double x, double y) { } public void mouseDragged (double x, double y) { } public void mouseReleased(double x, double y) { } public void clearBoard() { for(int i = 0; i < BH; i ++) for(int j = 0; j < BW; j ++) { if (board[i][j] > 7) { board[i][j] = 0; } } } public void draw() { draw.clear(Color.black); for (int i = 0; i < BH; i ++) for (int j = 0; j < BW; j ++) { int t = board[i][j]; if (t > 7) t = t - 7; if ( t != 0) { draw.setPenColor(tileColor[t-1]); draw.filledSquare(j + 0.5, BH - 1 - i + 0.5, 0.48); } } drawBorder(); draw.show(5); } public void drawBorder() { draw.setPenColor(Color.white); draw.line(BW, 0, BW, BH); draw.line(0, 0, 0, BH); draw.line(0, 0, BW, 0); draw.line(0, BH, BW, BH); } public static void main(String[] args) { Tetris tetris = new Tetris(); tetris.createNewTetrad(); int cn = 0; while(true) { tetris.clearBoard(); tetris.drawBorder(); tetris.tetrad.move(); tetris.draw(); tetris.draw.show(10); } } }