Java 小游戏开发之俄罗斯方块

 更新时间:2017年07月22日 10:13:35   投稿:lqh  
这篇文章主要介绍了Java 小游戏开发之俄罗斯方块的相关资料,这里实现俄罗斯方块的实例和实现效果给大家看下,学习java基础的朋友的好资料,需要的朋友可以参考下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

Java项目 俄罗斯方块

一、心得

二、游戏实例

游戏截图

目录结构

三、代码

1、主界面 Tetris.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 )};
  }
}

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

相关文章

  • 详解Java设计模式之备忘录模式的使用

    详解Java设计模式之备忘录模式的使用

    这篇文章主要介绍了Java设计模式之备忘录模式的使用,备忘录模式中的发起者和管需要的朋友可以参考下
    2016-02-02
  • 从零开始让你的Spring?Boot项目跑在Linux服务器

    从零开始让你的Spring?Boot项目跑在Linux服务器

    这篇文章主要给大家介绍了如何从零开始让你的Spring?Boot项目跑在Linux服务器的相关资料,由于springboot是内嵌了tomcat,所以可以直接将项目打包上传至服务器上,需要的朋友可以参考下
    2021-11-11
  • 关于IDEA的junit单元测试Scanner输入不可用的问题(多种原因分析)

    关于IDEA的junit单元测试Scanner输入不可用的问题(多种原因分析)

    IDEA在Junit测试中使用了Scanner,但是控制台一直转圈,无法输入,eclipse默认可用,其实解决方法很多,今天小编给大家分享多种问题原因分析及解决方法,感兴趣的朋友一起看看吧
    2021-06-06
  • sprng和struts有什么区别?

    sprng和struts有什么区别?

    Spring和Struts都是近年来比较流行的框架,Struts主要用于表示层,Spring用于业务层,以及Hiberate主要用于持久层,
    2015-06-06
  • 利用Log4j将不同Package的日志输出到不同文件的方法

    利用Log4j将不同Package的日志输出到不同文件的方法

    日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方便的日志记录。这篇文章主要介绍了利用Log4j将不同Package的日志输出到不同文件的方法,需要的朋友可以参考借鉴,下面来跟着小编一起学习学习吧。
    2017-01-01
  • Java 继承与多态的深入理解

    Java 继承与多态的深入理解

    这篇文章主要介绍了Java 继承与多态的深入理解的相关资料,子类继承父类的特征和行为,使得子类具有父类的各种属性和方法。或子类从父类继承方法,使得子类具有父类相同的行为,需要的朋友可以参考下
    2017-08-08
  • java实现统计字符串中大写字母,小写字母及数字出现次数的方法示例

    java实现统计字符串中大写字母,小写字母及数字出现次数的方法示例

    这篇文章主要介绍了java实现统计字符串中大写字母,小写字母及数字出现次数的方法,涉及java针对字符串的遍历、判断、运算相关操作技巧,需要的朋友可以参考下
    2019-06-06
  • Java中缀表达式转后缀表达式流程详解

    Java中缀表达式转后缀表达式流程详解

    中缀表达式是一个通用的算术或逻辑公式表示方法。,中缀表达式不容易被计算机解析,但仍被许多程序语言使用,因为它符合人们的普遍用法。本文介绍了实现中缀表达式的方法,需要的可以参考一下
    2022-09-09
  • LeetCode程序员面试题之无重复字符的最长子串

    LeetCode程序员面试题之无重复字符的最长子串

    Java计算无重复字符的最长子串是一种常见的字符串处理算法,它的目的是找出一个字符串中无重复字符的最长子串。该算法可以很好地解决一些字符串处理问题,比如寻找字符串中重复字符的位置,以及计算字符串中无重复字符的最长子串的长度。
    2023-02-02
  • java实现微信点餐申请微信退款

    java实现微信点餐申请微信退款

    这篇文章主要为大家详细介绍了java实现微信点餐申请微信退款,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-09-09

最新评论


http://www.vxiaotou.com