亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java 小游戲開發之俄羅斯方塊

發布時間:2020-09-02 20:05:07 來源:腳本之家 閱讀:144 作者:lqh 欄目:編程語言

Java項目 俄羅斯方塊

一、心得

二、游戲實例

游戲截圖

Java 小游戲開發之俄羅斯方塊

Java 小游戲開發之俄羅斯方塊

Java 小游戲開發之俄羅斯方塊

目錄結構

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實現俄羅斯方塊的實例,如有疑問請留言或者到本站社區討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

通许县| 乌审旗| 安溪县| 洪湖市| 平度市| 武安市| 马边| 罗山县| 德化县| 汉阴县| 河间市| 洛阳市| 县级市| 永平县| 平谷区| 井冈山市| 吉隆县| 瓦房店市| 江北区| 江川县| 南投市| 巴彦淖尔市| 玉山县| 洛宁县| 汕尾市| 阳信县| 微山县| 林口县| 托克托县| 元氏县| 库车县| 杂多县| 兴义市| 遂川县| 军事| 汝南县| 平定县| 咸丰县| 英超| 鹤壁市| 绥宁县|