import UIKit
import AVFoundation import ImageIO
class ViewController: UIViewController { @IBOutlet weak var questionsView: UILabel! @IBOutlet weak var progressBar: UIView! @IBOutlet weak var scoreCounter: UILabel! @IBOutlet weak var questionCounter: UILabel! @IBOutlet weak var topDancer: UIImageView! @IBOutlet weak var bottomDancer: UIImageView! @IBOutlet weak var topFinalScore: UILabel! @IBOutlet weak var bottomFinalScore: UILabel!
//Outlet for buttons
@IBOutlet weak var optionA: UIButton!
@IBOutlet weak var optionB: UIButton!
@IBOutlet weak var optionC: UIButton!
@IBOutlet weak var optionD: UIButton!
var audioPlayer = AVAudioPlayer()
let allQuestions = QuestionBank()
var questionNumber: Int = 0
var score: Int = 0
var selectedAnswer: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
updateQuestion()
updateUI()
topDancer.loadGif(name: "ChickenDance")
bottomDancer.loadGif(name: "ChickenDance")
topDancer.isHidden = true
bottomDancer.isHidden = true
topFinalScore.isHidden = true
bottomFinalScore.isHidden = true
self.bottomDancer.transform = CGAffineTransform(scaleX: -1, y: 1);
// Do any additional setup after loading the view.
// Begin playing music
let path = Bundle.main.path(forResource: "Chicken", ofType: "mp3")!
let url = URL(fileURLWithPath: path)
do {
audioPlayer = try AVAudioPlayer(contentsOf: url)
audioPlayer.play()
audioPlayer.numberOfLoops = 10 - 1
} catch {
print(error)
}
}
@IBAction func answerPressed(_ sender: UIButton) {
if sender.tag == selectedAnswer {
print("correct")
score += 1
}else{
print("wrong")
}
questionNumber += 1
updateQuestion()
} func updateQuestion(){ if questionNumber < allQuestions.list.count{ questionsView.text = allQuestions.list[questionNumber].question optionA.setTitle(allQuestions.list[questionNumber].optionA, for: UIControl.State.normal) optionB.setTitle(allQuestions.list[questionNumber].optionB, for: UIControl.State.normal) optionC.setTitle(allQuestions.list[questionNumber].optionC, for: UIControl.State.normal) optionD.setTitle(allQuestions.list[questionNumber].optionD, for: UIControl.State.normal) selectedAnswer = allQuestions.list[questionNumber].correctAnswer
} else {
topDancer.isHidden = false
bottomDancer.isHidden = false
topFinalScore.isHidden = false
bottomFinalScore.isHidden = false
// let fart = Bundle.main.path(forResource: "Fart sound effect", ofType: "mp3")!
// let fartnoise = URL(fileURLWithPath: fart)
// audioPlayer = try AVAudioPlayer(contentsOf: fartnoise)
let alert = UIAlertController(title: "(∩ ͡° ͜ʖ ͡°)⊃━☆゚. * ・ 。゚. * ", message: "The quiz is now over. If you scored below a 32 (75%) then you FAILED. Press 'Restart' to try again or 'Quit' because you suck.", preferredStyle: .alert)
let restartAction = UIAlertAction(title: "Restart", style: .default, handler: {action in self.restartQuiz()})
alert.addAction(restartAction)
present(alert, animated: true, completion: nil)
let quitAction = UIAlertAction(title: "Quit", style: .default, handler: {action in self.quitQuiz()})
alert.addAction(quitAction)
}
updateUI()
}
func updateUI(){
scoreCounter.text = "Score: \(score)"
topFinalScore.text = "FINAL SCORE: \(score)"
bottomFinalScore.text = "FINAL SCORE: \(score)"
questionCounter.text = "\(questionNumber + 1)/\(allQuestions.list.count)"
progressBar.frame.size.width = (view.frame.size.width / CGFloat(allQuestions.list.count)) * CGFloat(questionNumber + 1)
}
func restartQuiz(){
score = 0
questionNumber = 0
updateQuestion()
topDancer.isHidden = true
bottomDancer.isHidden = true
topFinalScore.isHidden = true
bottomFinalScore.isHidden = true
}
func quitQuiz(){
exit(-1)
}
}