#5 Hw3 創作有梗的IOS App遊戲

游傑如
海大 SwiftUI iOS / Flutter App 程式設計
11 min readMay 12, 2019

要想出一個有梗的遊戲真的很難很難很難QQ

Btw 我還沒想出一個適合他的題目名稱…

  1. App操作畫面

2. github

3.功能說明

(1) 多個頁面。

(2) 定義一個型別 Item

class item{var canTouch:Boolvar type:Intvar clicked:Boolinit(){  canTouch = false  type = 0  clicked = false}init(canTouch:Bool,type:Int){  self.canTouch = canTouch   self.type = type  clicked = false}func change(canTouch:Bool,type:Int){  self.canTouch = canTouch  self.type = type  clicked = false}func click(){  clicked = true  }}

(3). 利用 function prepare 傳資料,把難度模式傳給 PlayViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {   let des = segue.destination as? PlayViewController   des?.mode = self.mode}

(4) 使用 UIAlertController ,在遊戲結束時顯示出訊息框,可決定再玩一次或回首頁

func alert(){let controller = UIAlertController(title: "QQ 結束了", message:"你把重要的東西都趕跑了!" , preferredStyle: .alert)let restartAction = UIAlertAction(title:"再上班一次",style: .default){(_) inself.backMode()}let cancelAction = UIAlertAction(title: "下班囉", style: .cancel){(_)inself.backHome()}controller.addAction(restartAction)controller.addAction(cancelAction)present(controller,animated: true,completion: nil)}

(4–1) 導回前個頁面 (把現存 controller pop 掉)

func backMode(){_ = navigationController?.popViewController(animated: true)}func backHome(){_ = navigationController?.popToRootViewController(animated: true)}

(5)利用 Timer 實現計時或倒數功能。

一個是依照模式難度在某固定時間換圖片

changeTimer = Timer.scheduledTimer(withTimeInterval: timeInt, repeats: true){(_) inif(self.alive == false){self.clickButton.isEnabled = falseself.changeTimer?.invalidate()self.alert()}else{//self.timeScore+=1//self.playTime.text = String(self.timeScore) + " s"//print("change")if(self.showItem.clicked==false && self.showItem.canTouch==true){self.heart[0].removeFromSuperview()self.heart.remove(at: 0)if(self.heart.count==0){self.alive = false}else{self.setItem()}}else if(self.showItem.clicked==true && self.showItem.canTouch==false){self.heart[0].removeFromSuperview()self.heart.remove(at: 0)if(self.heart.count==0){self.alive = false}else{self.setItem()}}else{self.setItem()}}}

一個是計時功能

countTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true){(_) inif(self.alive == false){self.countTimer?.invalidate()let tmp = self.userDefaults.integer(forKey: "time"+String(self.mode))if (self.timeScore > tmp){self.userDefaults.set(self.timeScore,forKey:"time"+String(self.mode))}print(self.userDefaults.integer(forKey: "time"+String(self.mode)))}else{self.timeScore+=1self.playTime.text = String(self.timeScore) + " s"}}

(6) 利用 UIViewPropertyAnimator 製作動畫效果。(在HomeViewController 及 ModeViewController 控制按鈕動畫出現)

UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 1, delay: 0, animations:{self.playButton.transform = CGAffineTransform(translationX: 0, y: -400.0)},completion: nil)UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 1, delay: 0, animations:{self.ruleButton.transform = CGAffineTransform(translationX: 0, y: -300.0)},completion: nil)UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 1, delay: 0, animations:{self.scoreButton.transform = CGAffineTransform(translationX: 0, y: -200.0)},completion: nil)

(7) 透過 present 顯示 SFSafariViewController,點擊 info 按鈕顯示海大 IOS medium 頁面

@IBAction func infoClick(_ sender: Any) {if let url = URL(string: "https://medium.com/%E6%B5%B7%E5%A4%A7-ios-app-%E7%A8%8B%E5%BC%8F%E8%A8%AD%E8%A8%88"){let controller = SFSafariViewController(url: url)present(controller,animated: true)}}

(8) 用 UserDefaults 儲存簡單秒數紀錄,顯示在成績頁面上。

儲存

let tmp = self.userDefaults.integer(forKey: "time"+String(self.mode))if (self.timeScore > tmp){self.userDefaults.set(self.timeScore,forKey:"time"+String(self.mode))}

取值

var tmp:Inttmp = UserDefaults.standard.integer(forKey: "time0")easyScore.text = String(tmp) + " s"tmp = UserDefaults.standard.integer(forKey: "time1")normalScore.text = String(tmp) + " s"tmp = UserDefaults.standard.integer(forKey: "time2")hardScore.text = String(tmp) + " s"

(9) 加入通知提醒功能 , 一段時間跳出提醒

AppDelegate 中詢問是否允許通知

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {// Override point for customization after application launch.UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {granted, error inif granted {print("想要收到提醒")}else{print("不想要提醒")}})return true}

HomeViewController 中

let sendContent = UNMutableNotificationContent()sendContent.title = "快來上班囉"sendContent.subtitle = "小偷已經快要跑進來了QQ"sendContent.body = "再不來準備扣薪水了好嗎?"sendContent.badge = 1sendContent.sound = UNNotificationSound.defaultlet trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)let request = UNNotificationRequest(identifier: "notification1", content: sendContent, trigger: trigger)UNUserNotificationCenter.current().add(request,withCompletionHandler: nil)

(9) 利用 AudioToolbox 播放遊戲音效

@IBAction func touched(_ sender: UIButton) {if(showItem.canTouch==true){AudioServicesPlaySystemSound(1057)}else{AudioServicesPlaySystemSound(1009)}showItem.click()}

老實說,原本想要自己畫元件的,可是畫不出來哈哈哈哈哈哈,有夠難的

覺得美化類設計類人才真的太猛了……..

--

--