码迷,mamicode.com
首页 > 编程语言 > 详细

Java项目--俄罗斯方块

时间:2017-07-22 10:59:05      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:drop   添加   row   arrays   extends   工厂方法   功能   ati   tin   

Java项目--俄罗斯方块

一、心得

二、游戏实例

游戏截图

技术分享

技术分享

技术分享

目录结构

技术分享

三、代码

1、主界面 Tetris.java

  1 package com.fry.tetris;
  2 
  3 import java.util.Arrays;
  4 import java.util.Random;
  5 
  6 /**
  7  * 4格方块 
  8  */
  9 public class Tetromino {
 10     protected Cell[] cells = new Cell[4];
 11     /** 保存旋转的相对于轴位置状态 */
 12     protected State[] states;
 13     
 14     /** 随机生成 4格方块, 使用简单工厂方法模式! 
 15      * randomTetromino 随机生成一个四格方块 
 16      * 这个方面的返回值是多态的!
 17      * */
 18     public static Tetromino randomTetromino(){
 19         Random r = new Random();
 20         int type = r.nextInt(7);
 21         switch(type){
 22         case 0: return new T();
 23         case 1: return new I();
 24         case 2: return new J();
 25         case 3: return new L();
 26         case 4: return new O();
 27         case 5: return new S();
 28         case 6: return new Z();
 29         }
 30         return null;
 31     }
 32     
 33     public Cell[] getCells() {
 34         return cells;
 35     }
 36 
 37     /** 下落 */
 38     public void softDrop(){
 39         for(int i=0; i<cells.length; i++){
 40             cells[i].moveDown();
 41         }
 42     }
 43     public void moveRight(){
 44         //System.out.println("moveRight()");
 45         for(int i=0; i<cells.length; i++){
 46             this.cells[i].moveRight();
 47         }
 48     } 
 49     public void moveLeft(){
 50         for(int i=0; i<cells.length; i++){
 51             cells[i].moveLeft();
 52         }
 53     }
 54     private int index = 100000;
 55     /** 在 Tetromino 上添加方法  */
 56     public void rotateRight() {
 57         index++;//index = 10001
 58         // index % states.length = 10001 % 4 = 1
 59         State s = states[index%states.length];//s1
 60         // [0] + s1 = [1]
 61         Cell o = cells[0];//获取当前的轴
 62         //轴与相对位置的和作为旋转以后的格子位置
 63         cells[1].setRow(o.getRow()+s.row1);
 64         cells[1].setCol(o.getCol()+s.col1);
 65         cells[2].setRow(o.getRow()+s.row2);
 66         cells[2].setCol(o.getCol()+s.col2);
 67         cells[3].setRow(o.getRow()+s.row3);
 68         cells[3].setCol(o.getCol()+s.col3);
 69     }
 70     /** 在 Tetromino 上添加方法  */
 71     public void rotateLeft() {
 72         index--;//index = 10001
 73         // index % states.length = 10001 % 4 = 1
 74         State s = states[index%states.length];//s1
 75         // [0] + s1 = [1]
 76         Cell o = cells[0];//获取当前的轴
 77         cells[1].setRow(o.getRow()+s.row1);
 78         cells[1].setCol(o.getCol()+s.col1);
 79         cells[2].setRow(o.getRow()+s.row2);
 80         cells[2].setCol(o.getCol()+s.col2);
 81         cells[3].setRow(o.getRow()+s.row3);
 82         cells[3].setCol(o.getCol()+s.col3);
 83     }
 84     
 85     @Override
 86     public String toString() {
 87         return Arrays.toString(cells); 
 88     }
 89     
 90     /** Tetromino 类中添加的 内部类 用于记录旋转状态 */
 91     protected class State{
 92         int row0,col0,row1,col1,row2,col2,row3,col3;
 93 
 94         public State(int row0, int col0, int row1, int col1,
 95                 int row2, int col2,
 96                 int row3, int col3) {
 97             this.row0 = row0;
 98             this.col0 = col0;
 99             this.row1 = row1;
100             this.col1 = col1;
101             this.row2 = row2;
102             this.col2 = col2;
103             this.row3 = row3;
104             this.col3 = col3;
105         }      
106     }
107     
108 }//Tetromino 类的结束
109 class T extends Tetromino{
110     public T() {
111         cells[0] = new Cell(0, 4, Tetris.T);
112         cells[1] = new Cell(0, 3, Tetris.T);
113         cells[2] = new Cell(0, 5, Tetris.T);
114         cells[3] = new Cell(1, 4, Tetris.T);
115         states = new State[]{
116                 new State(0,0, 0,-1, 0,1, 1, 0),
117                 new State(0,0, -1,0, 1,0, 0,-1),
118                 new State(0,0, 0,1,  0,-1, -1,0),
119                 new State(0,0, 1,0, -1,0, 0,1)};
120     }
121 }
122 class I extends Tetromino{
123     public I() {
124         cells[0] = new Cell(0, 4, Tetris.I);
125         cells[1] = new Cell(0, 3, Tetris.I);
126         cells[2] = new Cell(0, 5, Tetris.I);
127         cells[3] = new Cell(0, 6, Tetris.I);
128         states = new State[]{
129                 new State(0,0, 0,1, 0,-1, 0,-2),
130                 new State(0,0, -1,0, 1,0,2,0)};
131     }
132 }
133 class L extends Tetromino {
134     public L() {
135         cells[0] = new Cell(0, 4, Tetris.L);
136         cells[1] = new Cell(0, 3, Tetris.L);
137         cells[2] = new Cell(0, 5, Tetris.L);
138         cells[3] = new Cell(1, 3, Tetris.L);
139         states = new State[]{
140                 new State(0,0, 0,-1, 0,1, 1,-1 ),
141                 new State(0,0, -1,0, 1,0, -1,-1),
142                 new State(0,0, 0,1, 0,-1, -1,1),
143                 new State(0,0, 1,0, -1,0, 1,1)};    
144     }
145 }
146 
147 class J extends Tetromino {
148     public J() {
149         cells[0] = new Cell(0, 4, Tetris.J);
150         cells[1] = new Cell(0, 3, Tetris.J);
151         cells[2] = new Cell(0, 5, Tetris.J);
152         cells[3] = new Cell(1, 5, Tetris.J);
153         states = new State[]{
154                 new State(0,0, 0,-1, 0,1, 1,1),
155                 new State(0,0, -1,0, 1,0, 1,-1),
156                 new State(0,0, 0,1, 0,-1, -1,-1),
157                 new State(0,0, 1,0, -1,0, -1,1 )};
158     }
159 }
160 
161 class S extends Tetromino {
162     public S() {
163         cells[0] = new Cell(0, 4, Tetris.S);
164         cells[1] = new Cell(0, 5, Tetris.S);
165         cells[2] = new Cell(1, 3, Tetris.S);
166         cells[3] = new Cell(1, 4, Tetris.S);
167         states = new State[]{
168             new State(0,0, 0,1, 1,-1, 1,0 ),
169             new State(0,0, -1,0, 1,1, 0,1 )};
170     }
171 }
172 
173 class Z extends Tetromino {
174     public Z() {
175         cells[0] = new Cell(1, 4, Tetris.Z);
176         cells[1] = new Cell(0, 3, Tetris.Z);
177         cells[2] = new Cell(0, 4, Tetris.Z);
178         cells[3] = new Cell(1, 5, Tetris.Z);
179         states = new State[]{
180                 new State(0,0, -1,-1, -1,0, 0,1 ),
181                 new State(0,0, -1,1, 0,1, 1,0 )};
182     }
183 }
184 
185 class O extends Tetromino {
186     public O() {
187         cells[0] = new Cell(0, 4, Tetris.O);
188         cells[1] = new Cell(0, 5, Tetris.O);
189         cells[2] = new Cell(1, 4, Tetris.O);
190         cells[3] = new Cell(1, 5, Tetris.O);
191         states = new State[]{
192                 new State(0,0, 0,1, 1,0, 1,1 ),
193                 new State(0,0, 0,1, 1,0, 1,1 )};
194     }
195 }

