Import uikit import avfoundation
Download 104.32 Kb.
|
import UIKit
import UIKit import AVFoundation import GoogleMobileAds import Lottie class ViewController: BaseViewController { var array = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"," "] var step = 0 { didSet { text.text = "Steps: " + String(step) } } let customView1 = UIView() let text = UILabel() let ressBtn = UIButton() let undoBut = UIButton() var arr:[Tags] = [] var startTime = Date().timeIntervalSince1970 private var rewardedAd: GADRewardedAd? let bannerView = GADBannerView(adSize: GADAdSizeLargeBanner) override func viewDidLoad() { super.viewDidLoad() self.navigationItem.title = "15! Puzzle" ressButton() undoButton() view.backgroundColor = colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) _ = customView1.frame.width _ = (UIScreen.main.bounds.height) addTestViews() view.addSubview(text) step = 0 text.textColor = colorLiteral(red: 0.4123191237, green: 0.368850708, blue: 0.9057943225, alpha: 1) text.font = UIFont(name: "AvenirNextCondensed-Heavy", size: (UIScreen.main.bounds.width)/12) text.frame = CGRect(x: 0, y: ((UIScreen.main.bounds.width)*15/100), width: UIScreen.main.bounds.width, height: ((UIScreen.main.bounds.width)*30/100)) text.textAlignment = .center show() addBannerViewToView(bannerView) } func addBannerViewToView(_ bannerView: GADBannerView) { bannerView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(bannerView) bannerView.adUnitID = GoogleIdKeys.bannerSetagiBottom bannerView.rootViewController = self bannerView.snp.makeConstraints { make in make.left.right.equalToSuperview() make.bottom.equalTo(view.safeAreaLayoutGuide) } bannerView.load(GADRequest()) } func show() { let request = GADRequest() GADRewardedAd.load(withAdUnitID:GoogleIdKeys.bannerGamesVideo, request: request, completionHandler: { [weak self] ad, error in if let error = error { print("Failed to load rewarded ad with error: \(error.localizedDescription)") return } guard let strongSelf = self else { return } strongSelf.rewardedAd = ad strongSelf.rewardedAd?.present(fromRootViewController: strongSelf) { if let reward = ad?.adReward{ print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)") } } print("Rewarded ad loaded.") } ) } func addTestViews() { customView1.backgroundColor = colorLiteral(red: 0.01360324398, green: 0.7805321813, blue: 0.7441406846, alpha: 1) customView1.layer.cornerRadius = 15 customView1.frame = CGRect(x: ((UIScreen.main.bounds.width)*2/100), y: ((UIScreen.main.bounds.height)*5/100), width: ((UIScreen.main.bounds.width)*96/100), height:((UIScreen.main.bounds.width)*96/100)) view.addSubview(customView1) customView1.center = view.center array.shuffle() addButton() } func addButton(){ var x = Int(customView1.frame.width)*2/100 var y = Int(customView1.frame.width)*2/100 var count = 0 for _ in 0...3 { for _ in 0...3 { let button = UIButton() button.backgroundColor = .darkGray button.frame = CGRect (x: x, y: y, width: Int((customView1.frame.width)*24/100), height: Int((customView1.frame.width)*24/100)) customView1.addSubview(button) button.layer.cornerRadius = 15 button.tag = count + 1 button.titleLabel?.font = UIFont(name: "AvenirNextCondensed-Heavy", size: (button.frame.width)/2) button.setTitle(array[count], for: .normal) if array[count] == " "{ button.backgroundColor = .clear } button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) x += Int(button.frame.width)+1 count += 1 } y += x*24/100+3 x = Int(customView1.frame.width)*2/100 } } func ressButton(){ ressBtn.setBackgroundImage(UIImage(named: "restart"), for: .normal) ressBtn.frame = CGRect(x: ((UIScreen.main.bounds.width)/2 - (UIScreen.main.bounds.width)*45/100), y: ((UIScreen.main.bounds.height)*15/100), width: ((UIScreen.main.bounds.width)/8), height: ((UIScreen.main.bounds.width)/8)) view.addSubview(ressBtn) ressBtn.addTarget(self, action: #selector(ressTapped), for: .touchUpInside) } func undoButton(){ undoBut.setBackgroundImage(UIImage(named: "undo"), for: .normal) undoBut.tintColor = colorLiteral(red: 0.4084324241, green: 0.3648548126, blue: 0.9018095136, alpha: 1) undoBut.frame = CGRect(x: ((UIScreen.main.bounds.width)/2 + ((UIScreen.main.bounds.width)*33/100)), y: ((UIScreen.main.bounds.height)*15/100), width: ((UIScreen.main.bounds.width)/8), height: ((UIScreen.main.bounds.width)/8)) view.addSubview(undoBut) undoBut.addTarget(self, action: #selector(undoTapped), for: .touchUpInside) undoBut.isEnabled = false } @objc func undoTapped(sender: UIButton){ if arr.count != 0{ let tag = arr[arr.count - 1] if let btn = customView1.viewWithTag(tag.btnOneTag ?? 0) as? UIButton, let btn2 = customView1.viewWithTag(tag.btnTwoTag ?? 0) as? UIButton{ swapView(btn: btn, sender: btn2, isUndo: true) arr.removeLast() } show() } if arr.count == 0 { self.undoBut.isEnabled = false } } @objc func ressTapped(){ for item in customView1.subviews { item.removeFromSuperview() } step = 0 arr = [] undoBut.isEnabled = false array.shuffle() addButton() AudioServicesPlaySystemSound (1305) show() } @objc func buttonTapped(sender:UIButton){ if sender.currentTitle != " "{ if let btn = customView1.viewWithTag(sender.tag - 1) as? UIButton, btn.currentTitle == " "{ swapView(btn: btn, sender: sender, isUndo: false) } else if let btn = customView1.viewWithTag(sender.tag - 4) as? UIButton, btn.currentTitle == " "{ swapView(btn: btn, sender: sender, isUndo: false) } else if let btn = customView1.viewWithTag(sender.tag + 1) as? UIButton, btn.currentTitle == " "{ swapView(btn: btn, sender: sender, isUndo: false) } else if let btn = customView1.viewWithTag(sender.tag + 4) as? UIButton, btn.currentTitle == " "{ swapView(btn: btn, sender: sender, isUndo: false) } } } private func startLottie(){ let jsonName = "tabrik4" let animation = Animation.named(jsonName) let animationView = AnimationView(animation: animation) animationView.frame = UIScreen.main.bounds view.addSubview(animationView) animationView.loopMode = .repeat(1) animationView.play { complete in if complete{ animationView.removeFromSuperview() self.ressTapped() } } } func swapView(btn:UIButton, sender:UIButton, isUndo:Bool){ if !isUndo { arr.append(Tags(btnOneTag: btn.tag, btnTwoTag: sender.tag)) self.undoBut.isEnabled = true } UIView.animate(withDuration: 0.2){ (sender.tag, btn.tag) = (btn.tag, sender.tag) (btn.center, sender.center) = (sender.center, btn.center) } step = isUndo ? step - 1 : step + 1 AudioServicesPlaySystemSound (1306) if check(){ startLottie() } } func check()->Bool{ let q = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"," "] var counter = 1 for i in 1...4{ for j in 1...4{ let button = customView1.viewWithTag(counter) as? UIButton if button?.titleLabel?.text != q[counter - 1]{ return false } counter += 1 } } return true } } struct Tags { var btnOneTag:Int? var btnTwoTag:Int? } import UIKit import SnapKit import GoogleMobileAds class MathTaskOne{ var task:String? var anwswers = [String]() var answer:String? } class BtnContainer : BaseView{ private let label = UILabel() private let correct = UILabel() private let btn1 = UIButton() private let btn2 = UIButton() private let btn3 = UIButton() private let btn4 = UIButton() private var answer = "" var score = 0{ didSet{ correct.text = "\(score)" } } var onAnswer: ((Bool) -> Void) = {_ in} private func btnSettings(btn:UIButton){ btn.layer.cornerRadius = 10 btn.setTitleColor(UIColor.white, for: .normal) btn.titleLabel?.font = AppFont.extraBold(size: 36) btn.backgroundColor = UIColor(patternImage: UIImage(named: "bg") ?? UIImage()) btn.addTarget(self, action: #selector(btnClicked(sender:)), for:.touchUpInside) } func setupTask(task:MathTaskOne){ let array = task.anwswers label.text = task.task answer = task.answer ?? "" correct.text = " \(score) " btn1.setTitle(array[0], for: .normal) btn2.setTitle(array[1], for: .normal) btn3.setTitle(array[2], for: .normal) btn4.setTitle(array[3], for: .normal) } override func initSubviews() { btn1.tag = 1 btn2.tag = 2 btn3.tag = 3 btn4.tag = 4 for a in [btn1, btn2, btn3, btn4]{ btnSettings(btn: a) } label.font = UIFont.boldSystemFont(ofSize: 48) label.textColor = .black label.textAlignment = .center correct.font = UIFont.boldSystemFont(ofSize: 24) correct.textColor = UIColor(patternImage: UIImage(named: "bg") ?? UIImage()) correct.textAlignment = .center } override func embedSubviews() { addSubview(correct) addSubview(label) for a in [btn1, btn2, btn3, btn4]{ addSubview(a) } } override func addSubviewsConstraints() { correct.snp.makeConstraints { make in make.top.equalToSuperview() make.left.equalTo(5) } label.snp.makeConstraints { make in make.centerX.equalToSuperview() } btn1.snp.makeConstraints { make in make.top.equalTo(label.snp.bottom).offset(10) make.left.equalToSuperview() make.height.equalTo(80) make.width.equalToSuperview().multipliedBy(0.5).offset(-10) } btn2.snp.makeConstraints { make in make.left.bottom.equalToSuperview() make.top.equalTo(btn1.snp.bottom).offset(10) make.height.equalTo(btn1) make.width.equalTo(btn1) } btn3.snp.makeConstraints { make in make.top.equalTo(btn1) make.right.equalToSuperview() make.height.equalTo(btn1) make.width.equalTo(btn1) } btn4.snp.makeConstraints { make in make.right.bottom.equalToSuperview() make.height.equalTo(btn1) make.width.equalTo(btn1) } } @objc private func btnClicked(sender:UIButton){ let title = sender.titleLabel?.text ?? "" if answer == title{ score = score + 1 } else{ score = score - 1 } correct.text = " \(score) " onAnswer(answer == title ) } } class PlusMinusViewController: UIViewController { let timeLbl = UILabel() let timeView = UIView() var shapeLayer: CAShapeLayer? var timer = Timer() var shapeslayer = CAShapeLayer() var durattiontimer = 5 let btnContainerView = BtnContainer() private var rewardedAd: GADRewardedAd? let bannerView = GADBannerView(adSize: GADAdSizeLargeBanner) override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() self.animationCircular() } override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white self.navigationItem.title = "Arithmetic" view.addSubview(timeView) timeView.addSubview(timeLbl) timeLbl.textAlignment = .center timeLbl.font = .systemFont(ofSize: 40 , weight: .semibold) timeLbl.textColor = .black timeView.snp.makeConstraints { make in make.top.equalTo(view.safeAreaLayoutGuide).offset(10) make.centerX.equalToSuperview() make.height.width.equalTo(100) } timeLbl.snp.makeConstraints { make in make.centerX.centerY.equalToSuperview() } view.addSubview(btnContainerView) addBannerViewToView(bannerView) btnContainerView.backgroundColor = UIColor.clear btnContainerView.snp.makeConstraints { make in make.top.equalTo(view.safeAreaLayoutGuide).offset(10) make.bottom.equalTo(bannerView.snp.top) make.left.equalTo(10) make.right.equalTo(-10) } btnContainerView.onAnswer = { [weak self] a in if a { } else{ } self?.perform(#selector(self?.nextTask), with: nil, afterDelay: 0.8) } addBannerViewToView(bannerView) durattiontimer = 5 timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timeAction), userInfo: nil, repeats: true) nextTask() show() } @objc func timeAction(){ durattiontimer -= 1 timeLbl.text = "\(durattiontimer)" if durattiontimer == 0{ gameOver() } } func animationCircular(){ let center = CGPoint(x: timeView.frame.width / 2 , y: timeView.frame.height/2 ) let endAngle = (-CGFloat.pi / 2) let startAngle = 2*CGFloat.pi + endAngle let circularPath = UIBezierPath(arcCenter: center, radius: 50, startAngle: startAngle, endAngle: endAngle, clockwise: false) shapeslayer.path = circularPath.cgPath shapeslayer.lineWidth = 12 shapeslayer.fillColor = UIColor.clear.cgColor shapeslayer.strokeEnd = 1 shapeslayer.lineCap = CAShapeLayerLineCap.round shapeslayer.strokeColor = UIColor(patternImage: UIImage(named: "bg") ?? UIImage()).cgColor timeView.layer.addSublayer(shapeslayer) } func basicAnimation() { let basicAnimation = CABasicAnimation(keyPath: "strokeEnd") basicAnimation.toValue = 0 basicAnimation.duration = CFTimeInterval(durattiontimer) basicAnimation.fillMode = CAMediaTimingFillMode.forwards basicAnimation.isRemovedOnCompletion = true shapeslayer.add(basicAnimation, forKey: "basicAnimation") } func gameOver() { if timer.isValid{ timer.invalidate() } btnContainerView.score = 0 let alertVC = UIAlertController(title: "GAME OVER", message: "", preferredStyle: .alert) alertVC.addAction(UIAlertAction(title: "Restart", style: .default, handler: { _ in self.nextTask() })) present(alertVC, animated: true) } func addBannerViewToView(_ bannerView: GADBannerView) { bannerView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(bannerView) bannerView.adUnitID = GoogleIdKeys.bannerSetagiBottom bannerView.rootViewController = self bannerView.snp.makeConstraints { make in make.left.right.equalToSuperview() make.bottom.equalTo(view.safeAreaLayoutGuide) } bannerView.load(GADRequest()) } func show() { let request = GADRequest() GADRewardedAd.load(withAdUnitID:GoogleIdKeys.bannerGamesVideo, request: request, completionHandler: { [weak self] ad, error in if let error = error { print("Failed to load rewarded ad with error: \(error.localizedDescription)") return } guard let strongSelf = self else { return } strongSelf.rewardedAd = ad strongSelf.rewardedAd?.present(fromRootViewController: strongSelf) { if let reward = ad?.adReward{ print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)") } } print("Rewarded ad loaded.") } ) } @objc private func nextTask(){ if timer.isValid{ timer.invalidate() } durattiontimer = 5 timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timeAction), userInfo: nil, repeats: true) let task = generateMathTask() btnContainerView.setupTask(task: task) basicAnimation() animationCircular() } func generateMathTask()->MathTaskOne{ let number1 = Int.random(in: 0..<101) let number2 = Int.random(in: 0..<101) let maxNumber = max(number1, number2) let minNumber = min(number2, number1) let operationNumber = Int.random(in: 0..<5)%4 let result = MathTaskOne() if operationNumber == 0{ // + result.task = "\(maxNumber) + \(minNumber) = ?" result.answer = "\(minNumber + maxNumber)" let a = "\(minNumber + maxNumber + 1)" let b = "\(minNumber + maxNumber + 2)" let c = "\(minNumber + maxNumber - 1)" let d = "\(minNumber + maxNumber)" result.anwswers = [a, b, c, d].shuffled() } if operationNumber == 1{ // - result.task = "\(maxNumber) - \(minNumber) = ?" result.answer = "\(maxNumber - minNumber)" let a = "\(maxNumber - minNumber + 1)" let b = "\(maxNumber - minNumber - 1)" let c = "\(maxNumber - minNumber + 2)" let d = "\(maxNumber - minNumber)" result.anwswers = [a, b, c, d].shuffled() } if operationNumber == 2{ // / let number1 = Int.random(in: 4..<15) let number2 = Int.random(in: 4..<15) let maxNumber = max(number1, number2) let minNumber = min(number2, number1) let k = maxNumber * minNumber; let bolinuvchi = Int.random(in: 0..<5) % 2 == 0 ? minNumber : maxNumber let answer = k / bolinuvchi result.task = "\(k) : \(bolinuvchi) = ?" result.answer = "\(answer)" let a = "\(answer + 1)" let b = "\(answer - 1)" let c = "\(answer - 2)" let d = "\(answer)" result.anwswers = [a, b, c, d].shuffled() } if operationNumber == 3{ // * let number1 = Int.random(in: 4..<15) let number2 = Int.random(in: 4..<15) let maxNumber = max(number1, number2) let minNumber = min(number2, number1) result.task = "\(maxNumber) x \(minNumber) = ?" result.answer = "\(maxNumber * minNumber)" let a = "\(maxNumber * (minNumber + 1))" let b = "\(maxNumber * (minNumber - 1))" let c = "\(maxNumber * minNumber - 2)" let d = "\(maxNumber * minNumber)" result.anwswers = [a, b, c, d].shuffled() } return result } } Download 104.32 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling