weixin

用java写一个俄罗斯方块游戏

日期: September 16, 2019 作者:网站维护

代码是在网上找到的,只有300多行非常简易。看的时候做了一些改动。到现在对java的很多概念还是很模糊的,代码也没有什么规范,只是大致了解一下怎么做这个小游戏。

最先要生成界面,比如一个方块,需要用java的“界面”的功能。jFrame, jPanel的类。
方块每秒自由下落,就跟动画一样,每秒要刷新一下界面,就需要用到定时执行的功能。
还需要移动方块,就需要使用java的键盘的接口。

直接把代码贴出来,只有四个类

  • testg 主要是运行入口
  • simpleFrame 主界面框架
  • blk 框架里的内容,有俄罗斯方块,边界,已经下落的部分
  • TimerListener 每秒执行的程序
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;
import javax.swing.Timer;


public class testg {
    public static void main(String[] args) {
        EventQueue.invokeLater(() ->
             {
            simpleFrame frame = new simpleFrame();
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
             });
    }
}

class simpleFrame extends JFrame{
    public simpleFrame() {
        setSize(300, 200);
        blk a = new blk();
        a.drawwall();
        add(a);
        addKeyListener(a);
        Timer timer = new Timer(1000, new TimerListener(a));
        timer.start();
    }
}

class blk extends JPanel implements KeyListener {
    private int y;
    private int x = 4;
    
    private int blockType;
    private int turnState;
    
    public int getblockType() {
        return this.blockType;
    }
    
    public int getturnState() {
        return this.turnState;
    }
    
    public int getx() {
        return this.x;
    }
    public int gety() {
        return this.y;
    }
    public void plusy() {
        y = y + 1;
    }
    private int map[][] = new int[13][23];

    private final int shapes[][][] = 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 void drawwall() {
        for (int i = 0; i < 12; i++) {
            map[i][21] = 2;
        }
        for (int j = 0; j < 22; j++) {
            map[11][j] = 2;
            map[0][j] = 2;
        }
    }
    
    public void add(int x, int y, int blockType, int turnState) {
        int j = 0;
        for (int a = 0; a < 4; a++) {
            for (int b = 0; b < 4; b++) {
                if (map[x + b][y + a] == 0) {
                    map[x + b][y + a] = shapes[blockType][turnState][j];
                }
                ;
                j++;
            }
        }
    }
    
    public void delline() {
        int c = 0;
        for (int b = 0; b < 22; b++) {
            for (int a = 0; a < 12; a++) {
                if (map[a][b] == 1) {

                    c = c + 1;
                    if (c == 10) {
                        for (int d = b; d > 0; d--) {
                            for (int e = 0; e < 11; e++) {
                                map[e][d] = map[e][d - 1];

                            }
                        }
                    }
                }
            }
            c = 0;
        }
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 画当前方块
        for (int j = 0; j < 16; j++) {
            if (shapes[blockType][turnState][j] == 1) {
                g.fillRect((j % 4 + x) * 10, (j / 4 + y) * 10, 10, 10);
            }
        }
        // 画固定的块 
        
        for (int j = 0; j < 22; j++) {
            for (int i = 0; i < 12; i++) {
                if (map[i][j] == 1) {
                    g.fillRect(i * 10, j * 10, 10, 10);

                }
                if (map[i][j] == 2) {
                    g.drawRect(i * 10, j * 10, 10, 10);

                }
            }
        }
    }
    
    public void right() {
        if (blow(x + 1, y, blockType,turnState) == 1) {
            x = x + 1;
        };
        repaint();
    }
    
    public void left() {
        if (blow(x - 1, y, blockType,turnState) == 1) {
            x = x - 1;
        };
        repaint();
    }
    
    public void down() {
        if (blow(x, y + 1, blockType,turnState) == 1) {
            y = y + 1;
        }
        ;
        if (blow(x, y + 1, blockType,turnState) == 0) {
            add(x, y, blockType,turnState);
            delline();
            newblock();
        }
        ;
        repaint();
    }
    
    public void turn() {
        int tempturnState = turnState;
        turnState = (turnState + 1) % 4;
        if (blow(x, y, blockType, turnState) == 1) {
        }
        if (blow(x, y, blockType, turnState) == 0) {
            turnState = tempturnState;
        }
        repaint();
    }
    
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
        case KeyEvent.VK_RIGHT:
            right();
            break;
        case KeyEvent.VK_LEFT:
            left();
            break;
        case KeyEvent.VK_DOWN:
            down();
            break;
        case KeyEvent.VK_UP:
            turn();
            break;
        }
    }
    
    public int blow(int x, int y,  int blockType, int turnState) {
        for (int a = 0; a < 4; a++) {
            for (int b = 0; b < 4; b++) {
                if ((shapes[blockType][turnState][a * 4 + b] == 1) && (map[x
                        + b ][y + a] == 2) 
                        || ((shapes[blockType][turnState][a * 4 + b] == 1) && (map[x + b][y + a] == 1))) {

                    return 0;
                }
            }
        }
        return 1;
    }
    
    // 无用
    public void keyReleased(KeyEvent e) {
    }

    // 无用
    public void keyTyped(KeyEvent e) {
    }
    
    public void newblock() {
        blockType = (int) (Math.random() * 1000) % 7;
        turnState = (int) (Math.random() * 1000) % 4;
        x = 4;
        y = 0;
    }
}

class TimerListener implements ActionListener {
    private blk a;
    
    TimerListener(blk x){
        a = x;
    }
    public void actionPerformed(ActionEvent e) {
        a.repaint();
        int x = a.getx();
        int y = a.gety();
        int blockType = a.getblockType();
        int turnState = a.getturnState();
        
        if (a.blow(x, y + 1,  blockType, turnState) == 1) {
            a.plusy();
        }
        if (a.blow(x, y + 1,  blockType, turnState) == 0) {
            a.add(x, y, blockType, turnState);
            a.delline();
            a.newblock();
        }
    }
}

广告内容为平台自动生成