import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.awt.geom.*; /** * Tetris.java 07-25-10 by Leon Schram and John Carpenter * * By July, 2009 the first 10 major stages of the Tetris game were completed. * The game was close to being finished, but it did have various bugs. * At that time a workshop of teacher working togetherv to create a new * Advanced Graphics Programming game was conducted and one teacher, John Carpenter * started with Stage 10 of the Tetris game, fixed the bugs and then completed the * game with features that are now lab assignment requirements which are: * 1. Remove full rows. * 2. Allow rapid drop down when pressing the down-arrow-key. * 3. Keep score and speed up the game as the score increases. * * This stage has a correct - bugs removed - Tetris game, but the final features are * not included since they become student graded requirements. **/ public class Tetris { public static void main(String args[]) { Tetris11 tester = new Tetris11(); tester.setSize(1000,750); tester.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}}); tester.show(); } } class Tetris11 extends Frame implements Runnable, KeyListener { private BufferedImage virtualMem; private Graphics2D g2; private Thread thread; private Piece currPiece; private Piece nextPiece; private Piece tempPiece; private Grid grid; private boolean newGame; private boolean waitForNewPiece = false; public Tetris11() { virtualMem = new BufferedImage(1000,750,BufferedImage.TYPE_INT_RGB); g2 = virtualMem.createGraphics(); grid = new Grid(); newGame = true; addKeyListener(this); drawGameField(); start(); } public void start() { if (thread == null) { thread = new Thread(this); thread.start(); } } public void stop() { if (thread != null) { thread.stop(); thread = null; } } public void run() { boolean pieceFits = false; Thread t = Thread.currentThread(); while (t == thread) { if (newGame) { currPiece = new Piece(); nextPiece = new Piece(); newGame = false; } else { currPiece = new Piece(nextPiece); nextPiece = new Piece(); } drawNextPiece(); try { int rowPos = currPiece.getRowPos(); if (grid.checkFit(currPiece)) { pieceFits = true; } else { pieceFits = false; System.out.println("GAME OVER"); stop(); } waitForNewPiece = false; while (pieceFits) { Thread.currentThread().setPriority(Thread.MAX_PRIORITY); grid.addPiece(currPiece); drawGrid(); grid.erasePiece(currPiece); rowPos++; currPiece.setRowPos(rowPos); if (grid.checkBoundary(currPiece)) if (grid.checkFit(currPiece)) pieceFits = true; else pieceFits = false; else pieceFits = false; if (!pieceFits) { waitForNewPiece = true; Piece tempPiece = new Piece(currPiece); tempPiece.setRowPos(rowPos-1); grid.addPiece(tempPiece); } Thread.sleep(300); repaint(); } } catch(Exception e) { System.out.println("Exception Movedown message " + e.getMessage()); stop(); } } } public void keyPressed(KeyEvent evt) { if (!waitForNewPiece) { boolean pieceFits = false; int colPos = currPiece.getColPos(); int dirNumber = currPiece.getDirNumber(); Piece tempPiece = new Piece(currPiece); int key = evt.getKeyCode(); switch (key) { case KeyEvent.VK_LEFT: colPos--; tempPiece.setColPos(colPos); break; case KeyEvent.VK_RIGHT: colPos++; tempPiece.setColPos(colPos); break; case KeyEvent.VK_UP: dirNumber++; if (dirNumber == 5) dirNumber = 1; tempPiece = new Piece(currPiece,dirNumber); break; } if (grid.checkBoundary(tempPiece)) if (grid.checkFit(tempPiece)) pieceFits = true; else pieceFits = false; else pieceFits = false; if (pieceFits) { grid.erasePiece(currPiece); currPiece = new Piece(tempPiece); grid.addPiece(currPiece); drawGrid(); try { Thread.sleep(10); } catch(Exception e) { System.out.println("Exception Keypressed Message " + e.getMessage()); } repaint(); } } } public void keyReleased(KeyEvent evt) { } public void keyTyped(KeyEvent evt) { } public void paint(Graphics g1) { g1.drawImage (virtualMem,0,0,this); } public void drawGameField() { g2.setColor(Color.white); g2.fillRect(0,0,1000,750); g2.setColor(Color.black); g2.drawRect(199,49,302,602); g2.drawRect(198,48,304,604); g2.drawRect(197,47,306,606); g2.drawRect(599,49,202,302); g2.drawRect(598,48,204,304); g2.drawRect(597,47,206,306); } public void drawGrid() { g2.setColor(Color.white); g2.fillRect(200,50,300,600); for (int row = 0; row < 20; row++) { for (int col = 0; col < 10; col++) { int leftTopX = (200 + col * 30); int leftTopY = (50 + row * 30); int pieceNumber = grid.getValue(row,col); if (pieceNumber >= 1) { g2.setColor(getPieceColor(pieceNumber)); g2.fillRect(leftTopX,leftTopY,30,30); g2.setColor(Color.BLACK); g2.drawRect(leftTopX+2,leftTopY+2,26,26); } } } } public void drawNextPiece() { g2.setColor(Color.white); g2.fillRect(600,50,200,300); int rowSize = nextPiece.getRowSize(); int colSize = nextPiece.getColSize(); for (int row = 0; row < rowSize; row++) for (int col = 0; col < colSize; col++) { int leftTopX = (670 + col * 30); int leftTopY = (70 + row * 30); int pieceNumber = nextPiece.getValue(row,col); if (pieceNumber >= 1) { g2.setColor(getPieceColor(pieceNumber)); g2.fillRect(leftTopX,leftTopY,30,30); g2.setColor(Color.BLACK); g2.drawRect(leftTopX+2,leftTopY+2,26,26); } } } private Color getPieceColor(int colorNumber) { Color pieceColor = Color.BLACK; switch (colorNumber) { case 1: pieceColor = Color.BLUE; break; case 2: pieceColor = Color.RED; break; case 3: pieceColor = Color.GREEN; break; case 4: pieceColor = Color.YELLOW; break; case 5: pieceColor = Color.ORANGE; break; case 6: pieceColor = Color.CYAN; break; case 7: pieceColor = Color.MAGENTA; break; } return pieceColor; } }