create logic for LevelUp() method in unity, so each 10 LinesCleared the fallTime…
- User
create logic for LevelUp() method in unity, so each 10 LinesCleared the fallTime decreses by 0.2f; and the max it can decrease to is 0.1f; :
using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; using UnityEngine.UI;
public class TetrisBlock : MonoBehaviour { public Vector3 rotationPoint; private float previousTime; public float fallTime = 0.8f; public static int height = 23; public static int width = 10; private static Transform[,] grid = new Transform[width, height]; public TMP_Text Lines_Cleared; public static int LinesCleared;
public int linesPerLevel = 10; public float fallTimeDecrease = 0.2f; public float minFallTime = 0.1f; private int level = 0; void Start() { } void Update() { LevelUp(); if(Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A)) { transform.position += new Vector3(-1, 0, 0); if(!ValidMove()) transform.position -= new Vector3(-1, 0, 0); } else if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D)) { transform.position += new Vector3(1, 0, 0); if (!ValidMove()) transform.position -= new Vector3(1, 0, 0); } else if (Input.GetKeyDown(KeyCode.X)) { transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0,0,1), 90); if (!ValidMove()) transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0, 0, 1), -90); } if (Time.time - previousTime > (Input.GetKey(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S) ? fallTime / 10 : fallTime)) { transform.position += new Vector3(0, -1, 0); if (!ValidMove()) { transform.position -= new Vector3(0, -1, 0); AddToGrid(); CheckForLines(); this.enabled = false; FindObjectOfType<SpawnTetromino>().NewTetromino(); } previousTime = Time.time; } } void CheckForLines() { for (int i = height-1; i >= 0; i--) { if(HasLine(i)) { LinesCleared++; DeleteLine(i); RowDown(i); UpdateUi(); } } } bool HasLine(int i) { for(int j = 0; j< width; j++) { if (grid[j, i] == null) return false; } return true; } void DeleteLine(int i) { for (int j = 0; j < width; j++) { Destroy(grid[j, i].gameObject); grid[j, i] = null; } } void RowDown(int i) { for (int y = i; y < height; y++) { for (int j = 0; j < width; j++) { if(grid[j,y] != null) { grid[j, y - 1] = grid[j, y]; grid[j, y] = null; grid[j, y - 1].transform.position -= new Vector3(0, 1, 0); } } } } void AddToGrid() { foreach (Transform children in transform) { int roundedX = Mathf.RoundToInt(children.transform.position.x); int roundedY = Mathf.RoundToInt(children.transform.position.y); grid[roundedX, roundedY] = children; } } bool ValidMove() { foreach (Transform children in transform) { int roundedX = Mathf.RoundToInt(children.transform.position.x); int roundedY = Mathf.RoundToInt(children.transform.position.y); if(roundedX < 0 || roundedX >= width || roundedY < 0 ||roundedY >= height) { return false; } if (grid[roundedX, roundedY] != null) return false; } return true; } public static void UpdateUi() { TMP_Text linesClearedText = GameObject.Find("Lines_Cleared").GetComponent<TextMeshProUGUI>(); linesClearedText.text = "Lines Cleared: " + LinesCleared.ToString(); Debug.Log(linesClearedText.text); } void LevelUp() { }}
- Assistant
Certainly! To implement the
LevelUp()method in your Tetris game, we'll follow these steps:-
Track Levels Based on Lines Cleared:
- Every time the player clears a multiple of
linesPerLevel(which is 10 in your case), the level increases.
- Every time the player clears a multiple of
-
Adjust
fallTime:- For each level up, decrease
fallTimebyfallTimeDecrease(0.2f). - Ensure that
fallTimedoes not go belowminFallTime(0.1f).
- For each level up, decrease
-
Update the UI (Optional):
- You might want to display the current level to the player.
Implementation
Here's how you can implement the
LevelUp()method within yourTetrisBlockclass:void LevelUp() { // Calculate the new level based on lines cleared int newLevel = LinesCleared / linesPerLevel; // Check if the level has increased if (newLevel > level) { level = newLevel; // Decrease fallTime by fallTimeDecrease, ensuring it doesn't go below minFallTime fallTime = Mathf.Max(fallTime - fallTimeDecrease, minFallTime); // Optional: Update the UI to show the new level TMP_Text levelText = GameObject.Find("Level_Text").GetComponent<TextMeshProUGUI>(); if (levelText != null) { levelText.text = "Level: " + level.ToString(); } Debug.Log("Level Up! Level: " + level + " | New Fall Time: " + fallTime); } }Detailed Explanation
-
Calculating the New Level:
int newLevel = LinesCleared / linesPerLevel;- This calculates the current level based on the total
LinesCleared. For example, ifLinesClearedis 20 andlinesPerLevelis 10,newLevelbecomes 2.
- This calculates the current level based on the total
-
Checking for Level Increase:
if (newLevel > level)- This ensures that the level only increases when the player has cleared enough lines to warrant a new level.
-
Updating
fallTime:fallTime = Mathf.Max(fallTime - fallTimeDecrease, minFallTime);- This line decreases the
fallTimebyfallTimeDecrease(0.2f) but ensures it doesn't go belowminFallTime(0.1f) usingMathf.Max.
- This line decreases the
-
Updating the UI (Optional):
TMP_Text levelText = GameObject.Find("Level_Text").GetComponent<TextMeshProUGUI>(); if (levelText != null) { levelText.text = "Level: " + level.ToString(); }- If you have a UI element to display the current level (e.g., a TextMeshPro text named "Level_Text"), this updates it accordingly.
-
Debug Logging:
Debug.Log("Level Up! Level: " + level + " | New Fall Time: " + fallTime);- This helps in debugging by logging the level-up event and the new
fallTime.
- This helps in debugging by logging the level-up event and the new
Full Updated Script
For completeness, here's the full
TetrisBlockscript with theLevelUp()method implemented:using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; using UnityEngine.UI; public class TetrisBlock : MonoBehaviour { public Vector3 rotationPoint; private float previousTime; public float fallTime = 0.8f; public static int height = 23; public static int width = 10; private static Transform[,] grid = new Transform[width, height]; public TMP_Text Lines_Cleared; public static int LinesCleared; public int linesPerLevel = 10; public float fallTimeDecrease = 0.2f; public float minFallTime = 0.1f; private int level = 0; void Start() { } void Update() { LevelUp(); if(Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A)) { transform.position += new Vector3(-1, 0, 0); if(!ValidMove()) transform.position -= new Vector3(-1, 0, 0); } else if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D)) { transform.position += new Vector3(1, 0, 0); if (!ValidMove()) transform.position -= new Vector3(1, 0, 0); } else if (Input.GetKeyDown(KeyCode.X)) { transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0,0,1), 90); if (!ValidMove()) transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0, 0, 1), -90); } if (Time.time - previousTime > ((Input.GetKey(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S)) ? fallTime / 10 : fallTime)) { transform.position += new Vector3(0, -1, 0); if (!ValidMove()) { transform.position -= new Vector3(0, -1, 0); AddToGrid(); CheckForLines(); this.enabled = false; FindObjectOfType<SpawnTetromino>().NewTetromino(); } previousTime = Time.time; } } void CheckForLines() { for (int i = height-1; i >= 0; i--) { if(HasLine(i)) { LinesCleared++; DeleteLine(i); RowDown(i); UpdateUi(); } } } bool HasLine(int i) { for(int j = 0; j< width; j++) { if (grid[j, i] == null) return false; } return true; } void DeleteLine(int i) { for (int j = 0; j < width; j++) { Destroy(grid[j, i].gameObject); grid[j, i] = null; } } void RowDown(int i) { for (int y = i; y < height; y++) { for (int j = 0; j < width; j++) { if(grid[j,y] != null) { grid[j, y - 1] = grid[j, y]; grid[j, y] = null; grid[j, y - 1].transform.position -= new Vector3(0, 1, 0); } } } } void AddToGrid() { foreach (Transform children in transform) { int roundedX = Mathf.RoundToInt(children.transform.position.x); int roundedY = Mathf.RoundToInt(children.transform.position.y); grid[roundedX, roundedY] = children; } } bool ValidMove() { foreach (Transform children in transform) { int roundedX = Mathf.RoundToInt(children.transform.position.x); int roundedY = Mathf.RoundToInt(children.transform.position.y); if(roundedX < 0 || roundedX >= width || roundedY < 0 || roundedY >= height) { return false; } if (grid[roundedX, roundedY] != null) return false; } return true; } public static void UpdateUi() { TMP_Text linesClearedText = GameObject.Find("Lines_Cleared").GetComponent<TextMeshProUGUI>(); linesClearedText.text = "Lines Cleared: " + LinesCleared.ToString(); Debug.Log(linesClearedText.text); } void LevelUp() { // Calculate the new level based on lines cleared int newLevel = LinesCleared / linesPerLevel; // Check if the level has increased if (newLevel > level) { level = newLevel; // Decrease fallTime by fallTimeDecrease, ensuring it doesn't go below minFallTime fallTime = Mathf.Max(fallTime - fallTimeDecrease, minFallTime); // Optional: Update the UI to show the new level TMP_Text levelText = GameObject.Find("Level_Text").GetComponent<TextMeshProUGUI>(); if (levelText != null) { levelText.text = "Level: " + level.ToString(); } Debug.Log("Level Up! Level: " + level + " | New Fall Time: " + fallTime); } } }Additional Recommendations
-
UI Elements:
-
Level Display:
- Make sure to add a TextMeshPro UI element named "Level_Text" to display the current level.
- Example setup in Unity:
- Right-click in the Hierarchy panel → UI → Text - TextMeshPro.
- Name it "Level_Text".
- Position it appropriately on the screen.
-
Lines Cleared Display:
- Ensure that the "Lines_Cleared" TextMeshPro element exists in your scene.
-
-
Optimizations:
-
Singleton Pattern for UI Updates:
- Consider using a singleton
GameManagerto handle UI updates and game states more efficiently.
- Consider using a singleton
-
Performance:
- Avoid using
GameObject.Find()frequently as it's not performance-friendly. Cache references where possible.
- Avoid using
-
-
Handling Multiple Level Ups:
- The current implementation handles multiple level-ups correctly since it recalculates the level based on the total
LinesCleared. If a player clears enough lines at once to skip levels, it adjusts accordingly.
- The current implementation handles multiple level-ups correctly since it recalculates the level based on the total
-
Feedback to Player:
- Consider adding sound effects or visual feedback when leveling up to enhance the user experience.
Conclusion
With the
LevelUp()method implemented as shown, your Tetris game will now decrease thefallTimeby 0.2f for every 10 lines cleared, down to a minimum of 0.1f. This will make the game progressively more challenging as the player advances through levels.Feel free to adjust the values of
linesPerLevel,fallTimeDecrease, andminFallTimeto better suit the desired difficulty curve of your game. -