Menu
CoddyTech

Tùy chỉnh theo ý bạn

Bài học 19 trên 19 trong khóa học Trò chơi Unity: phá gạch của Coddy.

Tính năng cuối cùng là lưu điểm cao nhất giữa các ván chơi. PlayerPrefs lưu các giá trị nhỏ trên thiết bị của người chơi: PlayerPrefs.GetInt(key, 0) đọc một giá trị, trả về 0 nếu chưa có gì được lưu, còn PlayerPrefs.SetInt(key, value) lưu một giá trị. Chơi lại sẽ tải lại cảnh, rồi GameManager mới tải điểm cao nhất.

Giờ bạn có thể tùy chỉnh trò chơi. Mỗi con số thiết lập cách chơi đều là một trường có giá trị ban đầu trong code, còn Inspector của người chơi chỉ cho phép xem, vì vậy hãy thay đổi các giá trị đó trong code:

  • Ball: speed (9) và minVertical (0.3).
  • Paddle: speed (14), wideFactor (1.6) và wideSeconds (8).
  • GameManager: lives (3) và dropChance (0.15).
challenge icon

Thử thách

Khó

Lưu điểm cao nhất qua các ván chơi, rồi điều chỉnh trò chơi.

  1. Trong GameManager, thêm public TMP_Text bestText; và int best;; phần Start() bắt đầu bằng best = PlayerPrefs.GetInt("BestScore", 0);
  2. ShowHud() cũng đặt bestText.text = "Best: " + best;
  3. Khi mất mạng cuối cùng và score > best, hãy đặt best, PlayerPrefs.SetInt("BestScore", best); rồi gọi ShowHud().
  4. Điều chỉnh tốc độ, số mạng và dropChance tại nơi khai báo chúng trong code.

Nhấn Chơi và lập điểm cao nhất: điểm đó vẫn còn sau khi nhấn Chơi lại. Sau đó, nhấn Kiểm tra.

Tự làm thử

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;

    public Brick brickPrefab;
    public Transform wall;
    public string[] levels;
    public float spacingX = 1.75f;
    public float spacingY = 0.75f;
    public float topY = 6f;

    public Ball ball;
    public int lives = 3;
    public TMP_Text scoreText;
    public TMP_Text livesText;
    public GameObject gameOverPanel;
    public GameObject[] powerUps;
    public float dropChance = 0.15f;

    int score;
    int level;
    int bricksLeft;
    List<Ball> balls = new List<Ball>();

    void Awake()
    {
        Instance = this;
    }

    void Start()
    {
        balls.Add(ball);
        BuildLevel(0);
        ShowHud();
    }

    void BuildLevel(int index)
    {
        level = index;
        string[] rows = levels[index].Split('/');
        for (int row = 0; row < rows.Length; row++)
        {
            string line = rows[row];
            float left = -(line.Length - 1) * spacingX / 2f;
            for (int col = 0; col < line.Length; col++)
            {
                char c = line[col];
                if (c == '.') continue;
                Vector3 pos = new Vector3(left + col * spacingX, topY - row * spacingY, 0f);
                Brick brick = Instantiate(brickPrefab, pos, Quaternion.identity, wall);
                brick.SetHits(c - '0');
                bricksLeft++;
            }
        }
    }

    public void BrickBroken(Brick brick)
    {
        score += brick.points;
        bricksLeft--;
        ShowHud();
        if (Random.value < dropChance)
        {
            GameObject prefab = powerUps[Random.Range(0, powerUps.Length)];
            Instantiate(prefab, brick.transform.position, Quaternion.identity);
        }
        if (bricksLeft == 0)
        {
            balls[0].ResetToPaddle();
            BuildLevel((level + 1) % levels.Length);
        }
    }

    public void BallLost(Ball lost)
    {
        if (balls.Count > 1)
        {
            balls.Remove(lost);
            Destroy(lost.gameObject);
            return;
        }
        lives--;
        ShowHud();
        if (lives == 0)
        {
            gameOverPanel.SetActive(true);
            lost.gameObject.SetActive(false);
        }
        else lost.ResetToPaddle();
    }

    public void SplitBall()
    {
        Ball source = balls[0];
        Ball left = Instantiate(source, source.transform.position, Quaternion.identity);
        left.Launch(new Vector3(-0.6f, 1f, 0f));
        balls.Add(left);
        Ball right = Instantiate(source, source.transform.position, Quaternion.identity);
        right.Launch(new Vector3(0.6f, 1f, 0f));
        balls.Add(right);
    }

    public void PlayAgain()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    void ShowHud()
    {
        scoreText.text = "Score: " + score;
        livesText.text = "Lives: " + lives;
    }
}
quiz iconTự kiểm tra

Bài học này có một bài trắc nghiệm ngắn. Bắt đầu bài học để trả lời và theo dõi tiến độ.

Tất cả bài học trong Trò chơi Unity: phá gạch

Tự luyện tập: Playground Unity