Unity 3D随机问答游戏
本文关键字:游戏 问答 随机 3D Unity | 更新日期: 2023-09-27 18:11:29
所以我有10个问题,所以当游戏开始时,例如开始问题是"4/10",然后如果下一个问题是随机的"10/strong"游戏结束。我想随机选出10个问题:
private int idMode;
public Text question;
public Text answerA;
public Text answerB;
public Text answerC;
public Text answerD;
public Text infoAnswer;
public Text stat;
public string[] questions;
public string[] alternativeA;
public string[] alternativeB;
public string[] alternativeC;
public string[] alternativeD;
public string[] correct;
private int idQuestion;
private float points;
private float fact;
private float average;
private int results;
void Start () {
idMode = PlayerPrefs.GetInt ("idMode");
idQuestion = 0;
fact = questions.Length;
question.text = questions [idQuestion];
answerA.text = alternativeA [idQuestion];
answerB.text = alternativeB [idQuestion];
answerC.text = alternativeC [idQuestion];
answerD.text = alternativeD [idQuestion];
infoAnswer.text = (idQuestion + 1).ToString() + " of " + fact.ToString () + "";
}
public void answer(string alternative)
{
if (alternative == "A") {
if (alternativeA [idQuestion] == correct [idQuestion]) {
points += 1;
} else {
}
}
if (alternative == "B") {
if (alternativeB [idQuestion] == correct [idQuestion]) {
points += 1;
} else {
}
}
if (alternative == "C") {
if (alternativeC [idQuestion] == correct [idQuestion]) {
points += 1;
} else {
}
}
if (alternative == "D") {
if (alternativeD [idQuestion] == correct [idQuestion]) {
points += 1;
} else {
}
}
nextQuestion ();
}
void nextQuestion()
{
idQuestion += Random.Range(0,10);
if(idQuestion <= (fact-1))
{
question.text = questions [idQuestion];
answerA.text = alternativeA [idQuestion];
answerB.text = alternativeB [idQuestion];
answerC.text = alternativeC [idQuestion];
answerD.text = alternativeD [idQuestion];
stat.text = " Correct: " + points.ToString () + "";
infoAnswer.text = (idQuestion + 1).ToString() + " of " + fact.ToString () + "";
}
else
{
average = 10 * (points / fact);
results = Mathf.RoundToInt (average);
if (results > PlayerPrefs.GetInt ("results" + idMode.ToString ())) {
PlayerPrefs.SetInt ("results" + idMode.ToString (), results);
PlayerPrefs.SetInt ("points" + idMode.ToString (), (int)points);
}
PlayerPrefs.SetInt ("resultsTemp" + idMode.ToString (), results);
PlayerPrefs.SetInt ("pointsTemp" + idMode.ToString (), (int)points);
Application.LoadLevel("results");
}
}
}
改变你的数据结构,创建一个代表问题和可能答案的类,这样你就有一个数组而不是6个。
一旦你这样做了,在你开始提问之前,打乱列表,然后按照新的随机顺序浏览列表。
[Serializeable]
public class Question
{
public string Text;
public string A;
public string B;
public string C;
public string D;
public string CorrectChoice; //Holds "A", "B", "C", or "D"
}
public static class RandomExtensions
{
public static void Shuffle<T> (this T[] array)
{
int n = array.Length;
while (n > 1)
{
int k = Random.Range(0, n--);
T temp = array[n];
array[n] = array[k];
array[k] = temp;
}
}
}
然后将代码更改为
private int idMode;
public Text question;
public Text answerA;
public Text answerB;
public Text answerC;
public Text answerD;
public Text infoAnswer;
public Text stat;
public Question[] questions;
private int idQuestion;
private float points;
private float fact;
private float average;
private int results;
void Start () {
idMode = PlayerPrefs.GetInt ("idMode");
idQuestion = 0;
fact = questions.Length;
questions.Shuffle();
question.text = questions[idQuestion].Text;
answerA.text = questions[idQuestion].A;
answerB.text = questions[idQuestion].B;
answerC.text = questions[idQuestion].C;
answerD.text = questions[idQuestion].D;
infoAnswer.text = (idQuestion + 1).ToString() + " of " + fact.ToString () + "";
}
public void answer(string alternative)
{
if (alternative == questions[idQuestion].CorrectChoice)
{
points += 1;
}
nextQuestion ();
}
void nextQuestion()
{
idQuestion += Random.Range(0,10);
if(idQuestion <= (fact-1))
{
question.text = questions[idQuestion].Text;
answerA.text = questions[idQuestion].A;
answerB.text = questions[idQuestion].B;
answerC.text = questions[idQuestion].C;
answerD.text = questions[idQuestion].D;
stat.text = " Correct: " + points.ToString () + "";
infoAnswer.text = (idQuestion + 1).ToString() + " of " + fact.ToString () + "";
}
else
{
average = 10 * (points / fact);
results = Mathf.RoundToInt (average);
if (results > PlayerPrefs.GetInt ("results" + idMode.ToString ())) {
PlayerPrefs.SetInt ("results" + idMode.ToString (), results);
PlayerPrefs.SetInt ("points" + idMode.ToString (), (int)points);
}
PlayerPrefs.SetInt ("resultsTemp" + idMode.ToString (), results);
PlayerPrefs.SetInt ("pointsTemp" + idMode.ToString (), (int)points);
Application.LoadLevel("results");
}
}
如果你真的不想改变你的数据结构,这是我在注释中提到的关于创建映射数组的另一个选择。
private int idMode;
public Text question;
public Text answerA;
public Text answerB;
public Text answerC;
public Text answerD;
public Text infoAnswer;
public Text stat;
public string[] questions;
public string[] alternativeA;
public string[] alternativeB;
public string[] alternativeC;
public string[] alternativeD;
public string[] correct;
private int idQuestion;
private float points;
private float fact;
private float average;
private int results;
private int[] questionMapper;
void Start () {
idMode = PlayerPrefs.GetInt ("idMode");
idQuestion = 0;
fact = questions.Length;
questionMapper = new int[questions.Count];
for(int i = 0; i < questionMapper.Count; i++)
{
questionMapper[i] = i;
}
questionMapper.Shuffle();
question.text = questions [questionMapper[idQuestion]];
answerA.text = alternativeA [questionMapper[idQuestion]];
answerB.text = alternativeB [questionMapper[idQuestion]];
answerC.text = alternativeC [questionMapper[idQuestion]];
answerD.text = alternativeD [questionMapper[idQuestion]];
infoAnswer.text = (idQuestion + 1).ToString() + " of " + fact.ToString () + "";
}
//...
void nextQuestion()
{
idQuestion += Random.Range(0,10);
if(idQuestion <= (fact-1))
{
question.text = questions [questionMapper[idQuestion]];
answerA.text = alternativeA [questionMapper[idQuestion]];
answerB.text = alternativeB [questionMapper[idQuestion]];
answerC.text = alternativeC [questionMapper[idQuestion]];
answerD.text = alternativeD [questionMapper[idQuestion]];
stat.text = " Correct: " + points.ToString () + "";
infoAnswer.text = (idQuestion + 1).ToString() + " of " + fact.ToString () + "";
}
else
{
average = 10 * (points / fact);
results = Mathf.RoundToInt (average);
if (results > PlayerPrefs.GetInt ("results" + idMode.ToString ())) {
PlayerPrefs.SetInt ("results" + idMode.ToString (), results);
PlayerPrefs.SetInt ("points" + idMode.ToString (), (int)points);
}
PlayerPrefs.SetInt ("resultsTemp" + idMode.ToString (), results);
PlayerPrefs.SetInt ("pointsTemp" + idMode.ToString (), (int)points);
Application.LoadLevel("results");
}
}