Menu
CoddyTech
flag Ar iconالعربيةdown icon

اجعله خاصًا بك

الدرس 16 من 16 في دورة لعبة Unity: تحطيم الطوب على Coddy.

الميزة الأخيرة هي تسجيل أفضل نتيجة يحتفظ بها بين الألعاب. تخزّن PlayerPrefs القيم الصغيرة على جهاز اللاعب: تقرأ PlayerPrefs.GetInt(key, 0) قيمةً، وتكون القيمة 0 إذا لم يُحفَظ شيء بعد، وتحفظ PlayerPrefs.SetInt(key, value) قيمةً. حمّل أفضل نتيجة عند بدء اللعبة، واحفظها عند انتهاء اللعبة إذا كانت النتيجة أعلى. يعيد زر «العب مجددًا» تحميل المشهد، ويحمّل GameManager الجديد أفضل نتيجة مرة أخرى.

بعد ذلك، يمكنك ضبط اللعبة كما تشاء. كل رقم يحدد طريقة لعبها هو حقل له قيمة ابتدائية في الشيفرة:

  • Ball: speed (9) وminVertical (0.3).
  • Paddle: speed (14) وwideFactor (1.6) وwideSeconds (8).
  • GameManager: lives (3) وdropChance (0.15) والمسافة بين الطوب.

التخطيطات هي قائمة levels الخاصة بـ GameManager في Inspector: الرقم يعني قطعة طوب تحتاج إلى ذلك العدد من الضربات، والنقطة تعني فراغًا، وتبدأ / الصف التالي، مثل 1.3.1/22222. في مشروع Unity الخاص بك، غيّر القيم في Inspector: بمجرد وضع سكربت على كائن، يحتفظ Inspector بنسخته الخاصة، ولن تصل إليه بعد ذلك أي قيمة ابتدائية جديدة في الشيفرة.

challenge icon

التحدي

صعب
  1. في GameManager، أضف public TMP_Text bestText (يربط المشهد العنصر BestText) وint best. في Start()، حمّل best من PlayerPrefs باستخدام المفتاح BestScore، والقيمة 0 إذا لم يُحفَظ شيء. تعرض ShowHud() أيضًا Best: 0 في bestText، مع أفضل نتيجة.
  2. عندما تنتهي اللعبة بنتيجة أعلى من best، خزّنها في best، واحفظها باستخدام المفتاح BestScore واعرضها.

ثم اجعل اللعبة على ذوقك: غيّر السرعات، وعدد الأرواح، واحتمالية إسقاط العناصر، وقوة التعزيزات، والعب حتى تصبح كما تريد.

اضغط Play، وسجّل أفضل نتيجة، ثم اضغط Check.

جرّب بنفسك

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 iconاختبر نفسك

يتضمن هذا الدرس اختبارًا قصيرًا. ابدأ الدرس للإجابة عليه وتتبّع تقدمك.

جميع دروس لعبة Unity: تحطيم الطوب