国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java 小游戲開發(fā)之俄羅斯方塊

Java 小游戲開發(fā)之俄羅斯方塊

2020-12-05 16:51Renyi-Fan Java教程

這篇文章主要介紹了Java 小游戲開發(fā)之俄羅斯方塊的相關資料,這里實現俄羅斯方塊的實例和實現效果給大家看下,學習java基礎的朋友的好資料,需要的朋友可以參考下

java項目 俄羅斯方塊

一、心得

二、游戲實例

游戲截圖

Java 小游戲開發(fā)之俄羅斯方塊

Java 小游戲開發(fā)之俄羅斯方塊

Java 小游戲開發(fā)之俄羅斯方塊

目錄結構

Java 小游戲開發(fā)之俄羅斯方塊

三、代碼

1、主界面 tetris.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.fry.tetris;
 
import java.util.arrays;
import java.util.random;
 
/**
 * 4格方塊
 */
public class tetromino {
  protected cell[] cells = new cell[4];
  /** 保存旋轉的相對于軸位置狀態(tài) */
  protected state[] states;
  
  /** 隨機生成 4格方塊, 使用簡單工廠方法模式!
   * randomtetromino 隨機生成一個四格方塊
   * 這個方面的返回值是多態(tài)的!
   * */
  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 類中添加的 內部類 用于記錄旋轉狀態(tài) */
  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 )};
  }
}

二、cell.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.fry.tetris;
 
import java.util.arrays;
import java.util.random;
 
/**
 * 4格方塊
 */
public class tetromino {
  protected cell[] cells = new cell[4];
  /** 保存旋轉的相對于軸位置狀態(tài) */
  protected state[] states;
  
  /** 隨機生成 4格方塊, 使用簡單工廠方法模式!
   * randomtetromino 隨機生成一個四格方塊
   * 這個方面的返回值是多態(tài)的!
   * */
  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 類中添加的 內部類 用于記錄旋轉狀態(tài) */
  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實現俄羅斯方塊的實例,如有疑問請留言或者到本站社區(qū)討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:http://www.cnblogs.com/Renyi-Fan/p/7220327.html

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 国产中文字幕观看 | 成人精品一区亚洲午夜久久久 | 欧美三级在线 | 午夜播放器在线观看 | 欧美在线综合 | 亚洲国产精品久久人人爱 | 国产日韩欧美视频 | 亚州中文字幕 | 久草成人网 | 欧美午夜影院 | 一本色道精品久久一区二区三区 | 久久综合九九 | 亚洲电影在线 | 九九九在线 | 欧美黄色小视频 | 亚洲天堂一区二区 | 私人毛片免费高清视频 | 成人精品视频99在线观看免费 | 欧美视频一二三区 | 国产精品一区久久 | 午夜剧场在线免费观看 | 91麻豆精品国产91久久久更新资源速度超快 | 一本a道v久大 | 欧美日韩国产在线观看 | 亚洲综合在线视频 | a级毛片免费高清视频 | 久久精品a一级国产免视看成人 | 欧美淫视频 | 91精品国产91久久综合桃花 | 亚洲精品在线视频 | 五月天婷婷激情 | 国产在线观看91一区二区三区 | 国产精品国产三级国产aⅴ原创 | 天天看夜夜 | 久久综合久久综合久久综合 | 亚洲欧洲av在线 | 亚洲精品一区二区三区蜜桃久 | 亚洲一区视频在线 | 成人免费观看视频大全 | 亚洲视频在线免费观看 | 中文字幕亚洲一区二区三区 |