package tetris; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Game extends JFrame implements ActionListener, KeyListener { private TetrisBoard tb; private Timer t; private boolean running; public static final int BOARD_WIDTH = 10; public static final int BOARD_HEIGHT = 25; public static void main(String args[]) { System.out.println("Piece controls: Left, Down, Right"); System.out.println("SPACE drops piece down"); System.out.println("Hit P to start the game."); Game g = new Game(); g.play(); } public Game() { super("Tetris"); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); tb = new TetrisBoard(BOARD_WIDTH, BOARD_HEIGHT); this.getContentPane().add(tb); this.running = false; t = new Timer(300, this); addKeyListener(this); } public void play() { pack(); show(); t.start(); } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == t && running) tb.tick(); } public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_Q) System.exit(0); if (key == KeyEvent.VK_P) running = !running; tb.keyPress(key); } }