二、Cell.java

 1 package com.fry.tetris;
 2 
 3 import java.awt.Image;
 4 
 5 /**
 6  * 格子
 7  * 每一个小格子,就有所在的行 列 和图片 
 8  */
 9 public class Cell {
10     private int row;
11     private int col;
12     //private int color;
13     private Image image;//格子的贴图
14     
15     public Cell() {
16     }
17 
18     public Cell(int row, int col, Image image) {
19         super();
20         this.row = row;
21         this.col = col;
22         this.image = image;
23     }
24 
25     public int getRow() {
26         return row;
27     }
28 
29     public void setRow(int row) {
30         this.row = row;
31     }
32 
33     public int getCol() {
34         return col;
35     }
36 
37     public void setCol(int col) {
38         this.col = col;
39     }
40     
41     
42     public Image getImage() {
43         return image;
44     }
45 
46     public void setImage(Image image) {
47         this.image = image;
48     }
49 
50     public void moveRight(){
51         col++;
52         //System.out.println("Cell moveRight()" + col); 
53     }
54     
55     public void moveLeft(){
56         col--;
57     }
58     
59     public void moveDown(){
60         row++;
61     }
62     
63     @Override
64     public String toString() {
65         return "["+row+","+col+"]";
66     }
67 }

三、功能实现 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<cells.length; i++){
            cells[i].moveDown();
        }
    }
    public void moveRight(){
        //System.out.println("moveRight()");
        for(int i=0; i<cells.length; i++){
            this.cells[i].moveRight();
        }
    } 
    public void moveLeft(){
        for(int i=0; i<cells.length; i++){
            cells[i].moveLeft();
        }
    }
    private int index = 100000;
    /** 在 Tetromino 上添加方法  */
    public void rotateRight() {
        index++;//index = 10001
        // index % states.length = 10001 % 4 = 1
        State s = states[index%states.length];//s1
        // [0] + s1 = [1]
        Cell o = cells[0];//获取当前的轴
        //轴与相对位置的和作为旋转以后的格子位置
        cells[1].setRow(o.getRow()+s.row1);
        cells[1].setCol(o.getCol()+s.col1);
        cells[2].setRow(o.getRow()+s.row2);
        cells[2].setCol(o.getCol()+s.col2);
        cells[3].setRow(o.getRow()+s.row3);
        cells[3].setCol(o.getCol()+s.col3);
    }
    /** 在 Tetromino 上添加方法  */
    public void rotateLeft() {
        index--;//index = 10001
        // index % states.length = 10001 % 4 = 1
        State s = states[index%states.length];//s1
        // [0] + s1 = [1]
        Cell o = cells[0];//获取当前的轴
        cells[1].setRow(o.getRow()+s.row1);
        cells[1].setCol(o.getCol()+s.col1);
        cells[2].setRow(o.getRow()+s.row2);
        cells[2].setCol(o.getCol()+s.col2);
        cells[3].setRow(o.getRow()+s.row3);
        cells[3].setCol(o.getCol()+s.col3);
    }
    
    @Override
    public String toString() {
        return Arrays.toString(cells); 
    }
    
    /** Tetromino 类中添加的 内部类 用于记录旋转状态 */
    protected class State{
        int row0,col0,row1,col1,row2,col2,row3,col3;

        public State(int row0, int col0, int row1, int col1,
                int row2, int col2,
                int row3, int col3) {
            this.row0 = row0;
            this.col0 = col0;
            this.row1 = row1;
            this.col1 = col1;
            this.row2 = row2;
            this.col2 = col2;
            this.row3 = row3;
            this.col3 = col3;
        }      
    }
    
}//Tetromino 类的结束
class T extends Tetromino{
    public T() {
        cells[0] = new Cell(0, 4, Tetris.T);
        cells[1] = new Cell(0, 3, Tetris.T);
        cells[2] = new Cell(0, 5, Tetris.T);
        cells[3] = new Cell(1, 4, Tetris.T);
        states = new State[]{
                new State(0,0, 0,-1, 0,1, 1, 0),
                new State(0,0, -1,0, 1,0, 0,-1),
                new State(0,0, 0,1,  0,-1, -1,0),
                new State(0,0, 1,0, -1,0, 0,1)};
    }
}
class I extends Tetromino{
    public I() {
        cells[0] = new Cell(0, 4, Tetris.I);
        cells[1] = new Cell(0, 3, Tetris.I);
        cells[2] = new Cell(0, 5, Tetris.I);
        cells[3] = new Cell(0, 6, Tetris.I);
        states = new State[]{
                new State(0,0, 0,1, 0,-1, 0,-2),
                new State(0,0, -1,0, 1,0,2,0)};
    }
}
class L extends Tetromino {
    public L() {
        cells[0] = new Cell(0, 4, Tetris.L);
        cells[1] = new Cell(0, 3, Tetris.L);
        cells[2] = new Cell(0, 5, Tetris.L);
        cells[3] = new Cell(1, 3, Tetris.L);
        states = new State[]{
                new State(0,0, 0,-1, 0,1, 1,-1 ),
                new State(0,0, -1,0, 1,0, -1,-1),
                new State(0,0, 0,1, 0,-1, -1,1),
                new State(0,0, 1,0, -1,0, 1,1)};    
    }
}

