Android Open Source - tetris-android World






From Project

Back to project page tetris-android.

License

The source code is released under:

MIT License

If you think the Android project tetris-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ultimate39.android.games.tetris;
/* ww w  . ja va  2 s . c om*/
import java.util.Random;
import com.badlogic.androidgames.framework.math.Rectangle;
import com.badlogic.androidgames.framework.math.Vector2;

public class World {

  Square[][] field;
  Shape shape;
  Square[] miniShape;
  Square[] shadowShape;
  Square[] shapeAr;
  Square sq1;
  int score;
  int level;
  int levelScore;
  static final int STATE_GAME_OVER = 1;
  static final int STATE_GAME_RUNNING = 2;
  static final int FIELD_WIDTH = 17;
  static final int FIELD_HEIGHT = 26;
  static final int SQUARE_CAPACITY = FIELD_WIDTH * FIELD_HEIGHT;
  static final float TICK_INITIAL = 0.8f;
  static final int MINUS_HEIGHT = 1;
  static final int PLUS_WIDTH = 2;
  Vector2 speed = new Vector2();
  float tick = 0;
  float tickLevel = 0;
  float tickTimeLevel = 160;
  float tickTime = TICK_INITIAL;
  int capacity = SQUARE_CAPACITY;
  static int state = 0;
  Rectangle turnButton;
  Rectangle moveRightButton;
  Rectangle moveLeftButton;
  Rectangle fieldScreen;
  Rectangle downButton;
  Rectangle yesBtn;
  Rectangle noBtn;
  static Random rand = new Random();
  private  int shapeType = rand.nextInt(7);
  private int color = rand.nextInt(4);

  
  float tickTimeShape = 0;
  float tickTimeShapeInitial =  0.17f;
  boolean shapeFall = false;
  public World() {
    field = new Square[FIELD_HEIGHT][FIELD_WIDTH];
    shape = FactoryShape.newShape(rand.nextInt(7),rand.nextInt(3));
    score = 0;
      level = 1;
      levelScore = 0;
    speed = new Vector2(0, -1);
    turnButton = new Rectangle(6.1f, 0, 4, 3);
    moveLeftButton = new Rectangle(-2, 0, 8, 3);
    moveRightButton = new Rectangle(10, 0, 7, 3);
    fieldScreen = new Rectangle(0, 3.05f, 13, 22);
      downButton = new Rectangle(13.5f,4,3,3);
      noBtn = new Rectangle(7.3f,11f,4,3);
      yesBtn = new Rectangle(2.3f,11f,4,3);
    shadowShape = new Square[4];
    shapeAr = shape.getArray();
    state = STATE_GAME_RUNNING;
    getShadowShape();
    newMiniShape();

  }

  void update(float deltaTime) {
    updateCollision();
    getShadowShape();
    moveShape(deltaTime, speed);
    updateLevel(deltaTime);
    if(shapeFall){
      tickTimeShape +=deltaTime;
      if(tickTimeShape > tickTimeShapeInitial){
        shapeFall = false;
        tickTimeShape = 0;
        insertShape();
        newShape();
        shapeType = rand.nextInt(7);
        color = rand.nextInt(4);
        newMiniShape();
        deleteFullLines();
      }
    }

  }

  void moveShape(float deltaTime, Vector2 speed) {
    tick += deltaTime;
    if (tick > tickTime) {
      tick -= tickTime;
      shape.update(speed.x, speed.y);
    }
  }

  void insertShape() {

    for (int i = 0; i < shapeAr.length; i++) {
      field[shapeAr[i].newFieldY][shapeAr[i].newFieldX] = shapeAr[i];
    }

  }

  void updateCollision() {
    for (int i = 0; i < shapeAr.length; i++) {
      if (shapeAr[i].newFieldY == 2
          || field[shapeAr[i].newFieldY - 1][shapeAr[i].newFieldX] != null) {
        stopShape();
        if (shapeAr[i].newFieldY == 21) {
          state = STATE_GAME_OVER;
          break;
        }
        stopShape();
        shapeFall = true;
        break;
      }else{
        shapeFall = false;
        startShape();
      }
    }
  }

  void stopShape() {
    speed.set(0, 0);
  }
    void startShape(){
      speed.set(0,-1);
    }
  void newShape() {
    shape = FactoryShape.newShape(shapeType,color);
    speed.set(0, -1);
    shapeAr = shape.getArray();
    FactoryShape.positionLine = 1;
    
  }

  void fallShape() {
    boolean fall = false;
    int y = shapeAr[1].newFieldY;
    synchronized (shape) {
      while (true) {
        Square[] shapeAr = shape.getArray();
        for (int i = 0; i < shapeAr.length; i++) {
          if (shapeAr[i].newFieldY != 2
              && field[shapeAr[i].newFieldY - 1][shapeAr[i].newFieldX] == null) {

          } else {
            stopShape();
            fall = true;
            break;
          }

        }

        if (fall) {
          score+=y/3+levelScore;
          break;
        }
        shape.update(0, -1);
      }
    }
  }

  void deleteFullLines() {
    // ??????? ?????? ??????????? ?????
    int firstFullLine = 0;
    boolean isFull = true;
    for (int i = 2; i <= 23; i++) {
      for (int j = 2; j <= 14; j++) {
        if (field[i][j] == null) {
          isFull = false;
          break;
        }
      }
      if (isFull) {
        firstFullLine = i;
        break;
      }
      isFull = true;
    }
    // ????????? ???? ?? ??????????? ?????
    int lastFullLine = 0;

    for (int i = 23; i >= 2; i--) {
      for (int j = 14; j >= 2; j--) {
        if (field[i][j] == null) {
          isFull = false;
          break;
        }
      }
      // ???? ???? ?? ??????? ???????? ?? ???? ?????
      if (isFull) {
        for (int b = 14; b >= 2; b--) {
          field[i][b] = null;
        }
        lastFullLine = i;
        score+=20+levelScore;
      }
      isFull = true;

    }
    boolean fill = false;
    int fillLine = firstFullLine;
    // C?????? ????????
    if (lastFullLine > 0) {
      for (int i = lastFullLine + 1; i < 23; i++) {
        for (int j = 2; j <= 14; j++) {
          if (field[i][j] != null) {
            field[fillLine][j] = field[i][j];
            field[i][j] = null;
            fill = true;

          }
        }
        if (fill == true) {
          fillLine += 1;
        }
        fill = false;
      }
    }

  }

  void getShadowShape() {
    Square squareCopy[] = new Square[4];
    Square squareMain[] = shape.getArray();
    squareCopy[0] = new Square(new Vector2(squareMain[0].position),
        squareMain[0].color, squareMain[0].newFieldX,
        squareMain[0].newFieldY);
    squareCopy[1] = new Square(new Vector2(squareMain[1].position),
        squareMain[1].color, squareMain[1].newFieldX,
        squareMain[1].newFieldY);
    squareCopy[2] = new Square(new Vector2(squareMain[2].position),
        squareMain[2].color, squareMain[2].newFieldX,
        squareMain[2].newFieldY);
    squareCopy[3] = new Square(new Vector2(squareMain[3].position),
        squareMain[3].color, squareMain[3].newFieldX,
        squareMain[3].newFieldY);
    boolean b = false;
    while (true) {
      for (int i = 0; i < squareCopy.length; i++) {
        if (squareCopy[i].newFieldY != 2
            && field[squareCopy[i].newFieldY - 1][squareCopy[i].newFieldX] == null) {

        } else {
          b = true;
          break;
        }

      }

      if (b) {
        break;
      }
      for (int j = 0; j < squareCopy.length; j++) {
        squareCopy[j].position.add(0, -1);
        squareCopy[j].newFieldY += -1;
        squareCopy[j].newFieldX += 0;

      }
    }
    shadowShape = squareCopy;
  }

  void reload() {
    for (int i = 1; i < FIELD_HEIGHT-1; i++) {
      for (int j = 1; j < FIELD_WIDTH-1; j++) {
        field[i][j] = null;
      }
      newShape();
      state = STATE_GAME_RUNNING;
      score = 0;
      level = 1;
      levelScore =0;
      tickTime = TICK_INITIAL;

    }
  }

  void newMiniShape() {
    int type = shapeType+1;
    int color = this.color;
    miniShape = new Square[4];
    switch (type) {
    case 0:
      return;
    case 1:
      miniShape[0] = new Square(new Vector2(14.2f, 19.0f), color, 0, 0);
      miniShape[1] = new Square(new Vector2(14.9f, 19.0f), color, 0, 0);
      miniShape[2] = new Square(new Vector2(14.2f, 19.7f), color, 0, 0);
      miniShape[3] = new Square(new Vector2(14.9f, 19.7f), color, 0, 0);
      break;

    case 2:
      miniShape[0] = new Square(new Vector2(14.5f, 20.3f), color, 0, 0);
      miniShape[1] = new Square(new Vector2(14.5f, 19.6f), color, 0, 0);
      miniShape[2] = new Square(new Vector2(14.5f, 18.9f), color, 0, 0);
      miniShape[3] = new Square(new Vector2(14.5f, 18.2f), color, 0, 0);
      break;
    case 3:
      miniShape[0] = new Square(new Vector2(14.9f, 19.9f), color, 0, 0);
      miniShape[1] = new Square(new Vector2(14.2f, 19.9f), color, 0, 0);
      miniShape[2] = new Square(new Vector2(14.2f, 19.2f), color, 0, 0);
      miniShape[3] = new Square(new Vector2(14.2f, 18.5f), color, 0, 0);
      break;
    case 4:
      miniShape[0] = new Square(new Vector2(14.2f, 19.9f), color, 0, 0);
      miniShape[1] = new Square(new Vector2(14.9f, 19.9f), color, 0, 0);
      miniShape[2] = new Square(new Vector2(14.9f, 19.2f), color, 0, 0);
      miniShape[3] = new Square(new Vector2(14.9f, 18.5f), color, 0, 0);
      break;
    case 5:
      miniShape[0] = new Square(new Vector2(13.9f, 19.0f), color, 0, 0);
      miniShape[1] = new Square(new Vector2(14.6f, 19.0f), color, 0, 0);
      miniShape[2] = new Square(new Vector2(14.6f, 19.7f), color, 0, 0);
      miniShape[3] = new Square(new Vector2(15.3f, 19.7f), color, 0, 0);
      break;
    case 6 :
      miniShape[0] = new Square(new Vector2(13.9f, 19.0f), color, 0, 0);
      miniShape[1] = new Square(new Vector2(14.6f, 19.0f), color, 0, 0);
      miniShape[2] = new Square(new Vector2(14.6f, 19.7f), color, 0, 0);
      miniShape[3] = new Square(new Vector2(15.3f, 19.0f), color, 0, 0);
      break;
    case 7 :
      miniShape[0] = new Square(new Vector2(13.9f, 19.7f), color, 0, 0);
      miniShape[1] = new Square(new Vector2(14.6f, 19.0f), color, 0, 0);
      miniShape[2] = new Square(new Vector2(14.6f, 19.7f), color, 0, 0);
      miniShape[3] = new Square(new Vector2(15.3f, 19.0f), color, 0, 0);
      break;
      

    }
  }
  public void fasterDown(){
    if(shapeFall!=true){
    shape.update(0, -1);
    score+=1+levelScore;
    }
  }
  public void updateLevel(float deltaTime){
    
      tickLevel += deltaTime;
      if (tickLevel > tickTimeLevel) {
        tickLevel -= tickTimeLevel;
        level +=1;
        levelScore+=1;
        tickTime -=0.08;
      }
    }
  }




Java Source Code List

com.badlogic.androidgames.framework.Audio.java
com.badlogic.androidgames.framework.Color.java
com.badlogic.androidgames.framework.DynamicGameObject.java
com.badlogic.androidgames.framework.FileIOInternal.java
com.badlogic.androidgames.framework.FileIO.java
com.badlogic.androidgames.framework.GameObject.java
com.badlogic.androidgames.framework.Game.java
com.badlogic.androidgames.framework.Graphics.java
com.badlogic.androidgames.framework.Input.java
com.badlogic.androidgames.framework.Music.java
com.badlogic.androidgames.framework.Pixmap.java
com.badlogic.androidgames.framework.Pool.java
com.badlogic.androidgames.framework.Screen.java
com.badlogic.androidgames.framework.Sound.java
com.badlogic.androidgames.framework.TestScreen.java
com.badlogic.androidgames.framework.gl.Animation.java
com.badlogic.androidgames.framework.gl.BTMPFont.java
com.badlogic.androidgames.framework.gl.Camera2D.java
com.badlogic.androidgames.framework.gl.Font.java
com.badlogic.androidgames.framework.gl.SpatialHashGrid.java
com.badlogic.androidgames.framework.gl.SpriteBatcher.java
com.badlogic.androidgames.framework.gl.TextureRegion.java
com.badlogic.androidgames.framework.gl.Texture.java
com.badlogic.androidgames.framework.gl.Vertices.java
com.badlogic.androidgames.framework.helper.DebugDraw.java
com.badlogic.androidgames.framework.helper.FPSCounter.java
com.badlogic.androidgames.framework.helper.Logger.java
com.badlogic.androidgames.framework.impl.AccelerometerHandler.java
com.badlogic.androidgames.framework.impl.AndroidAudio.java
com.badlogic.androidgames.framework.impl.AndroidFastRenderView.java
com.badlogic.androidgames.framework.impl.AndroidFileIOInternal.java
com.badlogic.androidgames.framework.impl.AndroidFileIO.java
com.badlogic.androidgames.framework.impl.AndroidGame.java
com.badlogic.androidgames.framework.impl.AndroidGraphics.java
com.badlogic.androidgames.framework.impl.AndroidInput.java
com.badlogic.androidgames.framework.impl.AndroidMusic.java
com.badlogic.androidgames.framework.impl.AndroidPixmap.java
com.badlogic.androidgames.framework.impl.AndroidSound.java
com.badlogic.androidgames.framework.impl.GLGame.java
com.badlogic.androidgames.framework.impl.GLGraphics.java
com.badlogic.androidgames.framework.impl.GLScreen.java
com.badlogic.androidgames.framework.impl.KeyboardHandler.java
com.badlogic.androidgames.framework.impl.MultiTouchHandler.java
com.badlogic.androidgames.framework.impl.SingleTouchHandler.java
com.badlogic.androidgames.framework.impl.TouchHandler.java
com.badlogic.androidgames.framework.math.Circle.java
com.badlogic.androidgames.framework.math.OverlapTester.java
com.badlogic.androidgames.framework.math.Rectangle.java
com.badlogic.androidgames.framework.math.Vector2.java
com.ultimate39.android.games.tetris.Assets.java
com.ultimate39.android.games.tetris.FactoryShape.java
com.ultimate39.android.games.tetris.GameScreen.java
com.ultimate39.android.games.tetris.MainMenuScreen.java
com.ultimate39.android.games.tetris.Settings.java
com.ultimate39.android.games.tetris.Shape.java
com.ultimate39.android.games.tetris.Square.java
com.ultimate39.android.games.tetris.Tetris.java
com.ultimate39.android.games.tetris.World.java