最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年07月22日 10:13:35   投稿:lqh  
這篇文章主要介紹了Java 小游戲開(kāi)發(fā)之俄羅斯方塊的相關(guān)資料,這里實(shí)現(xiàn)俄羅斯方塊的實(shí)例和實(shí)現(xiàn)效果給大家看下,學(xué)習(xí)java基礎(chǔ)的朋友的好資料,需要的朋友可以參考下

Java項(xiàng)目 俄羅斯方塊

一、心得

二、游戲?qū)嵗?/strong>

游戲截圖

目錄結(jié)構(gòu)

三、代碼

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];
  /** 保存旋轉(zhuǎn)的相對(duì)于軸位置狀態(tài) */
  protected State[] states;
  
  /** 隨機(jī)生成 4格方塊, 使用簡(jiǎn)單工廠方法模式! 
   * randomTetromino 隨機(jī)生成一個(gè)四格方塊 
   * 這個(gè)方面的返回值是多態(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];//獲取當(dāng)前的軸
    //軸與相對(duì)位置的和作為旋轉(zhuǎn)以后的格子位置
    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];//獲取當(dāng)前的軸
    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 類(lèi)中添加的 內(nèi)部類(lèi) 用于記錄旋轉(zhuǎn)狀態(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 類(lèi)的結(jié)束
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;

/**
 * 格子
 * 每一個(gè)小格子,就有所在的行 列 和圖片 
 */
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+"]";
  }
}

三、功能實(shí)現(xiàn) Tetromino.java

package com.fry.tetris;

import java.util.Arrays;
import java.util.Random;

/**
 * 4格方塊 
 */
public class Tetromino {
  protected Cell[] cells = new Cell[4];
  /** 保存旋轉(zhuǎn)的相對(duì)于軸位置狀態(tài) */
  protected State[] states;
  
  /** 隨機(jī)生成 4格方塊, 使用簡(jiǎn)單工廠方法模式! 
   * randomTetromino 隨機(jī)生成一個(gè)四格方塊 
   * 這個(gè)方面的返回值是多態(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];//獲取當(dāng)前的軸
    //軸與相對(duì)位置的和作為旋轉(zhuǎn)以后的格子位置
    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];//獲取當(dāng)前的軸
    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 類(lèi)中添加的 內(nèi)部類(lèi) 用于記錄旋轉(zhuǎn)狀態(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 類(lèi)的結(jié)束
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實(shí)現(xiàn)俄羅斯方塊的實(shí)例,如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • java 教你如何給你的頭像添加一個(gè)好看的國(guó)旗

    java 教你如何給你的頭像添加一個(gè)好看的國(guó)旗

    這篇文章主要介紹了java 教你如何給你的頭像添加一個(gè)好看的國(guó)旗,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Spring?Boot?調(diào)用外部接口的幾種方式

    Spring?Boot?調(diào)用外部接口的幾種方式

    在微服務(wù)架構(gòu)中,服務(wù)間的調(diào)用是不可或缺的環(huán)節(jié),本文主要介紹了Spring?Boot調(diào)用外部接口的幾種方式,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-05-05
  • SpringBoot集成slf4j2日志配置的實(shí)現(xiàn)示例

    SpringBoot集成slf4j2日志配置的實(shí)現(xiàn)示例

    本文主要介紹了SpringBoot集成slf4j2日志配置的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • 淺析Java編程中類(lèi)和對(duì)象的定義

    淺析Java編程中類(lèi)和對(duì)象的定義

    下面小編就為大家?guī)?lái)一篇淺析Java編程中類(lèi)和對(duì)象的定義。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,祝大家游戲愉快哦
    2016-05-05
  • 阿里的Easyexcel讀取Excel文件的方法(最新版本)

    阿里的Easyexcel讀取Excel文件的方法(最新版本)

    這篇文章主要介紹了阿里的Easyexcel讀取Excel文件(最新版本)的方法,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12
  • java使用定時(shí)器實(shí)現(xiàn)監(jiān)聽(tīng)數(shù)據(jù)變化

    java使用定時(shí)器實(shí)現(xiàn)監(jiān)聽(tīng)數(shù)據(jù)變化

    這篇文章主要為大家詳細(xì)介紹了Java如何使用定時(shí)器監(jiān)聽(tīng)數(shù)據(jù)變化,當(dāng)滿足某個(gè)條件時(shí)(例如沒(méi)有數(shù)據(jù)更新)自動(dòng)執(zhí)行某項(xiàng)任務(wù),有興趣的可以了解下
    2023-11-11
  • iReport使用教程(示例教程)

    iReport使用教程(示例教程)

    在使用ireport的過(guò)程中,因?yàn)楦鞣N功能都要百度,但是大家使用的例子又千差萬(wàn)別讓人很苦惱,所以用一個(gè)簡(jiǎn)單例子貫穿的展示一下ireport的常見(jiàn)功能
    2021-10-10
  • 網(wǎng)關(guān)Spring Cloud Gateway HTTP超時(shí)配置問(wèn)題

    網(wǎng)關(guān)Spring Cloud Gateway HTTP超時(shí)配置問(wèn)題

    這篇文章主要介紹了網(wǎng)關(guān)Spring Cloud Gateway HTTP超時(shí)配置問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • spring?boot常見(jiàn)get?、post請(qǐng)求參數(shù)處理、參數(shù)注解校驗(yàn)、參數(shù)自定義注解校驗(yàn)問(wèn)題解析

    spring?boot常見(jiàn)get?、post請(qǐng)求參數(shù)處理、參數(shù)注解校驗(yàn)、參數(shù)自定義注解校驗(yàn)問(wèn)題解析

    這篇文章主要介紹了spring?boot常見(jiàn)get?、post請(qǐng)求參數(shù)處理、參數(shù)注解校驗(yàn)、參數(shù)自定義注解校驗(yàn),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • Java 爬蟲(chóng)服務(wù)器被屏蔽的解決方案

    Java 爬蟲(chóng)服務(wù)器被屏蔽的解決方案

    這篇文章主要介紹了Java 爬蟲(chóng)服務(wù)器被屏蔽的解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10

最新評(píng)論

蕲春县| 柳林县| 林芝县| 辽宁省| 张家口市| 渑池县| 鱼台县| 桐梓县| 宜章县| 汝州市| 霞浦县| 寻乌县| 德钦县| 平顶山市| 双柏县| 新建县| 阳春市| 高淳县| 永川市| 梁山县| 华宁县| 平原县| 彩票| 丰顺县| 平顺县| 雅江县| 嵩明县| 伽师县| 河曲县| 凤山市| 海盐县| 玉环县| 临武县| 阆中市| 泾阳县| 元朗区| 临沂市| 武乡县| 惠水县| 安图县| 巧家县|