class J extends Tetromino {
    public J() {
        cells[0] = new Cell(0, 4, Tetris.J);
        cells[1] = new Cell(0, 3, Tetris.J);
        cells[2] = new Cell(0, 5, Tetris.J);
        cells[3] = new Cell(1, 5, Tetris.J);
        states = new State[]{
                new State(0,0, 0,-1, 0,1, 1,1),
                new State(0,0, -1,0, 1,0, 1,-1),
                new State(0,0, 0,1, 0,-1, -1,-1),
                new State(0,0, 1,0, -1,0, -1,1 )};
    }
}

class S extends Tetromino {
    public S() {
        cells[0] = new Cell(0, 4, Tetris.S);
        cells[1] = new Cell(0, 5, Tetris.S);
        cells[2] = new Cell(1, 3, Tetris.S);
        cells[3] = new Cell(1, 4, Tetris.S);
        states = new State[]{
            new State(0,0, 0,1, 1,-1, 1,0 ),
            new State(0,0, -1,0, 1,1, 0,1 )};
    }
}

class Z extends Tetromino {
    public Z() {
        cells[0] = new Cell(1, 4, Tetris.Z);
        cells[1] = new Cell(0, 3, Tetris.Z);
        cells[2] = new Cell(0, 4, Tetris.Z);
        cells[3] = new Cell(1, 5, Tetris.Z);
        states = new State[]{
                new State(0,0, -1,-1, -1,0, 0,1 ),
                new State(0,0, -1,1, 0,1, 1,0 )};
    }
}

class O extends Tetromino {
    public O() {
        cells[0] = new Cell(0, 4, Tetris.O);
        cells[1] = new Cell(0, 5, Tetris.O);
        cells[2] = new Cell(1, 4, Tetris.O);
        cells[3] = new Cell(1, 5, Tetris.O);
        states = new State[]{
                new State(0,0, 0,1, 1,0, 1,1 ),
                new State(0,0, 0,1, 1,0, 1,1 )};
    }
}

 

Java项目--俄罗斯方块

标签:drop   添加   row   arrays   extends   工厂方法   功能   ati   tin   

原文地址:http://www.cnblogs.com/Renyi-Fan/p/7220327.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!