斯坦福Stanford公开课2017年版CS193p课程iOS11开发01-翻牌游戏

斯坦福Stanford公开课2017年版CS193p课程iOS11开发01-翻牌游戏,第1张

翻牌游戏

老师主要运用的是storyboard来编写程序,但说实话,真的用起来很不习惯。

还是用回纯代码编写吧。

主要知识点:

观察属性@objc标签
class ViewController: UIViewController {
    
    var count = 0 {
        didSet{
            scoreLabel.text = "消耗步数:\(count)"
        }
    }
    let fruitKindArray = ["🍎","🍉","🍊","🍓","🍈","🍇","🥝","🥑","🌰","🍑","🍌","🍒","🍏","🍍","🍐","🍋","🥬","🌶️"]
    let animalKindArray = ["🐒","🦁️","🐯","🐔","🐶","🐍","🐷","🐎","🐑","🐫","🐭","🐲","🐰","🐱","🐘","🐂","🦎","🦔"]
    var hang:Int = 7
    var lie:Int = 4
    lazy var mysteriousDataSource = animalKindArray
    lazy var mysteriousArray = Array.init(repeating: "", count: hang*lie)
    var matchingArray:Array<UIButton> = Array.init()
    var matchingCount = 0 {
        didSet{
            if(matchingCount == hang*lie){
                let fenmu = Double(count)
                let fenzi = Double(count - hang*lie)
                let score = 1.0 - fenzi/fenmu
                let scoreString = String(format: "%.2f", score*100)
                scoreLabel.text = "得分:\(scoreString)"
            }
        }
    }
    
    var scoreLabel:UILabel = UILabel.init()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //对数据源初始化
        initMysteriousArray()
        //对计数界面初始化
        createScoreInterface()
        createNewGameButton()
        //对界面初始化
        createCardsInterface()
        
    }
    
    func initMysteriousArray(){
        mysteriousArray = Array.init(repeating: "", count: hang*lie)
        for _ in 0 ..< Int(hang*lie/2) {
            let kindIndex = Int(arc4random_uniform(UInt32(mysteriousDataSource.count)))
            let kindString = mysteriousDataSource[kindIndex]
            var times = 2
            repeat{
                //取出数组中随机的位置,看是否为空
                let inputIndex = Int(arc4random_uniform(UInt32(mysteriousArray.count)))
                if(mysteriousArray[inputIndex] == ""){
                    mysteriousArray[inputIndex] = kindString
                    times -= 1
                }
            }while(times>0)
        }
    }
    
    func createScoreInterface(){
        let screenSize = UIScreen.main.bounds.size
        scoreLabel.frame = CGRect.init(x: 0, y: 20, width: screenSize.width*0.65, height: 90-20)
        scoreLabel.font = UIFont.systemFont(ofSize: 25)
        scoreLabel.textColor = UIColor.label
        scoreLabel.textAlignment = NSTextAlignment.center
        self.view.addSubview(scoreLabel)
        count = 0
    }
    
    func createNewGameButton(){
        let screenSize = UIScreen.main.bounds.size
        let newGameButtonFrame = CGRect.init(x: screenSize.width*0.65, y: 35, width: screenSize.width*0.30, height: 90-50)
        let newGameButton = UIButton.init(frame:newGameButtonFrame)
        newGameButton.setTitle("New Game", for: UIControl.State.normal)
        newGameButton.backgroundColor = UIColor.blue
        newGameButton.titleLabel?.textColor = UIColor.white
        newGameButton.layer.cornerRadius = 7
        newGameButton.layer.masksToBounds = true
        newGameButton.addTarget(self, action: #selector(newGame), for: UIControl.Event.touchUpInside)
        self.view.addSubview(newGameButton)
    }
    
    func createCardsInterface(){
        let outsideMargin = 20.0
        let insideMargin = 10.0
        let screenSize = UIScreen.main.bounds.size
        let buttonWidth = (screenSize.width - outsideMargin*2 - insideMargin*Double(lie-1))/Double(lie)
        
        let upMargin = 90.0
        let downMargin = 35.0
        let buttonHeight = (screenSize.height - upMargin - downMargin - insideMargin*Double(hang-1))/Double(hang)

        for index in 0 ..< hang*lie {
            let buttonX = outsideMargin + (Double)(index % lie)*(buttonWidth + insideMargin)
            let buttonY = upMargin + Double(index / lie)*(buttonHeight + insideMargin)
            let button = UIButton.init(frame: CGRect.init(x: buttonX, y: buttonY, width: buttonWidth, height: buttonHeight))
            button.setTitle("👻", for: UIControl.State.normal)
            button.backgroundColor = UIColor.black
            button.titleLabel?.font = UIFont.systemFont(ofSize: 50)
            button.layer.cornerRadius = 7
            button.layer.masksToBounds = true
            button.tag = index
            button.addTarget(self, action: #selector(filpCard), for: UIControl.Event.touchUpInside)
            
            self.view.addSubview(button)
        }
    }
    
    @objc func filpCard(_ sender:UIButton){
        if(matchingArray.count>=2){
            return
        }
        //点击计数增加
        count += 1
        print("count : \(count)")
        //取得点击的按钮tag
        let tag = sender.tag
        //将按钮设置为不可点击状态,添加入匹配数组
        sender.isEnabled = false
        matchingArray.append(sender)
        let buttonString = mysteriousArray[tag] as String
        sender.setTitle(buttonString, for: UIControl.State.normal)
        sender.backgroundColor = UIColor.orange
        
        
        //判断匹配数是2,则匹配这对按钮是否是一致
        if(matchingArray.count == 2){
            //留点时间翻转卡片,延迟一秒执行逻辑判断
            self.perform(#selector(matching), with: nil, afterDelay: 0.5)
        }
    }
    
    @objc func matching(){
        let firstButton:UIButton = matchingArray[0] as UIButton
        let secButton:UIButton = matchingArray[1] as UIButton
//        print("first : \(firstButton.titleLabel?.text)")
//        print("sec : \(secButton.titleLabel?.text)")
        if (firstButton.titleLabel?.text == secButton.titleLabel?.text){
            //一致则从父控件中移除该两个按钮,记录分数,清空数组
            firstButton.removeFromSuperview()
            secButton.removeFromSuperview()
            matchingArray.removeAll()
            matchingCount += 2
        }else{
            //不一致则反转按钮从数组中移除该两个控件,
            turnOffTheCard(firstButton)
            turnOffTheCard(secButton)
            matchingArray.removeAll()
        }
    }
    
    func turnOffTheCard(_ button:UIButton){
        button.setTitle("👻", for: UIControl.State.normal)
        button.backgroundColor = UIColor.black
        button.isEnabled = true
    }
    
    @objc func newGame(){
        newGameHandle(withHang: 5, lie: 4)
    }
    
    func newGameHandle(withHang hang:Int,lie:Int){
        self.hang = hang
        self.lie = lie
        count = 0
        matchingCount = 0
        initMysteriousArray()
        createCardsInterface()
    }

运行结果:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/993577.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存