JAVA版俄罗斯方块游戏

班长俄罗斯方块游戏

1.ShapeFactory

import java.util.Random;

public class ShapeFactory {
    private int shapes[][][] = new int[][][]{
            {
                    {1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                    {1, 1, 0, 0, 1, 10, 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},
                    {0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0}
            },
            {
                    {1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 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},
                    {0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
                    {0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}
            },
            {
                    {1, 1, 1, 1, 0, 0, 0, 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}
            },
            {
                    {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}
            }
    };

    public Shape getShape(ShapeListener shapeListener) {
        System.out.println("ShapeFactory's getShape");
        Shape shape = new Shape();
        shape.addShapeListener(shapeListener);
        int type = new Random().nextInt(shapes.length);
        shape.setBody(shapes[type]);
        shape.setStatus(0);
        return shape;
    }
}

2.ShapeListener

public interface ShapeListener {
    void shapeMoveDown(Shape shape);

    boolean isShapeMoveDownable(Shape shape);
}

3.Shape

import java.awt.*;

public class Shape {
    public static final int ROTATE = 0;
    public static final int LEFT = 1;
    public static final int RIGHT = 2;
    public static final int DOWN = 3;
    private int[][] body;
    private int status;
    private int left;
    private int top;

    public int getLeft() {
        return left;
    }

    public int getTop() {
        return top;
    }

    private ShapeListener shapeListener;

    public void setBody(int[][] body) {
        this.body = body;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public void moveLeft() {
        System.out.println("shapes moveleft");
        left--;
    }

    public void moveRight() {
        System.out.println("shapes moveright");
        left++;
    }

    public void moveDown() {
        System.out.println("shapes moveDown");
        top++;
    }

    public void rotate() {
        System.out.println("shapes rotate");
        status = (status + 1) % body.length;
    }

    public void drawMe(Graphics g) {
        System.out.println("shapes drawMe");
        g.setColor(Color.red);
        for (int x = 0; x < 4; x++) {
            for (int y = 0; y < 4; y++) {
                if (getFlagByPoint(status, x, y)) {
                    g.fill3DRect((left + x) * Global.CELL_SIZE, (y + top) * Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, true);
                }
            }
        }
    }

    protected boolean getFlagByPoint(int status, int x, int y) {
        if (body != null) {
            return body[status][y * 4 + x] == 1;
        } else {
            return false;
        }
    }

    public boolean isMember(int x, int y, boolean isRotate) {
        return getFlagByPoint(isRotate ? (status + 1) % body.length : status,
                x, y);
    }

    private class ShapeDriver implements Runnable {

        @Override
        public void run() {
            //  shapeListener.isShapeMoveDownable(Shape.this)
            while (shapeListener.isShapeMoveDownable(Shape.this)) {
                moveDown();
                shapeListener.shapeMoveDown(Shape.this);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public Shape() {
        new Thread(new ShapeDriver()).start();
    }

    public void addShapeListener(ShapeListener l) {
        if (l != null)
            this.shapeListener = l;
    }
}

4.Ground

import java.awt.*;

public class Ground {
    private boolean gameOver = false;
    private int[][] obstacles = new int[Global.WIDTH][Global.HEIGHT];

    public void accept(Shape shape) {
        System.out.println("Ground's accept shape");
        for (int x = 0; x < 4; x++) {
            for (int y = 0; y < 4; y++) {
                if (shape.isMember(x, y, false)) {
                    obstacles[shape.getLeft() + x][shape.getTop() + y] = 1;
                }
            }
        }
        deleteFullLine();
        isGameOver();
    }

    public void isGameOver() {
        for (int x = 0; x < Global.WIDTH; x++) {
            if (obstacles[x][0] != 0) {
                gameOver = true;
            }
        }
    }

    public void deleteFullLine() {
        for (int y = Global.HEIGHT - 1; y > 0; y--) {
            boolean full = true;
            for (int x = 0; x < Global.WIDTH; x++) {
                if (obstacles[x][y] != 1)
                    full = false;
            }
            if (full) {
                deleteLine(y);
            }
        }
    }

    public void deleteLine(int numLine) {
        for (int y = numLine; y > 0; y--) {
            for (int x = 0; x < Global.WIDTH; x++) {
                obstacles[x][y] = obstacles[x][y - 1];
            }
        }
        for (int x = 0; x < Global.WIDTH; x++) {
            obstacles[x][0] = 0;
        }
    }

    public void drawMe(Graphics g) {
        g.setColor(Color.ORANGE);
        for (int y = 0; y < Global.HEIGHT; y++) {
            for (int x = 0; x < Global.WIDTH; x++) {
                if (obstacles[x][y] == 1) {
                    g.fill3DRect(x * Global.CELL_SIZE, y * Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, true);
                }
            }
        }
        g.setColor(Color.red);
        Font font = new Font("宋体", Font.BOLD, 100);
        g.setFont(font);
        if (gameOver) {
            g.drawString("gameover", 0, 300);
        }
        System.out.println("Ground's drawMe");
    }

    public boolean isMoveable(Shape shape, int action) {
        int left = shape.getLeft();
        int top = shape.getTop();
        switch (action) {
            case Shape.LEFT:
                left--;
                break;
            case Shape.RIGHT:
                left++;
                break;
            case Shape.DOWN:
                top++;
                break;
        }
        for (int x = 0; x < 4; x++) {
            for (int y = 0; y < 4; y++) {
                if (shape.isMember(x, y, action == Shape.ROTATE)) {
                    if (top + y >= Global.HEIGHT ||
                            left + x < 0 ||
                            left + x >= Global.WIDTH ||
                            obstacles[x + left][y + top] == 1)
                        return false;
                }
            }
        }
        return true;
    }
}

5.Global

public class Global {
    public static final int CELL_SIZE = 40;
    public static final int WIDTH = 15;
    public static final int HEIGHT = 15;
}

6.GameController

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class GameController extends KeyAdapter implements ShapeListener{
    private Shape shape;
    private ShapeFactory shapeFactory;
    private Ground ground;
    private GamePanel gamePanel;

    public GameController(ShapeFactory shapeFactory, Ground ground, GamePanel gamePanel) {
        this.shapeFactory= shapeFactory;
        this.ground =ground;
        this.gamePanel=gamePanel;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()){
            case KeyEvent.VK_UP:
                if (ground.isMoveable(shape,Shape.ROTATE))
                    shape.rotate();
                break;
            case KeyEvent.VK_DOWN:
                if (ground.isMoveable(shape,Shape.DOWN))
                    shape.moveDown();
                break;
            case KeyEvent.VK_LEFT:
                if (ground.isMoveable(shape,Shape.LEFT))
                    shape.moveLeft();
                break;
            case KeyEvent.VK_RIGHT:
                if (ground.isMoveable(shape,Shape.RIGHT))
                    shape.moveRight();
            break;
        }
        gamePanel.display(ground,shape);
    }
    public void newGame(){
        shape =shapeFactory.getShape(this);
    }

    @Override
    public void shapeMoveDown(Shape shape) {
        gamePanel.display(ground,shape);
    }
    @Override
    public synchronized boolean isShapeMoveDownable(Shape shape) {
        if (ground.isMoveable(shape,Shape.DOWN))
            return true;
        ground.accept(shape);
        this.shape = shapeFactory.getShape(this);
        boolean result = ground.isMoveable(shape,Shape.DOWN);
        return result;
    }
}

7.GamePanel

import javax.swing.*;
import java.awt.*;

public class GamePanel extends JPanel {
    public GamePanel() {
        this.setSize(Global.WIDTH * Global.CELL_SIZE, Global.HEIGHT * Global.CELL_SIZE);
    }

    private Ground ground;
    private Shape shape;

    public void display(Ground ground, Shape shape) {
        System.out.println("GamePanel's display");
        this.ground = ground;
        this.shape = shape;
        this.repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        if (shape != null && ground != null) {
            shape.drawMe(g);
            ground.drawMe(g);

        }
    }
}

8.Game

import javax.swing.*;

public class Game {
    public static void main(String[] args) {
        ShapeFactory shapeFactory = new ShapeFactory();
        Ground ground = new Ground();
        GamePanel gamePanel = new GamePanel();
        GameController gameController = new GameController(shapeFactory, ground, gamePanel);
        JFrame jFrame = new JFrame();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setTitle("班长-俄罗斯方块");
        jFrame.setResizable(false);
        jFrame.setSize(gamePanel.getWidth() + 10, gamePanel.getHeight() + 35);
        jFrame.add(gamePanel);
        //       gamePanel.addKeyListener(controller);
        jFrame.addKeyListener(gameController);
        jFrame.setVisible(true);
        gameController.newGame();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值