ExpertModeViewController.swift 5.02 KB
Newer Older
Mobile Roshka committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
//
//  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()
    }

}