// // advancedMode.swift // juego // // Created by Mobile Roshka on 3/4/20. // Copyright © 2020 Mobile Roshka. All rights reserved. // import UIKit class AdvancedModeViewController: 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 points = 0 var initGame = false var initTimer = false var timer = Timer() var timeRest = 0 var countGames = 0 let screenSize :CGRect = UIScreen.main.bounds let screenWidth = UIScreen.main.bounds.width let screenHeight = UIScreen.main.bounds.height var x = 0 var y = 0 var buttonWidth = 86 var buttonHeight = 86 //el puntaje actual del jugador @IBOutlet weak var pointsLblAM: UILabel! //el tiempo restante @IBOutlet weak var timeRestAMLbl: UILabel! //texto y propiedades del boton jugar (se opaca cuando inicia el juego) @IBOutlet weak var playGameAMOutlet: UIButton! @IBOutlet weak var viewOutlet: UIView! @IBAction func instructions(_ sender: Any) { showAlertIntructions(message: "Presiona el boton azul la mayor cantidad de veces posibles antes de que se acabe el tiempo ") } @IBAction func bestPlayerBtn(_ sender: Any) { showAlertBestPoint(message: "Mejor puntaje: \nObtenido por") } @IBOutlet weak var touchBtnOutlet: UIButton! //eventos relacionados al inicio del juego @IBAction func playGameAM(_ sender: Any) { /*Inicia el juego cuendo se presiona el boton, */ points = 0 initGame = true initTimer = true timeRest = 30 timeRestAMLbl.text = "\(timeRest)" playGameAMOutlet.alpha = 0.5 if initTimer{ points = 0 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 timeRestAMLbl.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) \nHas logrado \(points) puntos") playGameAMOutlet.alpha = 1 pointsLblAM.text = "0" resetButton() } } //contador de puntos segun cantidad de clicks @IBAction func touchPlayBtnAM(_ sender: Any) { if buttonHeight < 30 { timer.invalidate() showAlertGameOver(message: "Intento \(countGames) \nHas ganado en \(30 - timeRest) segundos") playGameAMOutlet.alpha = 1 resetButton() } if initGame{ x = Int.random(in: 53..<(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)) points += 1 pointsLblAM.text = "\(points)" } } //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 resetButton(){ touchBtnOutlet.center.x = screenWidth/2 touchBtnOutlet.center.y = screenHeight/2 buttonWidth = 86 buttonHeight = 86 touchBtnOutlet.frame.size = CGSize(width: CGFloat(buttonWidth), height: CGFloat(buttonHeight)) } override func viewDidLoad() { super.viewDidLoad() resetButton() } }