Menu
CoddyTech

다음 레벨

Coddy의 Unity 게임: 벽돌 깨기 코스 레슨. 19개 중 12번째.

마지막 벽돌이 깨지면 레벨이 완료됩니다. 개수: 벽돌을 만들 때마다 하나씩 더하고, 벽돌이 깨질 때마다 하나씩 빼세요. 개수가 0이 되면 레벨이 끝납니다.

그 순간 장면에 남아 있는 벽돌을 세는 방법은 통하지 않습니다. Destroy는 프레임이 끝날 때까지 기다리므로, 깨지고 있는 벽돌이 아직 남아 있습니다.

마지막 레벨 다음에는 다시 첫 번째 레벨이 옵니다. 나눗셈의 나머지는 숫자를 처음부터 다시 돌게 합니다.

int next = (2 + 1) % 3;   // 0: 3개 레벨의 인덱스 2 다음에는 다시 0으로 돌아감

새 벽이 날아가는 공 아래에서 생기지 않도록, 먼저 공을 패들 위로 되돌립니다.

challenge icon

챌린지

중급

벽돌을 모두 없애면 다음 레벨이 만들어집니다.

  1. GameManager에 public Ball ball; (씬에서 연결합니다)과 int bricksLeft;를 추가하세요. BuildLevel은 벽돌을 하나 만들 때마다 값을 1씩 더합니다.
  2. BrickBroken에서 bricksLeft 값을 1 줄이세요. 값이 0이면 ball.ResetToPaddle();과 BuildLevel((level + 1) % levels.Length);을 호출하세요.

‘플레이’를 누르고 레벨을 모두 클리어하세요. 공이 패들 위에서 대기하고 새로운 벽이 나타납니다. 그런 다음 ‘확인’을 누르세요.

직접 해보기

using UnityEngine;
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 int lives = 3;
    public TMP_Text scoreText;
    public TMP_Text livesText;

    int score;
    int level;

    void Awake()
    {
        Instance = this;
    }

    void Start()
    {
        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');
            }
        }
    }

    public void BrickBroken(Brick brick)
    {
        score += brick.points;
        ShowHud();
    }

    public void BallLost(Ball lost)
    {
        lives--;
        ShowHud();
        lost.ResetToPaddle();
    }

    void ShowHud()
    {
        scoreText.text = "Score: " + score;
        livesText.text = "Lives: " + lives;
    }
}

Unity 게임: 벽돌 깨기의 모든 레슨

직접 연습해 보세요: Unity 플레이그라운드