Как мне сделать счетчик очков для игры в кости в C# для Unity? - PullRequest
0 голосов
/ 13 марта 2020

я пытаюсь сделать табло для счетчика кубиков, например, если я бросаю 1, вы получаете 1 очко к табло, если вы бросаете 6, 6 очков, добавленных к табло. Вот мой код для броска костей:

using System.Collections;
using UnityEngine;

public class Dice : MonoBehaviour
{
// Array of dice sides sprites to load from Resources folder
private Sprite[] diceSides;

// Reference to this sprite renderer to change sprites
private SpriteRenderer rend;

// Use this for initialization
private void Start()
{
    // Assign Sprite Renderer component
    rend = GetComponent<SpriteRenderer>();

    // Load dice sides sprites to array from DiceSides subfolder of Resources folder
    diceSides = Resources.LoadAll<Sprite>("DiceSides/");
}

// If you left click over the dice then RollTheDice coroitine is started
private void OnMouseDown()
{
    StartCoroutine("RollTheDice");
}

//coroutine that rolls the dice
private IEnumerator RollTheDice()
{
    //variable to contain random dice side number
    //it needs to be assigned, let it be 0 initially
    int randomDiceSide = 0;

    //Final side or value that dice reads in the end of coroutine
    int finalSide = 0;

    //loop to switch dice sides randomly
    //before final side appears, 20 itterations here
    for (int i = 0; i <= 20; i++)
    {
        // Pick up random value from 0 to 5 (all inclusive)
        randomDiceSide = Random.Range(0,5);

        //set sprite to upper face of dice from array according to random value
        rend.sprite = diceSides[randomDiceSide];

        // Pause before next itteration
        yield return new WaitForSeconds(0.05f);
    }

    // Assigning final side so you can use this value later in your game
    // for player movement for example
    finalSide = randomDiceSide + 1;

    // Show final dice value in console
    Debug.Log(finalSide);

    //Attempt to add to the score board in game
}
}

Также у меня есть код для табло, я просто не знаю, как связать его с «сторонами костей», вот оно:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreScript : MonoBehaviour
{
public static int scoreValue = 0;
Text score;

void Start()
{
    score = GetComponent<Text>(); 
}

// Update is called once per frame
void Update()
{
    score.text = "Score:" + scoreValue;
}
}

1 Ответ

0 голосов
/ 18 марта 2020

Так что, если я все понял правильно, то, что вы просите, это то, что если вы сначала бросаете кости, и они показывают 1, и вы бросаете кости снова, и теперь кости показывают 6. Вы хотите сложить их вместе так, Табло показывает 7? Если это так, то вы можете добавить:

        Scoreboard.scoreValue += finalSide;

внизу вашего скрипта, например:

using System.Collections;
using UnityEngine;

public class Dice : MonoBehaviour
{
// Array of dice sides sprites to load from Resources folder
private Sprite[] diceSides;

// Reference to this sprite renderer to change sprites
private SpriteRenderer rend;

// Use this for initialization
private void Start()
{
    // Assign Sprite Renderer component
    rend = GetComponent<SpriteRenderer>();

    // Load dice sides sprites to array from DiceSides subfolder of Resources folder
    diceSides = Resources.LoadAll<Sprite>("DiceSides/");
}

// If you left click over the dice then RollTheDice coroitine is started
private void OnMouseDown()
{
    StartCoroutine("RollTheDice");
}

//coroutine that rolls the dice
private IEnumerator RollTheDice()
{
    //variable to contain random dice side number
    //it needs to be assigned, let it be 0 initially
    int randomDiceSide = 0;

    //Final side or value that dice reads in the end of coroutine
    int finalSide = 0;

    //loop to switch dice sides randomly
    //before final side appears, 20 itterations here
    for (int i = 0; i <= 20; i++)
    {
        // Pick up random value from 0 to 5 (all inclusive)
        randomDiceSide = Random.Range(0, 5);

        //set sprite to upper face of dice from array according to random value
        rend.sprite = diceSides[randomDiceSide];

        // Pause before next itteration
        yield return new WaitForSeconds(0.05f);
    }

    // Assigning final side so you can use this value later in your game
    // for player movement for example
    finalSide = randomDiceSide + 1;

    // Show final dice value in console
    Debug.Log(finalSide);
    Scoreboard.scoreValue += finalSide;
    //Attempt to add to the score board in game
   }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...