來自「吃樂天小熊餅乾,救救無尾熊」的靈感 — 記憶遊戲

anan
彼得潘的 Swift iOS / Flutter App 開發教室
8 min readFeb 13, 2020

澳洲在2019年9月開始因最熱、最乾燥的氣候下產生的自然野火,約三個台灣面積大的森林遭到焚毀,及數十億隻野生動物(包含:無尾熊、袋鼠…等)葬身火海,造成嚴重的生態浩劫。

澳洲大火無尾熊面臨生存危險,澳洲推行「無尾熊助養計劃」,邀請世界各地的人一起來領養無尾熊,希望有好心人士能花小錢給無尾熊有更好的環境照顧牠們。

製作靈感:

某一天,台灣「LOTTE樂天小熊餅乾」官方證實消息,開心享用小熊餅乾同時也能幫助「澳洲無尾熊基金會」,一看到就直接去買一盒,進到教室,彼得潘看到我手上一盒就說可以做小熊餅乾的翻牌遊戲,因彼得潘一句話我就來做一個簡單版的記憶遊戲。

為了找可愛的圖片並且修圖,花兩小時左右的時間完成。

看到可愛小熊餅乾的圖案,心情就很好,更希望受傷的無尾熊及袋鼠都能有溫暖的環境照顧牠們。

程式:

點擊BearCard,會有翻牌動作,並先確認兩個圖案是否相同,相同則會反灰,無法再次點擊。

//翻牌(是否相同)
func disAbleBear(index: Int) -> Void {
bears[index].isAlive = false
bearButtons[index].alpha = 0.4
UIView.transition(with: bearButtons[index], duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: nil)
}

func displayBears() -> Void {
for (i,_) in bearButtons.enumerated() {
if bears[i].isAlive == true{
if bears[i].isFlipped == true {
bearButtons[i].setImage(bears[i].image, for: .normal)
}else{
bearButtons[i].setImage(UIImage(named: "Back.png"), for: .normal)
}
}else{
bearButtons[i].setImage(bears[i].image, for: .normal)
bearButtons[i].alpha = 0.4
}
}
}
//翻牌動作
@IBAction func flipBear(_ sender: UIButton) {

func flipBearIndex(index: Int) -> Void {
if bears[index].isFlipped == true {
bearButtons[index].setImage(UIImage(named: "Back.png"), for: .normal)
UIView.transition(with: bearButtons[index], duration: 0.5, options: .transitionFlipFromRight, animations: nil, completion: nil)
bears[index].isFlipped = false
} else {
bearButtons[index].setImage(bears[index].image, for: .normal)
UIView.transition(with: bearButtons[index], duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: nil)
bears[index].isFlipped = true
}
}
if time == nil{
time = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.UpdateTimer), userInfo: nil, repeats: true)
}
if let bearIndex = bearButtons.firstIndex(of: sender){
if bears[bearIndex].isAlive == false{
return
}
if selectedBears.count == 0 {
selectedBears.append(bearIndex)
flipBearIndex(index: bearIndex)
}else if selectedBears.count == 1 {
moves += 1
movesMade.text = String(moves)
if selectedBears.contains(bearIndex) {
flipBearIndex(index: bearIndex)
selectedBears.removeAll()
}else{
selectedBears.append(bearIndex)
flipBearIndex(index: bearIndex)
if bears[selectedBears[0]].name == bears[selectedBears[1]].name{
//disable
Timer.scheduledTimer(withTimeInterval: 0.7, repeats: false){ (_) in
for (_,num) in self.selectedBears.enumerated() {
self.disAbleBear(index: num)
}
self.selectedBears.removeAll()
self.pairsFound += 1
if self.pairsFound == 8 {
self.time?.invalidate()
//在時間內完成,會跳出視窗訊息視窗,按下ok會直接重新開始
let controller = UIAlertController(title: "挑戰成功", message: "再來一場!!", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default) { _ in
self.restartAction()
}
controller.addAction(okAction)
//在時間內結束時間也會跟著暫停
self.present(controller, animated: true, completion: nil)
print("game end")

}
}
}else{
Timer.scheduledTimer(withTimeInterval: 0.7, repeats: false) {
(_) in for(_,num) in self.selectedBears.enumerated() {
flipBearIndex(index: num)
}
self.selectedBears.removeAll()

}
}
}
}
}
}

遊戲在時間內完成,則會跳出訊息視窗,並且停止時間,點下 “ ok ” ,遊戲會直接重新開始,時間也會從 60 秒開始。

//未在時間內完成遊戲,而會跳出警告訊息,按下ok則會重新開始
@objc func UpdateTimer() {
seconds = seconds - 1
if seconds == 0{
time?.invalidate()
time = nil
isPlaying = false
let controller = UIAlertController(title: "挑戰失敗", message: "回家練練再來吧!", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default) { _ in
self.restartAction()
}
controller.addAction(okAction)
present(controller, animated: true, completion: nil)
}
timeLabel.text = String(seconds)
}

Github

--

--