using UnityEngine; using System.Collections; using System.Collections.Generic; using…
- User
using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class AnswerQuestion : MonoBehaviour {
class Question { public string QuestionContent { get; set; } public List<string> Answers { get; set; } public Question(string content, List<string> answerList) { QuestionContent = content; Answers = answerList; } } List<Question> questionList = new List<Question>(); Question currentQuestion = null; public Text QuestionText; public Text AnswerTextA; public Text AnswerTextB; public Text AnswerTextC; public Text AnswerTextD; public Button AnswerButtonA; public Button AnswerButtonB; public Button AnswerButtonC; public Button AnswerButtonD; string answer; int correct = 0; int wrong = 0; ColorBlock green = new ColorBlock(); ColorBlock red = new ColorBlock(); ColorBlock normal = new ColorBlock(); public Text FinalText; public GameObject MainSet; // Use this for initialization первый ответ всегда верный void Start() { questionList.Add(new Question("Кто главный герой мультсериала \"Губка Боб Квадратные штаны\"?", new List<string> { "Губка Боб", "Сквидвард", "Патрик", "Планктон"})); questionList.Add(new Question("Как зовут собаку Микки Мауса в мультфильме \"Клуб Микки Мауса\"?", new List<string> { "Плуто", "Гуфи", "Минни Маус", "Дональд Дак"})); questionList.Add(new Question("Какой мультяшный супергерой носит красную накидку и умеет летать?", new List<string> { "Супермен", "Бэтмен", "Человек-паук", "Железный человек"})); questionList.Add(new Question("В мультсериале \"Том и Джерри\" кто постоянно пытается поймать мышонка Джерри?", new List<string> { "Кот Том", "Кузен Джерри", "Кот Бутч", "Бульдог Спайк"})); newQ(); green = AnswerButtonA.colors; red = AnswerButtonA.colors; normal = AnswerButtonA.colors; green.normalColor = Color.green; green.pressedColor = Color.green; green.highlightedColor = Color.green; green.disabledColor = Color.green; red.normalColor = Color.red; red.pressedColor = Color.red; red.highlightedColor = Color.red; red.disabledColor = Color.red; } float timeToWait = 0; bool changeTime = false; bool returnTime = false; void Update() { timeToWait -= Time.deltaTime; if (timeToWait < 0 && changeTime) { AnswerButtonA.colors = normal; AnswerButtonB.colors = normal; AnswerButtonC.colors = normal; AnswerButtonD.colors = normal; newQ(); changeTime = false; } if (timeToWait < 0 && returnTime) { SceneManager.LoadScene("Main"); returnTime = false; } } void newQ() { if (questionList.Count != 0) { currentQuestion = questionList[(new System.Random()).Next(questionList.Count)]; answer = currentQuestion.Answers[0]; QuestionText.text = currentQuestion.QuestionContent + " (C:" + correct + " W:" + wrong + ")"; var availableAnswers = currentQuestion.Answers; List<int> notIncluded = new List<int>(); var curr = Random.Range(0, 4); while (notIncluded.Contains(curr)) { curr = Random.Range(0, 4); } notIncluded.Add(curr); AnswerTextA.text = "" + availableAnswers[curr]; while (notIncluded.Contains(curr)) { curr = Random.Range(0, 4); } notIncluded.Add(curr); AnswerTextB.text = "" + availableAnswers[curr]; while (notIncluded.Contains(curr)) { curr = Random.Range(0, 4); } notIncluded.Add(curr); AnswerTextC.text = "" + availableAnswers[curr]; while (notIncluded.Contains(curr)) { curr = Random.Range(0, 4); } notIncluded.Add(curr); AnswerTextD.text = "" + availableAnswers[curr]; } else { MainSet.SetActive(false); FinalText.gameObject.SetActive(true); FinalText.text = string.Format("Ты правильно ответил(а) {0} и ошибся(лась) {1} раз!", correct, wrong); changeTime = false; returnTime = true; timeToWait = 5; } } void commonStuff(bool isCorrect, Button currentButton) { questionList.Remove(currentQuestion); if (isCorrect) { correct++; currentButton.colors = green; } else { wrong++; currentButton.colors = red; if (AnswerTextA.text == answer) { AnswerButtonA.colors = green; } else if (AnswerTextB.text == answer) { AnswerButtonB.colors = green; } else if (AnswerTextC.text == answer) { AnswerButtonC.colors = green; } else if (AnswerTextD.text == answer) { AnswerButtonD.colors = green; } } timeToWait = 1.5f; changeTime = true; } public void AnswerA() { if (!changeTime) { commonStuff((AnswerTextA.text == answer), AnswerButtonA); } } public void AnswerB() { if (!changeTime) { commonStuff((AnswerTextB.text == answer), AnswerButtonB); } } public void AnswerC() { if (!changeTime) { commonStuff((AnswerTextC.text == answer), AnswerButtonC); } } public void AnswerD() { if (!changeTime) { commonStuff((AnswerTextD.text == answer), AnswerButtonD); } } - Assistant
This script is used to handle answering questions in a quiz game.
First, the script defines a nested class called "Question" which represents a single question in the quiz game. Each question has a question content string and a list of possible answers.
The script then creates a list of Question objects called "questionList" and initializes it with several questions and their corresponding answers.
- User
In this code, you need to add a timer visualization on the game screen. 20 seconds for one question, if the time ends, the game ends and the transition to the initial scene occurs, the timer updates the time on each question.
- Assistant
- User
void commonStuff(bool isCorrect, Button currentButton) { questionList.Remove(currentQuestion);
if (isCorrect) { correct++; currentButton.colors = green; } else { wrong++; currentButton.colors = red; if (AnswerTextA.text == answer) { AnswerButtonA.colors = green; } else if (AnswerTextB.text == answer) { AnswerButtonB.colors = green; } else if (AnswerTextC.text == answer) { AnswerButtonC.colors = green; } else if (AnswerTextD.text == answer) { AnswerButtonD.colors = green; } } timeToWait = 1.5f; changeTime = true; }what's going on here? - Assistant
The
commonStuffmethod is called when a button is clicked to handle the logic for both correct and incorrect answers.Here's a breakdown of what's happening in the
commonStuffmethod:-
The current question is removed from the questionList using
questionList.Remove(currentQuestion). -
If the answer provided is correct (
isCorrectis true), thecorrectcounter is incremented, and the color of thecurrentButtonis set to green usingcurrentButton.colors = green. -
If the answer provided is incorrect (else statement), the
wrongcounter is incremented, and the color of thecurrentButtonis set to red usingcurrentButton.colors = red. Additionally, if the incorrect answer corresponds toAnswerTextA,AnswerButtonAis set to green. If it corresponds toAnswerTextB,AnswerButtonBis set to green, and so on.
-