【Java游戏】java俄罗斯方块!

目录

一、心得

二、游戏实例

三、代码

 

一、心得

      在学习Java的过程中,难免会遇到很多的问题,这些问题可能会困扰你许久,为此我有个Java学习交流群(925050116),里面都是CSDN的小伙伴,大家互相学习互相交流共同进步,每日分享不同的学习资料。

点击获取更多素材游戏源码!!

百度盘链接

链接:http://pan.baidu.com/s/1mhQ9SYc 密码:9ujo

 

二、游戏实例

游戏截图

【Java游戏】java俄罗斯方块!_第1张图片

【Java游戏】java俄罗斯方块!_第2张图片

【Java游戏】java俄罗斯方块!_第3张图片

目录结构

【Java游戏】java俄罗斯方块!_第4张图片

 

 

三、代码

1、主界面 Tetris.java

package com.hsj.tetris;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
 * 俄罗斯方块游戏面板
 *
 */
public class Tetris extends JPanel {
    /** 正在下落方块 */
    private Tetromino tetromino;
    /** 下一个下落方块 */
    private Tetromino nextOne;
    /** 行数 */
    public static final int ROWS = 20;
    /** 列数 */
    public static final int COLS = 10;
    /** 墙 */
    private Cell[][] wall = new Cell[ROWS][COLS];
    /** 消掉的行数 */
    private int lines;
    /** 分数 */
    private int score;

    public static final int CELL_SIZE = 26;

    private static Image background;//背景图片
    public static Image I;
    public static Image J;
    public static Image L;
    public static Image S;
    public static Image Z;
    public static Image O;
    public static Image T;
    static{//加载静态资源的,加载图片
        //建议将图片放到 Tetris.java 同包中!
        //从包中加载图片对象,使用Swing API实现
//        Toolkit toolkit = Toolkit.getDefaultToolkit();
//        background = toolkit.getImage(
//                Tetris.class.getResource("tetris.png"));
//        T = toolkit.getImage(Tetris.class.getResource("T.png"));
//        S = toolkit.getImage(Tetris.class.getResource("S.png"));
//        Z = toolkit.getImage(Tetris.class.getResource("Z.png"));
//        L = toolkit.getImage(Tetris.class.getResource("L.png"));
//        J = toolkit.getImage(Tetris.class.getResource("J.png"));
//        I = toolkit.getImage(Tetris.class.getResource("I.png"));
//        O = toolkit.getImage(Tetris.class.getResource("O.png"));
        //import javax.imageio.ImageIO;
        try{
            background = ImageIO.read(
                Tetris.class.getResource("tetris.png"));
            T=ImageIO.read(Tetris.class.getResource("T.png"));
            I=ImageIO.read(Tetris.class.getResource("I.png"));
            S=ImageIO.read(Tetris.class.getResource("S.png"));
            Z=ImageIO.read(Tetris.class.getResource("Z.png"));
            L=ImageIO.read(Tetris.class.getResource("L.png"));
            J=ImageIO.read(Tetris.class.getResource("J.png"));
            O=ImageIO.read(Tetris.class.getResource("O.png"));
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void action(){
        //tetromino = Tetromino.randomTetromino();
        //nextOne = Tetromino.randomTetromino();
        //wall[19][2] = new Cell(19,2,Tetris.T);
        startAction();
        repaint();
        KeyAdapter l = new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                int key = e.getKeyCode();
                if(key == KeyEvent.VK_Q){
                    System.exit(0);//退出当前的Java进程
                }
                if(gameOver){
                    if(key==KeyEvent.VK_S){
                        startAction();
                    }
                    return;
                }
                //如果暂停并且按键是[C]就继续动作
                if(pause){//pause = false
                    if(key==KeyEvent.VK_C){    continueAction();    }
                    return;
                }
                //否则处理其它按键
                switch(key){
                case KeyEvent.VK_RIGHT: moveRightAction(); break;
                case KeyEvent.VK_LEFT: moveLeftAction(); break;
                case KeyEvent.VK_DOWN: softDropAction() ; break;
                case KeyEvent.VK_UP: rotateRightAction() ; break;
                case KeyEvent.VK_Z: rotateLeftAction() ; break;
                case KeyEvent.VK_SPACE: hardDropAction() ; break;
                case KeyEvent.VK_P: pauseAction() ; break;
                }
                repaint();
            }
        };
        this.requestFocus();
        this.addKeyListener(l);
    }

