#1 _Q&A問答題 | 猜猜大挑戰 App
Published in
9 min readOct 27, 2022
原本要製作雙人版 紅白猜題大賽…難產先來一個單人版 XD
| 功能簡介
- Slider (顯示題數進度條)
- Label (顯示題目、分數、提示)
- Button (選項、下一題、在玩一次)
- 點選Button 變色 按下一題 恢復原狀
- 答對一題加 10 分
- 畫面上顯示目前題目是第幾題
- 題目順序跟選項順序每次出現時都要不一樣
- 訊息視窗應用(詢問是否再玩一次)
| Swift 程式碼
Code of QuestionViewController.swift
// ViewController.swift// // Created by aaron on 2022/10/18.//import UIKitclass ViewController: UIViewController {@IBOutlet weak var questionNumberLabel: UILabel!@IBOutlet weak var scoreLabel: UILabel!@IBOutlet weak var questionLabel: UILabel!@IBOutlet weak var hintLabel: UILabel!@IBOutlet weak var questionSlider: UISlider!@IBOutlet var answerButton: [UIButton]!var questions = [Question]()var index = 0var score = 0var count = 1var rightAnswer = ""override func viewDidLoad() {super.viewDidLoad()let question1 = Question(description: "白雪公主跟包公結婚,生下的女兒叫什麼名字?", answer: "灰姑娘", option: ["包姑娘","白姑娘","黑姑娘","灰姑娘"])questions.append(question1)let question2 = Question(description: "地球.太陽.星星.月亮哪一個是啞巴? (提示:魯冰花)", answer: "星星", option: ["月亮","太陽","星星","地球"])questions.append(question2)let question3 = Question(description: "把妹要開什麼車?", answer: "垃圾車", option: ["小貨車","進口車","國產車","垃圾車"])questions.append(question3)let question4 = Question(description: "麵包超人扭到腳會變成什麼?", answer: "牛角麵包", option: ["肉鬆麵包","花生麵包","牛角麵包","菠蘿麵包"])questions.append(question4)let question5 = Question(description: "哪種動物最熱情?(提示:熱情的沙漠)", answer: "黑豬", option: ["黑狗","黑豬","黑熊","黑貓"])questions.append(question5)let question6 = Question(description: "企鵝的台語是什麼?", answer: "徛鵝", option: ["去鵝","企鵝","不倒鵝","徛鵝"])questions.append(question6)let question7 = Question(description: "一歲的蜜蜂到了,一百歲變成甚麼?", answer: "高齡蜂", option: ["長壽蜂","龜齡蜂","高齡蜂","長命蜂"])questions.append(question7)let question8 = Question(description: "魚在水裡玩會變成什麼?", answer: "魚丸", option: ["美人魚","小丑魚","鹹魚","魚丸"])questions.append(question8)let question9 = Question(description: "怎麼讓珍珠奶茶變大杯的?", answer: "唸大悲咒", option: ["喝別人的","唸大悲咒","倒立喝茶","加水稀釋"])questions.append(question9)let question10 = Question(description: "國人不洗澡會變成什麼人?", answer: "泰國人", option: ["台灣人","日本人","印度人","泰國人"])questions.append(question10)let question11 = Question(description: "為什麼你交不到女朋友?", answer: "選擇不在我身上", option: ["我無所謂","我沒錢","我太帥了","選擇不在我身上"])questions.append(question11)let question12 = Question(description: "在麥當勞過生日點什麼?", answer: "薯條加漢堡", option: ["冰炫風","麥克雞塊","蘋果派","薯條加漢堡"])questions.append(question12)let question13 = Question(description: "哪個科系畢業最適合賣壽司?", answer: "美術系", option: ["外文系","電機系","醫學系","美術系"])questions.append(question13)let question14 = Question(description: " 小黑小白小紅小綠,四人搭飛機誰最會暈機?", answer: "小白", option: ["小黑","小紅","小綠","小白"])questions.append(question14)let question15 = Question(description: " 甲乙丙丁,哪一個字最酷?", answer: "丁", option: ["甲","乙","丙","丁"])questions.append(question15)let question16 = Question(description: " 哪種花最好養?", answer: "菊花", option: ["百合花","玫瑰花","菊花","蘭花"])questions.append(question16)questions.shuffle()startGame()}func startGame(){questions[index].option.shuffle()for i in 0...3{answerButton[i].setTitle(questions[index].option[i], for: .normal)}rightAnswer = questions[index].answerquestionLabel.text = questions[index].descriptionquestionSlider.value = Float(count)}@IBAction func clickAnswer(_ sender: UIButton) {sender.backgroundColor = .red// sender.backgroundColor = .systemGray3if sender.currentTitle == rightAnswer {score = score+10scoreLabel.text = "分數:\(score)"hintLabel.text = "恭喜答對,加分加分!"}else{hintLabel.text = "答錯了,(T_T). . .不用哭哭換下一題!"}}@IBAction func nextQuestion(_ sender: Any) {index = index+1count = count+1questionSlider.value = Float(count)hintLabel.text = ""questionNumberLabel.text = "第\(count)題"answerButton.forEach { button inbutton.backgroundColor = .systemGray3}if count == 11 {index = 0count = 1questionNumberLabel.text = "第\(count)題"score = 0scoreLabel.text = "分數:\(score)"questions.shuffle()}startGame()}func reset(){index = 0count = 1score = 0questionNumberLabel.text = "第\(count)題"scoreLabel.text = "分數:\(score)"startGame()}@IBAction func tryAgain(_ sender: Any) {let controller = UIAlertController(title: "再玩一次?", message: "資料關閉視窗後清除~~", preferredStyle: UIAlertController.Style.alert)let noAction = UIAlertAction(title: "取消", style: UIAlertAction.Style.default, handler: nil)controller.addAction(noAction)let yesAction = UIAlertAction(title: "確定", style: UIAlertAction.Style.default){_ inself.reset()}controller.addAction(yesAction)present(controller,animated:true,completion:nil)}}