翻牌記憶遊戲(無圖片翻轉)
Published in
8 min readNov 7, 2020
OutLetCollection
陣列
append
stuffle
迴圈
Timer
button.tag
button.setImage
button.setTitle
Class
if else
@IBOutlet var buttons: [UIButton]!//每個buttons的圖片名稱,和真到迪米,和真到迪米var imageViewNames = ["和真","昂","帕克","惠惠","艾蜜莉亞","阿克婭","貝蒂","羅茲瓦爾","雷姆","拉姆","雅兒貝德","達克妮絲","夏提雅","安茲","迪米烏哥斯"]//每個buttons的 識別ID,0~14,0~14 用來比對是否一樣var identifiers = [Int]()//每個buttons的圖片名稱與識別ID 對應起來 和真 配 0 迪米 配14var cards = [Card]()var cardOne = Card() //第一張牌var cardTwo = Card() //第二張牌var buttonsTag = [Int]() //給buttons 0~29的位置編號var tagOne = 0 //第一張牌的button位置var tagTwo = 0 //第二張牌的button位置
//將 數字0~14放進陣列for n in 0...14 {identifiers.append(n)}//使陣列的內容重複兩次,使兩個陣列內容完整 與 buttons 數量一致for i in 0...14 {imageViewNames.append(imageViewNames[i])identifiers.append(identifiers[i])}//將每個buttons的圖片名稱與識別ID 對應起來 加到cards陣列for i in 0...29 {let newElement = Card()newElement.imgaeName = imageViewNames[i]newElement.identifier = Int(identifiers[i])cards.append(newElement)//將卡片帶入buttons,設定每個button的標題,用來對比是否翻到相同的牌buttons[i].setTitle("\(cards[i].identifier)", for: UIControl.State.normal)//給buttons 0~29的位置編號buttonsTag.append(i)buttons[i].tag = buttonsTag[i]}
//sender為當下按的那個button@IBAction func buttonClip(_ sender: UIButton) {if cardOne.isOpen == true { //判斷是否打開第一張牌,有的話進行配對cardTwo.identifier = Int(sender.currentTitle!)! //將按鈕當下的ID 存到 第二張牌的ID 等等用來對比是否相同cardTwo.isOpen = true //打開第二張牌tagTwo = sender.tag //將按鈕當下的位置 存到 變數tagTwo 記錄起來sender.setImage(UIImage(named: cards[tagTwo].imgaeName), for: UIControl.State.normal)//按鈕當下的照片為按鈕當下位置的卡片if cardOne.identifier == cardTwo.identifier , tagOne != tagTwo {var countTime = 0var timerWin = Timer()timerWin = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { [self] (Timer) inbuttons[tagOne].isEnabled = falsebuttons[tagTwo].isEnabled = falsecountTime += 1if countTime >= 0 {timerWin.invalidate()}}else{var countTimeTwo = 0var timerFail = Timer()timerFail = Timer.scheduledTimer(withTimeInterval: 0.35, repeats: true) { [self] (Timer) inbuttons[tagOne].setImage(UIImage(named: "問號"), for: UIControl.State.normal)buttons[tagTwo].setImage(UIImage(named: "問號"), for: UIControl.State.normal)countTimeTwo += 1if countTimeTwo >= 0 {timerFail.invalidate()}}}}
if cardTwo.isOpen == true { //判斷是否已經打開第二張牌,有的話要重新開始計cardTwo.identifier = 0cardTwo.isOpen = falsecardOne.identifier = 0cardOne.isOpen = false}else{ //第一張、第二張都沒打開的時候,執行打開第一張cardOne.identifier = Int(sender.currentTitle!)! //將按鈕當下的ID 存到 第一張牌的ID 等等用來對比是否相同cardOne.isOpen = true //打開第一張牌tagOne = sender.tag //將按鈕當下的位置 存到 變數tagOne 記錄起來sender.setImage(UIImage(named: cards[tagOne].imgaeName), for: UIControl.State.normal)//按鈕當下的照片為按鈕當下位置的卡片}}
@IBAction func replay(_ sender: Any) {cards.shuffle()for i in 0...29 {buttons[i].setImage(UIImage(named: "問號"), for: UIControl.State.normal)buttons[i].setTitle("\(cards[i].identifier)", for: UIControl.State.normal)buttons[i].isEnabled = true}}
@IBAction func peep(_ sender: Any) {if cardOne.isOpen == false {for i in 0...29 {buttons[i].setImage(UIImage(named: cards[i].imgaeName), for: UIControl.State.normal)}var count = 0var timer = Timer()timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true , block: { [self] (Timer) infor i in 0...29 {if buttons[i].isEnabled == true {self.buttons[i].setImage(UIImage(named:"問號"), for: UIControl.State.normal)}}count += 1if count >= 0 {timer.invalidate()}})}}