欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-22 08:41:22
java游戏俄罗斯方块的实现实例          java小游戏主要理解应用java s...

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

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

实现代码:

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 俄罗斯方块的详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!