AdvancedModeViewController.swift 4.98 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 145 146 147 148 149 150 151 152 153
//
//  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()
    }

}