Menu
CoddyTech

最后一个球才算数

Coddy Unity 游戏:打砖块 课程的第 18 课,共 19 课。

场上有 3 个球时,只有最后一个球丢失才会扣除一条命。在此之前,丢失的球会从列表中移除并被销毁。分裂后的史莱姆也采用相同的处理方式:

public void SlimeDied(Slime slime)
{
    slimes.Remove(slime);
    Destroy(slime.gameObject);
    if (slimes.Count == 0) Debug.Log("Room cleared");
}

原来的球可能会丢失,而复制出来的球仍在飞行,因此列表中的第一个球可能是复制出来的球。需要用到“球”的代码,例如关卡通关逻辑,应使用 balls[0],而不是场景中的 Ball 字段。

challenge icon

挑战

困难

丢失额外的球不会损失生命。

  1. 在 BallLost 的开头:如果 balls.Count > 1,就执行 balls.Remove(lost);,销毁它的对象并返回。
  2. 清除一个关卡时,调用 balls[0].ResetToPaddle(),而不是 ball.ResetToPaddle()。

按“开始”,分裂球并让两个球都落空:直到最后一个球落下,生命数都不会减少。然后按“检查”。

亲手试试

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)
        {
            ball.ResetToPaddle();
            BuildLevel((level + 1) % levels.Length);
        }
    }

    public void BallLost(Ball lost)
    {
        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;
    }
}

Unity 游戏:打砖块中的所有课

自己练习: Unity 在线编辑器