// // expertMode.swift // juego // // Created by Mobile Roshka on 3/4/20. // Copyright © 2020 Mobile Roshka. All rights reserved. // import UIKit class ExpertModeViewController: UIViewController { /*Se inicializan los valores a ser utilizados, puntaje, si el juego esta en curso, si el tiempo esta en curso, temporizador, contador de intentos y posicion del boton */ var touchs = 0 var initGame = false var initTimer = false var timer = Timer() var timeRest = 0 var countGames = 0 /*se obtiene el largo y ancho de pantalla*/ let screenSize :CGRect = UIScreen.main.bounds let screenWidth = UIScreen.main.bounds.width let screenHeight = UIScreen.main.bounds.height /*se inicializa las variables de posicion para el boton*/ var x = 0 var y = 0 /*Largo y ancho del boton*/ var buttonWidth = 86 var buttonHeight = 86 //toques restantes para ganar @IBOutlet weak var touchsLblEM: UILabel! //el tiempo restante @IBOutlet weak var timeRestEMLbl: UILabel! //texto y propiedades del boton jugar (se opaca cuando inicia el juego) @IBOutlet weak var playGameEMOutlet: UIButton! @IBAction func instructionsEM(_ sender: Any) { showAlertIntructions(message: "Reduce el tamaño del boton al mínimo posible antes de que se acabe el tiempo ") } @IBAction func bestPlayerBtnEM(_ sender: Any) { showAlertBestPoint(message: "Mejor puntaje: \nObtenido por") } @IBOutlet weak var touchBtnOutlet: UIButton! //eventos relacionados al inicio del juego @IBAction func playGameEM(_ sender: Any) { /*Inicia el juego cuendo se presiona el boton, */ initGame = true initTimer = true timeRest = 30 timeRestEMLbl.text = "\(timeRest)" playGameEMOutlet.alpha = 0.5 if initTimer{ touchs = 10 timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timeRestMethod), userInfo :nil, repeats: true) } } //Método para ir decrementando el tiempo y eventos correspondientes @objc func timeRestMethod() { timeRest -= 1 timeRestEMLbl.text = "\(timeRest)" //condicion de parada del temporizador y enventos al terminar el juego if timeRest == 0 { timer.invalidate() initGame = false countGames += 1 showAlertGameOver(message: "Intento \(countGames) \nPerdiste \n te faltaron \(touchs) toques") resetGame() } } //eventos relacionados a cuando se presiona el botón @IBAction func touchPlayBtnAM(_ sender: Any) { if touchs == 1 { timer.invalidate() showAlertGameOver(message: "Intento \(countGames) \nHas ganado en \(30 - timeRest) segundos") playGameEMOutlet.alpha = 1 resetGame() } if initGame{ x = Int.random(in: 103..<(Int(screenWidth)-53) ) y = Int.random(in: 100..<(Int(screenHeight)/2)) touchBtnOutlet.center.x = CGFloat(x) touchBtnOutlet.center.y = CGFloat(y) buttonWidth -= 6 buttonHeight -= 6 touchBtnOutlet.frame.size = CGSize(width: CGFloat(buttonWidth), height: CGFloat(buttonHeight)) touchs -= 1 touchsLblEM.text = "\(touchs)" } } //alerta cuando termina el juego, aparece el numero actual de intentos y el puntaje obtenido private func showAlertGameOver(message: String){ let alertGameOver = UIAlertController(title: "Juego Terminado", message:message, preferredStyle: .alert) alertGameOver.addAction(UIAlertAction(title: "Aceptar", style: .default, handler: nil)) present(alertGameOver, animated:true, completion:nil) } private func showAlertIntructions(message: String){ let alert = UIAlertController(title: "Instrucciones", message:message, preferredStyle: .alert) alert.addAction(UIAlertAction(title: "Aceptar", style: .default, handler: nil)) present(alert, animated:true, completion:nil) } private func showAlertBestPoint(message: String){ let alert = UIAlertController(title: "Mejor Puntaje", message:message, preferredStyle: .alert) alert.addAction(UIAlertAction(title: "Aceptar", style: .default, handler: nil)) present(alert, animated:true, completion:nil) } private func resetGame(){ touchBtnOutlet.center.x = screenWidth/2 touchBtnOutlet.center.y = screenHeight/2 touchs = 0 buttonWidth = 86 buttonHeight = 86 touchBtnOutlet.frame.size = CGSize(width: CGFloat(buttonWidth), height: CGFloat(buttonHeight)) playGameEMOutlet.alpha = 1 touchsLblEM.text = "0" } override func viewDidLoad() { super.viewDidLoad() resetGame() } }