My tetris game descends one piece and in the second piece it game over

Asked

Viewed 106 times

0

My game tetris descends the first normal piece, but in the second piece there in the same high, he of the game over. As if the second piece had reached the top entering the condition of the GAME OVER function.

My game code:

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;

public class Game : MonoBehaviour {

    public static int gridWidth = 10;
    public static int gridHeight = 20;
    public Tetromino[,] Grid = new Tetromino[gridWidth, gridHeight];
    private GameObject previewTetromino;
    private GameObject nextTetromino;
    private bool gameStarted = false;
    public int scoreOneLine = 40;
    public int scoreTwoLine = 100;
    public int cont = 0;
    private int numerOfRowsThisTurn = 0;
    private int currentRowMax = 0;
    public Text hud_score;
    public static int currentScore = 0;
    private Vector2 previewTetrominoPosition = new Vector2 (13.88f, 11.08f);


    // Use this for initialization
    void Start () {
        SpawnNextTetromino ();


        //AudioSource = GetComponent<AudioSource> ();
    } 

    void Update () {

        Checks ();
        UpdateScore ();
        UpdateUI ();
    }

    // Update is called once per frame
    public void UpdateScore(){
        if (numerOfRowsThisTurn > 0) {

            if (numerOfRowsThisTurn == 1) {
                ClearedOneLine ();
            } else if (numerOfRowsThisTurn == 2) {
                ClearedTwoLine ();
            }
            numerOfRowsThisTurn = 0;
        }
    }

    public void Checks(){

        for (int y = 0; y < gridHeight; ++y) {
            for (int x = 0; x < gridWidth; ++x) {

            }
        }

        /*
        for (int y = 0; y < gridHeight; ++y) {
            for (int x = 0; x < gridWidth; ++x) {

                if (Grid [x, y] != null) {
                    Debug.Log ("grid : " + x + " , " + y + " nao nulo " + Grid[x,y].tag);
                    if (Grid [x, y].tag == "CARBONO") {
                        Debug.Log ("EXISTE C: ");
                        if(Grid [x, y].tag == "CARBONO")
                        if (Grid [x + 1, y] != null){

                            if (Grid [x + 1, y].tag == "HIDROGENIO") {
                            cont++;
                            }
                        }
                        if (Grid [x - 1, y] != null) {
                                if (Grid [x - 1, y].tag == "HIDROGENIO") {
                                    cont++;
                                }
                            }
                            if (Grid [x, y + 1] != null) {
                                if (Grid [x, y + 1].tag == "HIDROGENIO") {
                                    cont++;
                                }
                            }
                        if (Grid [x, y - 1] != null) {

                                    if (Grid [x, y - 1].tag == "HIDROGENIO") {
                                        cont++;
                                    }
                                }
                            }

                    Debug.Log ("contador: " + cont); //teste
                }cont = 0;
            }
        }*/
    }

    public void UpdateUI(){
        hud_score.text = currentScore.ToString ();
    }



    public void ClearedOneLine(){
        currentScore += scoreOneLine;
    }
    public void ClearedTwoLine(){
        currentScore += scoreTwoLine;
    }

    public bool CheckIsAboveGrid(Tetromino tetromino){
        for(int x = 0; x < 20; ++x){
        foreach(Transform mino in tetromino.transform){
        Vector2 pos = Round (tetromino.transform.position);
                Debug.Log ("POS Y " + pos.y);
                if(pos.y > gridHeight - 1){
                    return true;
                }
            }
        }
        return false;
    }

    public bool IsFullRowAt(int y){
        for (int x = 0; x < gridWidth; ++x){
            if(Grid[x,y] = null){
                return false;
            }
        }
        numerOfRowsThisTurn++;
        return true;
    }

    public void updateGrid (Tetromino tetromino) {
        for (int y = 0; y <gridHeight; ++y) {
            for (int x = 0; x <gridWidth; ++x) {
                if (Grid [x, y] != null) {
                    if (Grid [x, y].transform.parent == tetromino.transform) {
                        Grid [x, y] = null;
                    }
                }
            }
        }

        foreach (Transform mino in tetromino.transform) {
            Vector2 pos = Round (mino.position);
            if (pos.y < gridHeight) {
                mino.tag = tetromino.tag;
                Grid [(int)pos.x, (int)pos.y] = tetromino;

                //Debug.Log ("x :" + pos.x + "y: " + pos.y + "tag next tetromino : "+ mino.tag);
            }
        }
    }
    public Transform GetTransformAtGetPosition (Vector2 pos) {

        if (pos.y > gridHeight - 1) {
            return null;
        } else {
            if (Grid [(int)pos.x, (int)pos.y] == null) {
                return null;
            } else {
                return Grid [(int)pos.x, (int)pos.y].transform;
            }

        }

    }

    public void SpawnNextTetromino () {
        if (!gameStarted) {

            gameStarted = true;
            nextTetromino = (GameObject)Instantiate(Resources.Load(GetRandomTetromino(), typeof(GameObject)), new Vector2 (5.0f, 22.0f), Quaternion.identity);
            Debug.Log ("tag next tetromino : "+ nextTetromino.tag);
            previewTetromino = (GameObject)Instantiate (Resources.Load (GetRandomTetromino (), typeof(GameObject)), previewTetrominoPosition, Quaternion.identity);
            previewTetromino.GetComponent<Tetromino> ().enabled = false;

        } else {
            previewTetromino.transform.localPosition = new Vector2 (5.0f, 20.0f);
            nextTetromino = previewTetromino;
            nextTetromino.GetComponent<Tetromino> ().enabled = true;

            previewTetromino = (GameObject)Instantiate (Resources.Load (GetRandomTetromino (), typeof(GameObject)), previewTetrominoPosition, Quaternion.identity);
            previewTetromino.GetComponent<Tetromino> ().enabled = false;
        }

    }


    public bool CheckIsInsideGrid (Vector2 pos){
        return ((int)pos.x >= 0 && (int)pos.x < gridWidth && (int)pos.y >= 0);
    }

    public Vector2 Round (Vector2 pos){
        return new Vector2 (Mathf.Round(pos.x), Mathf.Round(pos.y));
    }

    string GetRandomTetromino(){
        int randomTetromino = Random.Range (1, 5);
        string randomTetrominoName = "Prefabs/C";
        switch (randomTetromino) {
        case 1:
            randomTetrominoName = "Prefabs/H";
            break;
        case 2:
            randomTetrominoName = "Prefabs/C";
            break;
        case 3:
            randomTetrominoName = "Prefabs/H";
            break;
        case 4:
            randomTetrominoName = "Prefabs/C";
            break;

        }
        return randomTetrominoName;


    }

    public void updateConnection(Tetromino tetromino){
        if (tetromino.tag == "CARBONO") {

            foreach (Transform mino in tetromino.transform) {
                Vector2 pos = Round (mino.position);

                if (Grid[(int)pos.x,(int)pos.y+1] != null) {
                    tetromino.setConnection (Grid[(int)pos.x,(int)pos.y+1], Tetromino.UP);
                }

                if (pos.y != 0) {
                    if (Grid[(int)pos.x,(int)pos.y-1] != null) {
                        tetromino.setConnection (Grid[(int)pos.x,(int)pos.y-1], Tetromino.DOWN);
                    }               
                }

                if (pos.x != 0) {
                    if (Grid[(int)pos.x-1,(int)pos.y] != null) {
                        tetromino.setConnection (Grid[(int)pos.x-1,(int)pos.y], Tetromino.LEFT);
                    }
                }

                if (pos.x != gridWidth-1) {
                    if (Grid[(int)pos.x+1,(int)pos.y] != null) {
                        tetromino.setConnection (Grid[(int)pos.x+1,(int)pos.y] , Tetromino.RIGHT);
                    }
                }

            }
        }
    }


    public void GameOver(){
        Application.LoadLevel ("GameOver");
    }

}

segunda classe:

    using UnityEngine;
    using System.Collections;

public class Tetromino : MonoBehaviour {

    public static int UP = 1;  
    public static int DOWN = 2;
    public static int RIGHT = 3;
    public static int LEFT = 4;


    float fall = 0;
    public float fallSpeed = 2;
    public bool allowRotation = true;
    public bool limitRoatation = false; 
    public int individualScore = 100;
    private float individualScoreTime;
    private bool visited = false;
    private Tetromino[] connections = new Tetromino[4];


    // Update is called once per frame
    void Update () {
        CheckUserInput ();
        UpdateIndividualScore ();
    }

    public void UpdateIndividualScore(){

        if (individualScoreTime < 1) {

            individualScoreTime += Time.deltaTime;

        } else {

            individualScoreTime = 0;

            individualScore = Mathf.Max (individualScore - 10, 0);
        }
    }
    public void CheckUserInput () {

        if (Input.GetKeyDown(KeyCode.RightArrow)) {

            transform.position += new Vector3(1, 0, 0);
            bool t = CheckIsValidPosition ();
            if (t) {

                FindObjectOfType<Game>().updateGrid(this);

            } else {

                transform.position += new Vector3(-1, 0, 0);
            }

        } else if (Input.GetKeyDown(KeyCode.LeftArrow)) {

            transform.position += new Vector3(-1, 0, 0);
            bool t = CheckIsValidPosition ();
            if (t) {

                FindObjectOfType<Game>().updateGrid(this);

            } else {

                transform.position += new Vector3(1, 0, 0);
            }

        } else if (Input.GetKeyDown(KeyCode.DownArrow) || Time.time - fall >= fallSpeed) {

            transform.position += new Vector3(0, -1, 0);
            bool t = CheckIsValidPosition ();
            //Debug.Log ("CHECK VALIDATION " + t);
            if (t) {

                FindObjectOfType<Game>().updateGrid(this);
                FindObjectOfType<Game> ().Checks ();

            } else {

                transform.position += new Vector3 (0, 1, 0);

                if (FindObjectOfType<Game> ().CheckIsAboveGrid (this)) 
                {
                    FindObjectOfType<Game> ().GameOver ();
                }
                    enabled = false;
                    FindObjectOfType<Game> ().SpawnNextTetromino ();
                    //FindObjectOfType<Game> ().updateConnection (this);
                    //FindObjectOfType<Game> ().Checks ();
                    Game.currentScore += individualScore;

            }
            fall = Time.time;
        }
    }


    bool CheckIsValidPosition () {

        foreach (Transform mino in transform) {

            Vector2 pos = FindObjectOfType<Game>().Round (mino.position);

            if (FindObjectOfType<Game>().CheckIsInsideGrid (pos) == false) {
                Debug.Log ("False 1");  
                return false;
            }

            Transform t = FindObjectOfType<Game> ().GetTransformAtGetPosition (pos);
            if (t != null) {
                if (t.parent != transform) {
                    Debug.Log ("False 3");
                    return false;
                }               
            }

            if (FindObjectOfType<Game>().GetTransformAtGetPosition(pos) != null && FindObjectOfType<Game>().GetTransformAtGetPosition(pos).parent != transform) {
                Debug.Log ("False 2");
                return false;
            }
        }

        return true;
    }

    public void setVisited(bool v){
        visited = v;
    }

    public bool isVisited(){
        return true;
    }

    public void setConnection(Tetromino t, int position){
        if (position == UP) {
            connections [0] = t;
        }
        if (position == DOWN) {
            connections [1] = t;
        }
        if (position == LEFT) {
            connections [2] = t;
        }
        if (position == RIGHT) {
            connections [3] = t;
        }
    }

    public Tetromino getTetrominoConnected(int position){
        if (position == UP) {
            return connections [0];
        }else if (position == DOWN) {
            return connections [1];
        }else if (position == LEFT) {
            return connections [2];
        }else {
            return connections [3];
        }
    }
}
  • You’d better dry the code, first remove the commented codes, and then remove the functions that are not being executed, to make it easier to read and to find the problem

  • 1

    I’m not sure if this is the reason (your code is a bit messy and you didn’t explain the details of it), but it seems that the function CheckIsAboveGrid simply checks if the object is outside the grid WITHOUT CHECKING IF HE’S ALREADY IN PLACE. Tip: Create a flag for the object, and change that flag to true only when it actually fits into the background. Then change that function to return true only if the object is outside the grid And is also fitted.

  • 1

    @Luizvieira, "... return true only if the object is outside the grid And" is not fitted.

  • 1

    Well noted, @Marcelouchimura. :) Tks

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.