Advertisement
Szkaplerny

Unity code

Apr 6th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. public class TriviaGame : MonoBehaviour {
  6.  
  7.     public struct Question
  8.     {
  9.         public string questionText;
  10.         public string[] answers;
  11.         public int correctAnswerIndex;
  12.         public Question(string questionText, string[] answers, int correctAnswer)
  13.         {
  14.             this.questionText = questionText;
  15.             this.answers = answers;
  16.             this.correctAnswerIndex = correctAnswer;
  17.         }
  18.     }
  19.     Question currentQuestion = new Question("Hi?", new string[] { "y", "p", "z", "g", "e"}, 2);
  20.  
  21.     public Button[] answerButtons;
  22.     public Text questionText;
  23.     private Question[] questions = new Question[10];
  24.     private int currentQuestionIndex;
  25.     private int[] questionNumbersChoosen = new int[5];
  26. //    private int questionsFinished;
  27.  
  28.  
  29.     void Start () {
  30.         questions[0] = new Question("What is the capital of Spain?", new string[] { "Topeka", "Amsterdam", "Madrid", "London", "Toledo" }, 2);
  31.         questions[1] = new Question("Who was the second US president?", new string[] { "Thomas Jefferson", "John Adams", "Bill Clinton", "George Washington", "Abraham Lincon" }, 1);
  32.         questions[2] = new Question("What is the second planet in our solor system?", new string[] { "Mercury", "Earth", "Saturn", "Venus", "Pluto" }, 3);
  33.         questions[3] = new Question("What is the largest continent?", new string[] { "Africa", "North America", "Asia", "Europe", "Austalia" }, 2);
  34.         questions[4] = new Question("What US state has the hightest population?", new string[] { "California", "Florida", "Texas", "New York", "North Carolina" }, 0);
  35.         questions[5] = new Question("A Platypus is a _____", new string[] { "Bird", "Reptile", "Insect", "Amphibian", "Mammal" }, 4);
  36.         questions[6] = new Question("What is the boiling tempurature in fahrenheit?", new string[] { "100 degrees", "190 degrees", "300 degrees", "312 degreesn", "212 degrees" }, 4);
  37.         questions[7] = new Question("How many degrees are in a circle?", new string[] { "360", "180", "640", "16", "270" }, 0);
  38.         questions[8] = new Question("What is a name for a group of crows?", new string[] { "A bloat", "A herd", "A pack", "A murder", "A team" }, 3);
  39.         questions[9] = new Question("Who created the painting starry night?", new string[] { "Pablo Picasso", "Vincent van Gogh", "Andy Warhol", "Leonardo da Vinci", "Frida Kahlo" }, 1);
  40.  
  41.         chooseQuestion();
  42.         assignQuestion(questionNumbersChoosen[0]);
  43.     }
  44.  
  45.     void Update () {
  46.    
  47.     }
  48.  
  49.     void assignQuestion(int questionNum)
  50.     {
  51.         currentQuestion = questions[questionNum];
  52.         questionText.text = currentQuestion.questionText;
  53.         for (int i = 0; i < answerButtons.Length; i++)
  54.         {
  55.             answerButtons[i].GetComponentInChildren<Text>().text = currentQuestion.answers[i];
  56.         }
  57.     }
  58.  
  59.     public void checkAnswer(int buttonNumber)
  60.     {
  61.         if(buttonNumber == currentQuestion.correctAnswerIndex)
  62.         {
  63.             print("you are correct!");
  64.         }
  65.         else
  66.         {
  67.             print("nope ;(");
  68.         }
  69. //        if(questionsFinished < questionNumbersChoosen.Length - 1)
  70.         {
  71.             moveToNextQuestion();
  72. //            questionsFinished++;
  73.         }
  74.     }
  75.  
  76.     void chooseQuestion()
  77.     {
  78.         for(int i = 0; i < questionNumbersChoosen.Length; i++)
  79.         {
  80.             int questionNum = Random.Range(0, questions.Length);
  81.             if(numberNotContained(questionNumbersChoosen, questionNum))
  82.             {
  83.                 questionNumbersChoosen[i] = questionNum;
  84.             }
  85.             else
  86.             {
  87.                 i--;
  88.             }
  89.         }
  90.         currentQuestionIndex = Random.Range(0, questions.Length);
  91.     }
  92.  
  93.     bool numberNotContained(int[] numbers, int number)
  94.     {
  95.         for(int i = 0; i < numbers.Length; i++) // zakres 0-4
  96.         {
  97.             if(number == numbers[i])
  98.             {
  99.                 return false;
  100.             }
  101.         }
  102.         return true;
  103.     }
  104.  
  105.     public void moveToNextQuestion()
  106.     {
  107. //        assignQuestion(questionNumbersChoosen[questionNumbersChoosen.Length - 1 - questionsFinished]);
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement