Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class ViewController: UIViewController {
- @IBOutlet weak var okButton: UIButton!
- @IBOutlet weak var bombButton: UIButton!
- @IBOutlet weak var pointsLabel: UILabel!
- @IBOutlet weak var elapsedTimeLabel: UILabel!
- @IBOutlet weak var addButton: UIButton!
- @IBOutlet weak var bombLabel: UILabel!
- @IBOutlet weak var startButton: UIButton!
- var gameButtons = [UIButton]()
- var gamePoints = 0
- let stopwatch = Stopwatch()
- var timer: Timer?
- var newOKImageViews: [String] = ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8"]
- var newAddImageViews: [String] = ["m1", "m2", "m3"]
- var newBombImageViews: [String] = ["jim"]
- var okImageViews = ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8"]
- var addImageViews = ["m1", "m2", "m3"]
- var bombImageViews = ["jim"]
- enum GameState {
- case gameOver
- case playing
- }
- var state = GameState.gameOver
- override func viewDidLoad() {
- super.viewDidLoad()
- pointsLabel.isHidden = true
- gameButtons = [okButton, bombButton, addButton]
- refreshGameState()
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- func startNewGame(){
- startButton.isHidden = true
- gamePoints = 0
- updatePointsLabel(gamePoints)
- pointsLabel.textColor = .black
- pointsLabel.isHidden = false
- oneGameRound()
- }
- func oneGameRound(){
- updatePointsLabel(gamePoints)
- displayRandomButton()
- timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) {
- _ in if self.state == GameState.playing {
- if self.okButton == self.okButton {
- self.oneGameRound()
- } else {
- self.gameOver()
- }
- }
- }
- }
- @IBAction func startButtonPressed(_ sender: Any) {
- state = GameState.playing
- startNewGame()
- stopwatch.start()
- Timer.scheduledTimer(timeInterval: 0.1, target: self,
- selector: #selector(ViewController.updateElapsedTimeLabel(_:)), userInfo: nil, repeats: true)
- }
- @IBAction func okButtonPressed(_ sender: Any) {
- gamePoints = gamePoints + 1
- updatePointsLabel(gamePoints)
- okButton.isHidden = true
- timer?.invalidate()
- oneGameRound()
- }
- @IBAction func addButton(_ sender: Any) {
- gamePoints = gamePoints + 3
- updatePointsLabel(gamePoints)
- addButton.isHidden = true
- timer?.invalidate()
- oneGameRound()
- }
- @IBAction func bombButtonPressed(_ sender: Any) {
- gamePoints = 0
- gameOver()
- timer?.invalidate()
- startButton.isHidden = false
- okButton.isEnabled = false
- addButton.isEnabled = false
- bombButton.isEnabled = false
- }
- @objc func updateElapsedTimeLabel(_ timer: Timer) {
- if stopwatch.isRunning {
- elapsedTimeLabel.text = stopwatch.elapsedTimeAsString
- } else {
- timer.invalidate()
- }
- }
- func displayRandomButton(){
- addButton.isEnabled = true
- okButton.isEnabled = true
- bombButton.isEnabled = true
- bombImageViews = newBombImageViews.shuffled()
- newOKImageViews = okImageViews.shuffled()
- addImageViews = newAddImageViews.shuffled()
- for myButton in gameButtons{
- myButton.isHidden = true
- }
- okButton.center = CGPoint(x: randomXCoordinate(), y: randomYCoordinate())
- okButton.isHidden = false
- bombButton.center = CGPoint(x: randomXCoordinate(), y: randomYCoordinate())
- bombButton.isHidden = false
- addButton.center = CGPoint(x: randomXCoordinate(), y: randomYCoordinate())
- addButton.isHidden = false
- addButton.setBackgroundImage(UIImage(named: newAddImageViews[0]), for: UIControl.State.normal)
- okButton.setBackgroundImage(UIImage(named: newOKImageViews[0]), for: UIControl.State.normal)
- bombButton.setBackgroundImage(UIImage(named: newBombImageViews[0]), for: UIControl.State.normal)
- }
- func gameOver() {
- state = GameState.gameOver
- pointsLabel.textColor = .black
- gamePoints = 0
- refreshGameState()
- stopwatch.stop()
- }
- func refreshGameState() {
- startButton.isHidden = false
- for myButton in gameButtons {
- myButton.isHidden = false
- }
- pointsLabel.alpha = 0.4
- state = GameState.gameOver
- stopwatch.start()
- }
- func randomCGFloat(_ min: CGFloat, _ max: CGFloat) -> CGFloat {
- return CGFloat.random(in: min..<max)
- }
- func randomXCoordinate() -> CGFloat {
- let left = view.safeAreaInsets.left + okButton.bounds.width
- let right = view.bounds.width - view.safeAreaInsets.right; okButton.bounds.width
- return randomCGFloat(left, right)
- }
- func randomYCoordinate() -> CGFloat {
- let top = view.safeAreaInsets.top + okButton.bounds.height
- let bottom = view.bounds.height - view.safeAreaInsets.bottom - okButton.bounds.height
- return randomCGFloat(top, bottom)
- }
- func updatePointsLabel(_ newValue: Int) {
- pointsLabel.text = "\(newValue)"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement