服务器之家

服务器之家 > 正文

Java游戏俄罗斯方块的实现实例

时间:2020-12-15 14:29     来源/作者:海那边的小萌男

Java游戏俄罗斯方块的实现实例

         java小游戏主要理解应用java Swing,awt等基础组件的知识,通过本例应当掌握面向对象的知识。

实现代码:

?
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package cn.hncu.games;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
 
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
 
public class Tetris extends JFrame {
  private TetrisPanel tp;
 
  public Tetris() {
    // 添加菜单,这里只是意思一下,留待大家自己做更详细的
    // 菜单条
    JMenuBar menubar = new JMenuBar();
    setJMenuBar(menubar);
    // 菜单
    JMenu menuGame = new JMenu("游戏");
    menubar.add(menuGame);
    // 菜单项
    JMenuItem mi1 = new JMenuItem("新游戏");
    mi1.setActionCommand("new");
    JMenuItem mi2 = new JMenuItem("暂停");
    mi2.setActionCommand("pause");
    JMenuItem mi3 = new JMenuItem("继续");
    mi3.setActionCommand("continue");
    JMenuItem mi4 = new JMenuItem("退出");
    mi4.setActionCommand("exit");
 
    menuGame.add(mi1);
    menuGame.add(mi2);
    menuGame.add(mi3);
    menuGame.add(mi4);
 
    //菜单项监听
    MenuListener menuListener = new MenuListener();
    mi1.addActionListener(menuListener);
    mi2.addActionListener(menuListener);
    mi3.addActionListener(menuListener);
    mi4.addActionListener(menuListener);
 
    // 版本菜单
    JMenu menuHelp = new JMenu("帮助");
    menubar.add(menuHelp);
    menuHelp.add("版本所有@湖南城院 QQ:666688888");
 
 
    setLocation(700, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(220, 275);
    setResizable(false);
    tp = new TetrisPanel();
    getContentPane().add(tp);
 
    // 让整个画布添加键盘监听
    // tp.addKeyListener(tp.listener);//不行,画布不方便获得键盘焦点
    this.addKeyListener(tp.listener); // 让框架来监听键盘
  }
 
  public static void main(String[] args) {
    Tetris te = new Tetris();
    te.setVisible(true);
  }
 
  class MenuListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
      if(e.getActionCommand().equalsIgnoreCase("new")){
        getContentPane().remove(tp);
        tp = new TetrisPanel();
        getContentPane().add(tp);
        getContentPane().validate();//校验当前容器,有刷新功能      
      }else if(e.getActionCommand().equalsIgnoreCase("pause")){
        tp.getTimer().stop();
      }else if(e.getActionCommand().equalsIgnoreCase("continue")){
        tp.getTimer().restart();
      }else if(e.getActionCommand().equalsIgnoreCase("exit")){
        System.exit(0);
      }
    }
  }
}
 
class TetrisPanel extends JPanel {
  private int map[][] = new int[13][23];// map[列号][行号]。真正的方块区是:21行*10列。边框(2列,1行)
 
  // 方块的形状:
  // 第一维代表方块类型(包括7种:S、Z、L、J、I、O、T)
  // 第二维代表旋转次数
  // 第三四维代表方块矩阵
  // shapes[type][turnState][i] i--> block[i/4][i%4]
  int shapes[][][] = new int[][][] {
  /*
   * 模板 { {0,0,0,0,0,0,0,0, 0,0,0,0, 0,0,0,0}, {0,0,0,0,0,0,0,0, 0,0,0,0,
   * 0,0,0,0}, {0,0,0,0,0,0,0,0, 0,0,0,0, 0,0,0,0}, {0,0,0,0,0,0,0,0, 0,0,0,0,
   * 0,0,0,0} }
   */
      // I (※把版本1中的横条从第1行换到第2行)
      { { 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, 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 } },
      // Z 第3行: shapes[2][2][]
      { { 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 } } };
 
  private int type;
  private int turnState;
  private int x, y;// 当前块的位置---左上角的坐标
  private int score = 0;
  private Timer timer = null;
  private int delay = 1000;
 
  TimerListener listener = null;
 
  public TetrisPanel() {
    newGame();
    nextBlock();
 
    listener = new TimerListener();
    timer = new Timer(delay, listener);
    timer.start();
 
  }
 
  private void newGame() {
    // 初始化游戏地图
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 21; j++) {
        if (i == 0 || i == 11) {// 边框
          map[i][j] = 3;
        } else {
          map[i][j] = 0;
        }
      }
      map[i][21] = 3;
    }
    score = 0;
  }
 
  private void nextBlock() {
    type = (int) (Math.random() * 1000) % 7; // type=5;
    turnState = (int) (Math.random() * 1000) % 4; // turnState=3;
    x = 4;
    y = 0;
    if (crash(x, y, type, turnState) == 0) {
      timer.stop();
      int op = JOptionPane.showConfirmDialog(null,
          "Game Over!....笨蛋,敢再来一局吗?!");
      if (op == JOptionPane.YES_OPTION) {
        newGame();
      } else if (op == JOptionPane.NO_OPTION) {
        System.exit(0);
      }
    }
  }
 
  private void down() {
    if (crash(x, y + 1, type, turnState) == 0) {// 判断当前块往下落一格后是否和地图存在填充块完全重合---注意实参:y+1
      add(x, y, type, turnState);// 把该块加到地图---形成堆积块
      nextBlock();
    } else {
      y++;
    }
    repaint();
  }
 
  private void left() {
    if (x >= 0) {
      x -= crash(x - 1, y, type, turnState);
    }
    repaint();
  }
 
  private void right() {
    if (x < 8) {
      x += crash(x + 1, y, type, turnState);
    }
    repaint();
  }
 
  private void turn() {
    if (crash(x, y, type, (turnState + 1) % 4) == 1) {
      turnState = (turnState + 1) % 4;
    }
    repaint();
  }
 
  // 让一个块堆积,其实是把当前块中的填充块信息记录到map[][]中
  private void add(int x, int y, int type, int turnState) {
    for (int a = 0; a < 4; a++) {
      for (int b = 0; b < 4; b++) {
        if (shapes[type][turnState][a * 4 + b] == 1) {
          map[x + b + 1][y + a] = 1;
        }
      }
    }
    tryDelLine();
  }
 
  // 消块
  private void tryDelLine() {
    // 从上往下,一行行依次遍历,如果某一行的map[i][j]值全是1,则把这一行消掉---上一行往下落
    for (int b = 0; b < 21; b++) {
      int c = 1;
      for (int a = 0; a < 12; a++) {
        c &= map[a][b];
      }
      if (c == 1) {// 全是1--下落一行
        score += 10;
        for (int d = b; d > 0; d--) {
          for (int e = 0; e < 11; e++) {
            map[e][d] = map[e][d - 1];
          }
        }
 
        // 更改游戏的难度(加快下落速度)
        delay /= 2;
        timer.setDelay(delay);
      }
 
    }
 
  }
 
  private int crash(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] & map[x + b + 1][y
            + a]) == 1) {// 和填充块或框架重合,都算碰撞
          return 0; // 碰撞了---方块的填充块和地图中的填充块完全重合
        }
      }
    }
    return 1;// 没有碰撞
  }
 
  // 表现层
  @Override
  public void paint(Graphics g) {
    super.paint(g);// 清除残影
 
    // 画当前块
    for (int j = 0; j < 16; j++) {
      if (shapes[type][turnState][j] == 1) {
        g.setColor(Color.green);
        g.fillRect((j % 4 + x + 1) * 10, (j / 4 + y) * 10, 10, 10);
      }
    }
 
    /*
     * for(int a=0;a<4;a++){ for(int b=0;b<4;b++){
     * if(shapes[type][turnState][a*4+b]==1){ g.fillRect((b+x+1)*10,
     * (a+y)*10, 10, 10); } } }
     */
 
    // 画地图(整个游戏的方块区和边框)
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 22; j++) {
        if (map[i][j] == 1) {
          g.setColor(Color.red);
          g.fillRect(i * 10, j * 10, 10, 10);// 填充
 
          g.setColor(Color.yellow);
          g.drawRect(i * 10, j * 10, 10, 10);// 格线
        } else if (map[i][j] == 3) {
          g.setColor(Color.red);
          g.drawRect(i * 10, j * 10, 10, 10);
        }
      }
    }
 
    // 显示分数,同时为版面美观,在界面上再加点东西
    // 画方块区右侧部分
    g.setColor(Color.blue);
    g.setFont(new Font("aa", Font.BOLD, 18));
    g.drawString("score=" + score, 130, 20);
 
    g.setFont(new Font("aa", Font.PLAIN, 13));
    g.drawString("拒绝盗版游戏", 130, 70);
    g.drawString("注意自我保护", 130, 90);
    g.drawString("谨防受骗上当。", 125, 110);
    g.drawString("适度游戏益脑,", 125, 130);
    g.drawString("沉迷游戏伤身。", 125, 150);
    g.drawString("合理安排时间,", 125, 170);
    g.drawString("享受健康生活。", 125, 190);
 
  }
 
  class TimerListener extends KeyAdapter implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
      down();
    }
 
    @Override
    public void keyPressed(KeyEvent e) {
      // System.out.println("aaaaa");
      switch (e.getKeyCode()) {
      case KeyEvent.VK_DOWN:
        down();
        break;
      case KeyEvent.VK_LEFT:
        left();
        break;
      case KeyEvent.VK_RIGHT:
        right();
        break;
      case KeyEvent.VK_UP:
        turn();
      }
    }
 
  }
 
  public Timer getTimer() {
    return timer;
  }
}

以上就是Java 俄罗斯方块的详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/weixin_37720904/article/details/71743195

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部