    public void paint(Graphics g){
        g.drawImage(background, 0, 0, null);//使用this 作为观察者
        g.translate(15, 15);//平移绘图坐标系
        paintTetromino(g);//绘制正在下落的方块
        paintWall(g);//画墙
        paintNextOne(g);
        paintScore(g);
    }
    public static final int FONT_COLOR = 0x667799;
    public static final int FONT_SIZE = 0x20;
    private void paintScore(Graphics g) {
        Font f = getFont();//获取当前的 面板默认字体
        Font font = new Font(
                f.getName(), Font.BOLD, FONT_SIZE);
        int x = 290;
        int y = 162;
        g.setColor(new Color(FONT_COLOR));
        g.setFont(font);
        String str = "SCORE:"+this.score;
        g.drawString(str, x, y);
        y+=56;
        str = "LINES:"+this.lines;
        g.drawString(str, x, y);
        y+=56;
        str = "[P]Pause";
        if(pause){str = "[C]Continue";}
        if(gameOver){    str = "[S]Start!";}
        g.drawString(str, x, y);
    }

    private void paintNextOne(Graphics g) {
        Cell[] cells = nextOne.getCells();
        for(int i=0; i=1; i--){
            //复制 [i-1] -> [i]
            System.arraycopy(wall[i-1], 0, wall[i], 0, COLS);
        }
        Arrays.fill(wall[0], null);
    }

    /** 检查当前的4格方块能否继续下落 */
    public boolean tetrominoCanDrop(){
        Cell[] cells = tetromino.getCells();
        for(int i = 0; i=COLS){
                return true;//出界了
            }
        }
        return false;
    }
    private boolean coincide(){
        Cell[] cells = tetromino.getCells();
        //for each 循环、迭代,简化了"数组迭代书写"
        for(Cell cell: cells){//Java 5 以后提供增强版for循环
            int row = cell.getRow();
            int col = cell.getCol();
            if(row<0 || row>=ROWS || col<0 || col>=COLS ||
                    wall[row][col]!=null){
                return true; //墙上有格子对象,发生重合
            }
        }
        return false;
    }
    /** 向右旋转动作 */
    public void rotateRightAction(){
        //旋转之前
        //System.out.println(tetromino);
        tetromino.rotateRight();
        //System.out.println(tetromino);
        //旋转之后
        if(outOfBound() || coincide()){
            tetromino.rotateLeft();
        }
    }

    /** Tetris 类中添加的方法 */
    public void rotateLeftAction(){
        tetromino.rotateLeft();
        if(outOfBound() || coincide()){
            tetromino.rotateRight();
        }
    }
    public void hardDropAction(){
        while(tetrominoCanDrop()){
            tetromino.softDrop();
        }
        tetrominoLandToWall();
        destroyLines();
        checkGameOver();
        tetromino = nextOne;
        nextOne = Tetromino.randomTetromino();
    }

    private boolean pause;
    private boolean gameOver;
    private Timer timer;
    /** Tetris 类中添加的方法, 用于启动游戏 */
    public void startAction(){
        clearWall();
        tetromino = Tetromino.randomTetromino();
        nextOne = Tetromino.randomTetromino();
        lines = 0; score = 0;    pause=false; gameOver=false;
        timer = new Timer();
        timer.schedule(new TimerTask(){
            public void run() {
                softDropAction();
                repaint();
            }
        }, 700, 700);
    }

    private void clearWall(){
        //将墙的每一行的每个格子清理为null
        for(int row=0; row

 

二、Cell.java

package com.fry.tetris;

import java.awt.Image;

/**
 * 格子
 * 每一个小格子,就有所在的行 列 和图片
 */
public class Cell {
    private int row;
    private int col;
    //private int color;
    private Image image;//格子的贴图

    public Cell() {
    }

    public Cell(int row, int col, Image image) {
        super();
        this.row = row;
        this.col = col;
        this.image = image;
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getCol() {
        return col;
    }

    public void setCol(int col) {
        this.col = col;
    }


    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public void moveRight(){
        col++;
        //System.out.println("Cell moveRight()" + col);
    }

    public void moveLeft(){
        col--;
    }

    public void moveDown(){
        row++;
    }

    @Override
    public String toString() {
        return "["+row+","+col+"]";
    }
}

三、功能实现 Tetromino.java

package com.fry.tetris;

import java.util.Arrays;
import java.util.Random;

/**
 * 4格方块 
 */
public class Tetromino {
    protected Cell[] cells = new Cell[4];
    /** 保存旋转的相对于轴位置状态 */
    protected State[] states;
    
    /** 随机生成 4格方块, 使用简单工厂方法模式! 
     * randomTetromino 随机生成一个四格方块 
     * 这个方面的返回值是多态的!
     * */
    public static Tetromino randomTetromino(){
        Random r = new Random();
        int type = r.nextInt(7);
        switch(type){
        case 0: return new T();
        case 1: return new I();
        case 2: return new J();
        case 3: return new L();
        case 4: return new O();
        case 5: return new S();
        case 6: return new Z();
        }
        return null;
    }
    
    public Cell[] getCells() {
        return cells;
    }

    /** 下落 */
    public void softDrop(){
        for(int i=0; i

 

你可能感兴趣的:(java学习交流,java,java,游戏,eclipse,java俄罗斯方块,Java基础)