日期:2014-05-20  浏览次数:20723 次

写个俄罗斯方块,怎么监听不了键盘
Java code

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class TestGame extends Frame {
    int temporaryType, temporaryChape;
    int mark;
    Panel PUP = new Panel() {
        public void paint(Graphics g) {
            g.setColor(Color.blue);
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    if (map[temporaryType][temporaryChape][i][j] != 0)
                        g.fillRect(45 + j * 10, 15 + i * 10, 10, 10);
                }
            }
        }
    };
    final int[][][][] map = new int[][][][] {
            // i
            { { { 0, 0, 0, 0 }, { 1, 1, 1, 1 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }

            }, { { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 }

            }, { { 0, 0, 0, 0 }, { 1, 1, 1, 1 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }

            }, { { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 }

            } },
            // s
            {
                    { { 0, 1, 1, 0 }, { 1, 1, 0, 0 }, { 0, 0, 0, 0 },
                            { 0, 0, 0, 0 } },
                    { { 1, 0, 0, 0, }, { 1, 1, 0, 0, }, { 0, 1, 0, 0 },
                            { 0, 0, 0, 0 } },
                    { { 0, 1, 1, 0, }, { 1, 1, 0, 0 }, { 0, 0, 0, 0 },
                            { 0, 0, 0, 0 } },
                    { { 1, 0, 0, 0, }, { 1, 1, 0, 0, }, { 0, 1, 0, 0 },
                            { 0, 0, 0, 0 } } },
            // z
            {
                    { { 1, 1, 0, 0 }, { 0, 1, 1, 0, }, { 0, 0, 0, 0 },
                            { 0, 0, 0, 0 } },
                    { { 0, 1, 0, 0 }, { 1, 1, 0, 0 }, { 1, 0, 0, 0 },
                            { 0, 0, 0, 0 } },
                    { { 1, 1, 0, 0 }, { 0, 1, 1, 0, }, { 0, 0, 0, 0 },
                            { 0, 0, 0, 0 } },
                    { { 0, 1, 0, 0 }, { 1, 1, 0, 0 }, { 1, 0, 0, 0 },
                            { 0, 0, 0, 0 } } },
            // j
            {
                    { { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 1, 1, 0, 0 },
                            { 0, 0, 0, 0 } },
                    { { 1, 0, 0, 0 }, { 1, 1, 1, 0 }, { 0, 0, 0, 0 },
                            { 0, 0, 0, 0 } },
                    { { 1, 1, 0, 0 }, { 1, 0, 0, 0 }, { 1, 0, 0, 0 },
                            { 0, 0, 0, 0 }, },
                    { { 1, 1, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 0 },
                            { 0, 0, 0, 0 } } },
            // o
            {
                    { { 1, 1, 0, 0 }, { 1, 1, 0, 0 }, { 0, 0, 0, 0 },
                            { 0, 0, 0, 0 } },
                    { { 1, 1, 0, 0 }, { 1, 1, 0, 0 }, { 0, 0, 0, 0 },
                            { 0, 0, 0, 0 } },
                    { { 1, 1, 0, 0 }, { 1, 1, 0, 0 }, { 0, 0, 0, 0 },
                            { 0, 0, 0, 0 } },
                    { { 1, 1, 0, 0 }, { 1, 1, 0, 0 }, { 0, 0, 0, 0 },
                            { 0, 0, 0, 0 } } },
            // L
            {
                    { { 1, 0, 0, 0 }, { 1, 0, 0, 0 }, { 1, 1, 0, 0 },
                            { 0, 0, 0, 0 } },
                    { { 1, 1, 1, 0 }, { 1, 0, 0, 0 }, { 0, 0, 0, 0 },
                            { 0, 0, 0, 0 } },
                    { { 1, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 },
                            { 0, 0, 0, 0 } },
                    { { 0, 0, 1, 0 }, { 1, 1, 1, 0 }, { 0, 0, 0, 0 },
                            { 0, 0, 0, 0 } } },
            // t
            {
                    { { 0, 1, 0, 0 }, { 1, 1, 1, 0 }, { 0, 0, 0, 0 },
                            { 0, 0, 0, 0 } },
                    { { 0, 1, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 0, 0 },
                            { 0, 0, 0, 0 } },
                    { { 1, 1, 1, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 0 },
                            { 0, 0, 0, 0 } },
                    { { 0, 1, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 },
                            { 0, 0, 0, 0 } } } };

    public static void main(String[] args) {
        TestGame tg = new TestGame();
        Game gui = tg.new Game();
        tg.UI(gui);
        gui.automatic();
    }
    // 画外框
    void UI(Game gui) {
        this.setBounds(400, 400, 260, 260);
        this.addKeyListener(new MyKeyMonitor());
        this.setLayout(new GridLayout(1, 2));
        Panel p = new Panel();
        p.setLayout(new GridLayout(1, 2));
        p.add(gui);
        Panel p3 = new Panel();
        p3.setLayout(new GridLayout(3, 1));
        p.add(p3);
        PUP.setBackground(Color.cyan);
        p3.add(PUP);
        Panel pCentre = new Panel();
        pCentre.setBackground(Color.green);
        p3.add(pCentre);
        Panel pDown = new Panel();
        pDown.setLayout(new GridLayout(4, 1));
        AllListener al = new AllListener();
        Button b1 = new Button("重新开始");
        b1.addActionListener(al);
        pDown.add(b1);
        Button b3 = new Button("开始");
        b3.addActionListener(al);
        pDown.add(b3);
        Button b2 = new Button("暂停");
        b2.addActionListener(al);
        pDown.add(b2);
        Button b = new Button("退出");
        b.addActionListener(al);
        pDown.add(b);
        p3.add(pDown);
        this.add(p);
        this.setVisible(true);
    }
    
    class Game extends Panel {
        private boolean over = false;
        private int mapX = 4;
        private int mapY;
        private int[][] container;
        private int typeMap;
        private int chapeMap;

        public Game() {
            containerMap();
            newMap();
            newMap();
        }

        // 绘制
        public void paint(Graphics g) {
            g.setColor(Color.blue);
            // g.fillRect(10, 10, 50,50);
            for (int i = 0; i < 22; i++) {
                for (int j = 0; j < 12; j++) {
                    if (container[i][j] != 0)
                        g.fillRect(j * 10, i * 10, 10, 10);
                }
            }
            g.setColor(Color.black);
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    if (map[typeMap][chapeMap][i][j] != 0)
                        g.fillRect(mapX * 10 + j * 10, mapY * 10 + i * 10, 10,
                                10);
                }
            }
        }

        // 画墙
        void containerMap() {
            container = new int[22][12];
            for (int y = 0; y < 22; y++) {
                if (y == 21) {
                    for (int x = 0; x < 12; x++) {
                        container[y][x] = 2;
                    }
                }
                container[y][0] = 2;
                container[y][11] = 2;
            }
        }

        // 算出新方块
        void newMap() {
            typeMap = temporaryType;
            chapeMap = temporaryChape;
            int i = new Random().nextInt(28);
            temporaryType = i % 7;
            temporaryChape = i % 4;
            TestGame.this.PUP.repaint();
        }

        // 判断是否合法
        boolean ifMap(int x, int y) {
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    if (map[typeMap][chapeMap][i][j] == 1) {
                        if (container[y + i][x + j] != 0)
                            return false;
                    }
                }
            }
            return true;
        }

        // 判断是否over
        boolean ifOver() {
            int x = 0;

            for (int i = 0; i < 21; i++) {
                for (int j = 1; j < 11; j++) {
                    if (container[i][j] != 0) {
                        x++;
                        break;
                    }
                }
                if (x >= 20)
                    return true;
            }
            return false;
        }

        // 判断消除
        void ifScores() {
            int ifscores = 0;
            for (int i = 0; i < 21; i++) {
                for (int j = 1; j < 11; j++) {
                    if (container[i][j] == 1)
                        ifscores++;
                }
                if (ifscores == 10) {
                    mark++;
                    this.soures(i);
                }
                ifscores = 0;
            }
        }

        // 消除后整合
        void soures(int y) {
            for (int i = (y - 1); i >= 0; i--) {
                for (int j = 1; j < 11; j++) {
                    container[i + 1][j] = container[i][j];
                }
            }
        }

        // 右移动
        void right() {
            if (this.ifMap(mapX - 1, mapY))
                mapX--;
        }

        // 左移动
        void left() {
            if (this.ifMap(mapX + 1, mapY))
                mapX++;
        }

        // 下移动
        void down() {
            if (this.ifMap(mapX, mapY + 1)) {
                this.mapY++;
            } else {
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        if (map[typeMap][chapeMap][i][j] == 1) {
                            container[mapY + i][mapX + j] = map[typeMap][chapeMap][i][j];
                        }
                    }
                }
                this.ifScores();
                over = this.ifOver();
                mapX = 4;
                mapY = 0;
                this.newMap();
            }
        }

        // 旋转
        void spin() {
            chapeMap = (chapeMap + 1) % 4;
            if (!ifMap(mapX, mapY))
                chapeMap = (chapeMap + 3) % 4;
        }

        // 自动下落
        void automatic() {
            while (!over) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.exit(0);
                }
                down();
                repaint();
            }
            System.out.println("game over");
        }
    }

    class AllListener implements ActionListener {
        public void actionPerformed(ActionEvent ae) {
            String s = ae.getActionCommand();
            if (s.equals("重新开始"))
                System.out.println("000");
            if (s.equals("开始"))
                System.out.println("001");
            if (s.equals("暂停"))
                System.out.println("002");
            if (s.equals("退出"))
                System.exit(0);
        }
    }
    class MyKeyMonitor extends KeyAdapter {
        public void keyPressed(KeyEvent e) {    
            int keyCode = e.getKeyCode();
            if (keyCode == KeyEvent.VK_UP) {
                System.out.println("UP");
            }
        }
    }
}