検索
ホームページJava&#&チュートリアルJava はテトリス ミニ ゲームのグラフィックとテキストのコード共有を実装します

この記事では、Java ミニゲーム開発におけるテトリスの関連情報を主に紹介します。ここでは、Java の基本を学習している友人に向けて、テトリスの実装効果について説明します。参考にしてください

Javaプロジェクトテトリス

1. 体験

2. ゲーム例

ゲームスクリーンショット

3. コード

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 )};
  }
}

2. 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+"]";
  }
}

3. 関数実装 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 はテトリス ミニ ゲームのグラフィックとテキストのコード共有を実装しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
Javaのプラットフォームの独立性を脅かしたり強化したりする新しいテクノロジーはありますか?Javaのプラットフォームの独立性を脅かしたり強化したりする新しいテクノロジーはありますか?Apr 24, 2025 am 12:11 AM

新しいテクノロジーは、両方の脅威をもたらし、Javaのプラットフォームの独立性を高めます。 1)Dockerなどのクラウドコンピューティングとコンテナ化テクノロジーは、Javaのプラットフォームの独立性を強化しますが、さまざまなクラウド環境に適応するために最適化する必要があります。 2)WebAssemblyは、Graalvmを介してJavaコードをコンパイルし、プラットフォームの独立性を拡張しますが、パフォーマンスのために他の言語と競合する必要があります。

JVMのさまざまな実装は何ですか、そしてそれらはすべて同じレベルのプラットフォームの独立性を提供しますか?JVMのさまざまな実装は何ですか、そしてそれらはすべて同じレベルのプラットフォームの独立性を提供しますか?Apr 24, 2025 am 12:10 AM

JVMの実装が異なると、プラットフォームの独立性が得られますが、パフォーマンスはわずかに異なります。 1。OracleHotspotとOpenJDKJVMは、プラットフォームの独立性で同様に機能しますが、OpenJDKは追加の構成が必要になる場合があります。 2。IBMJ9JVMは、特定のオペレーティングシステムで最適化を実行します。 3. Graalvmは複数の言語をサポートし、追加の構成が必要です。 4。AzulzingJVMには、特定のプラットフォーム調整が必要です。

プラットフォームの独立性は、開発コストと時間をどのように削減しますか?プラットフォームの独立性は、開発コストと時間をどのように削減しますか?Apr 24, 2025 am 12:08 AM

プラットフォームの独立性により、開発コストが削減され、複数のオペレーティングシステムで同じコードセットを実行することで開発時間を短縮します。具体的には、次のように表示されます。1。開発時間を短縮すると、1セットのコードのみが必要です。 2。メンテナンスコストを削減し、テストプロセスを統合します。 3.展開プロセスを簡素化するための迅速な反復とチームコラボレーション。

Javaのプラットフォームの独立性は、コードの再利用をどのように促進しますか?Javaのプラットフォームの独立性は、コードの再利用をどのように促進しますか?Apr 24, 2025 am 12:05 AM

java'splatformentedencefacilitatesecodereusebyAllowingbyTeCodeCodeCodeCodeTorunonAnyPlatformm.1)DevelopersConcodeCodeOnceOnceOnconconsentEntentEntEntEntEntEntentPlatforms.2)維持化されたアスカデドは、NoeedReadedoesではありません

Javaアプリケーションのプラットフォーム固有の問題をどのようにトラブルシューティングしますか?Javaアプリケーションのプラットフォーム固有の問題をどのようにトラブルシューティングしますか?Apr 24, 2025 am 12:04 AM

Javaアプリケーションのプラットフォーム固有の問題を解決するには、次の手順を実行できます。1。Javaのシステムクラスを使用して、システムプロパティを表示して実行中の環境を理解します。 2。ファイルクラスまたはjava.nio.fileパッケージを使用して、ファイルパスを処理します。 3。オペレーティングシステムの条件に応じてローカルライブラリをロードします。 4. VisualVMまたはJProfilerを使用して、クロスプラットフォームのパフォーマンスを最適化します。 5.テスト環境が、Dockerコンテナ化を通じて生産環境と一致していることを確認してください。 6. githubactionsを使用して、複数のプラットフォームで自動テストを実行します。これらの方法は、Javaアプリケーションでプラットフォーム固有の問題を効果的に解決するのに役立ちます。

JVMのクラスローダーサブシステムは、プラットフォームの独立性にどのように貢献していますか?JVMのクラスローダーサブシステムは、プラットフォームの独立性にどのように貢献していますか?Apr 23, 2025 am 12:14 AM

クラスローダーは、統一されたクラスファイル形式、動的読み込み、親代表団モデル、プラットフォーム非依存バイトコードを通じて、さまざまなプラットフォーム上のJavaプログラムの一貫性と互換性を保証し、プラットフォームの独立性を実現します。

Javaコンパイラはプラットフォーム固有のコードを作成しますか?説明する。Javaコンパイラはプラットフォーム固有のコードを作成しますか?説明する。Apr 23, 2025 am 12:09 AM

Javaコンパイラによって生成されたコードはプラットフォームに依存しませんが、最終的に実行されるコードはプラットフォーム固有です。 1。Javaソースコードは、プラットフォームに依存しないバイトコードにコンパイルされます。 2。JVMは、特定のプラットフォームのバイトコードをマシンコードに変換し、クロスプラットフォーム操作を保証しますが、パフォーマンスは異なる場合があります。

JVMは、さまざまなオペレーティングシステムでマルチスレッドをどのように処理しますか?JVMは、さまざまなオペレーティングシステムでマルチスレッドをどのように処理しますか?Apr 23, 2025 am 12:07 AM

マルチスレッドは、プログラムの応答性とリソースの利用を改善し、複雑な同時タスクを処理できるため、最新のプログラミングで重要です。 JVMは、スレッドマッピング、スケジューリングメカニズム、同期ロックメカニズムを介して、異なるオペレーティングシステム上のマルチスレッドの一貫性と効率を保証します。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)