纯代码猜数字游戏 app 7.20

纯代码猜数字游戏 app 7.20

import UIKit

class ViewController: UIViewController {
    var label:UILabel!
    var  a:UITextField!
    var number = arc4random_uniform(100)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        label = UILabel()
        label.frame = CGRect(x: 80, y: 2, width: 200, height: 200)
        label.text = "猜猜猜"
        label.textColor = UIColor.blueColor()
        label.backgroundColor = UIColor.clearColor()
        label.font = UIFont.systemFontOfSize(50)
        self.view.addSubview(label)
        
        
        let label0 = UILabel()
        label0.frame = CGRect(x: 100, y:150, width: 30, height: 30)
        label0.text = "0"
        label0.textColor = UIColor.redColor()
        label0.font = UIFont.systemFontOfSize(25)
        self.view.addSubview(label0)
        
        let labelD = UILabel()
        labelD.frame = CGRect(x: 140, y: 150, width: 30, height: 30)
        labelD.text = "到"
        labelD.textColor = UIColor.blackColor()
        labelD.font = UIFont.systemFontOfSize(25)
        self.view.addSubview(labelD)
        
        let label100 = UILabel()
        label100.frame = CGRect(x: 180, y: 150, width: 50, height: 30)
        label100.text = "100"
        label100.textColor = UIColor.redColor()
        label100.font = UIFont.systemFontOfSize(25)
        self.view.addSubview(label100)
        
        
        
        a = UITextField()
        a.frame = CGRect(x: 90, y: 240, width: 150, height: 40)
        a.placeholder = "请输入文字"
        a.borderStyle = .RoundedRect
        a.keyboardType = .NumberPad
        self.view.addSubview(a)
        
        
        var btn = UIButton(type: .System)
        let image = UIImage(named:"ok")               //"ok"为图片名字
        btn.frame = CGRect(x: 140, y: 290, width: 35, height: 35)
        btn.setBackgroundImage(image, forState: .Normal)
                btn.tag = 122
        btn.addTarget(self, action: #selector(zz), forControlEvents: .TouchUpInside)

        self.view.addSubview(btn)
         
                
        }
    func zz() {
        
        let result  = UInt32(a.text!)
        if result  < number{
            label.text = "太小了"
        }
        else if result  > number {
            label.text = "太大了"
        }
        else {
            label.text = "答对了"
         
            number = arc4random_uniform(100)
        }
        
    }
    
}

你可能感兴趣的:(纯代码猜数字游戏 app 7.20)