Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. import UIKit
  2.  
  3.  
  4.  
  5. class ViewController: UIViewController {
  6.  
  7. @IBOutlet weak var okButton: UIButton!
  8. @IBOutlet weak var bombButton: UIButton!
  9. @IBOutlet weak var pointsLabel: UILabel!
  10.  
  11. @IBOutlet weak var elapsedTimeLabel: UILabel!
  12. @IBOutlet weak var addButton: UIButton!
  13. @IBOutlet weak var bombLabel: UILabel!
  14. @IBOutlet weak var startButton: UIButton!
  15.  
  16. var gameButtons = [UIButton]()
  17. var gamePoints = 0
  18. let stopwatch = Stopwatch()
  19.  
  20. var timer: Timer?
  21. var newOKImageViews: [String] = ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8"]
  22. var newAddImageViews: [String] = ["m1", "m2", "m3"]
  23. var newBombImageViews: [String] = ["jim"]
  24.  
  25.  
  26. var okImageViews = ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8"]
  27. var addImageViews = ["m1", "m2", "m3"]
  28. var bombImageViews = ["jim"]
  29.  
  30. enum GameState {
  31. case gameOver
  32. case playing
  33. }
  34. var state = GameState.gameOver
  35.  
  36.  
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. pointsLabel.isHidden = true
  40. gameButtons = [okButton, bombButton, addButton]
  41. refreshGameState()
  42.  
  43. }
  44.  
  45. override func didReceiveMemoryWarning() {
  46. super.didReceiveMemoryWarning()
  47. // Dispose of any resources that can be recreated.
  48. }
  49. func startNewGame(){
  50. startButton.isHidden = true
  51. gamePoints = 0
  52. updatePointsLabel(gamePoints)
  53. pointsLabel.textColor = .black
  54. pointsLabel.isHidden = false
  55. oneGameRound()
  56.  
  57.  
  58.  
  59. }
  60.  
  61.  
  62. func oneGameRound(){
  63. updatePointsLabel(gamePoints)
  64. displayRandomButton()
  65.  
  66. timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) {
  67. _ in if self.state == GameState.playing {
  68. if self.okButton == self.okButton {
  69. self.oneGameRound()
  70. } else {
  71. self.gameOver()
  72.  
  73. }
  74. }
  75. }
  76. }
  77.  
  78.  
  79. @IBAction func startButtonPressed(_ sender: Any) {
  80. state = GameState.playing
  81. startNewGame()
  82. stopwatch.start()
  83. Timer.scheduledTimer(timeInterval: 0.1, target: self,
  84. selector: #selector(ViewController.updateElapsedTimeLabel(_:)), userInfo: nil, repeats: true)
  85.  
  86. }
  87.  
  88. @IBAction func okButtonPressed(_ sender: Any) {
  89. gamePoints = gamePoints + 1
  90. updatePointsLabel(gamePoints)
  91. okButton.isHidden = true
  92. timer?.invalidate()
  93. oneGameRound()
  94. }
  95.  
  96. @IBAction func addButton(_ sender: Any) {
  97. gamePoints = gamePoints + 3
  98. updatePointsLabel(gamePoints)
  99. addButton.isHidden = true
  100. timer?.invalidate()
  101. oneGameRound()
  102. }
  103.  
  104. @IBAction func bombButtonPressed(_ sender: Any) {
  105. gamePoints = 0
  106. gameOver()
  107. timer?.invalidate()
  108. startButton.isHidden = false
  109. okButton.isEnabled = false
  110. addButton.isEnabled = false
  111. bombButton.isEnabled = false
  112. }
  113.  
  114. @objc func updateElapsedTimeLabel(_ timer: Timer) {
  115. if stopwatch.isRunning {
  116. elapsedTimeLabel.text = stopwatch.elapsedTimeAsString
  117. } else {
  118. timer.invalidate()
  119.  
  120. }
  121. }
  122.  
  123.  
  124. func displayRandomButton(){
  125. addButton.isEnabled = true
  126. okButton.isEnabled = true
  127. bombButton.isEnabled = true
  128. bombImageViews = newBombImageViews.shuffled()
  129. newOKImageViews = okImageViews.shuffled()
  130. addImageViews = newAddImageViews.shuffled()
  131.  
  132. for myButton in gameButtons{
  133. myButton.isHidden = true
  134. }
  135. okButton.center = CGPoint(x: randomXCoordinate(), y: randomYCoordinate())
  136. okButton.isHidden = false
  137.  
  138. bombButton.center = CGPoint(x: randomXCoordinate(), y: randomYCoordinate())
  139. bombButton.isHidden = false
  140.  
  141. addButton.center = CGPoint(x: randomXCoordinate(), y: randomYCoordinate())
  142. addButton.isHidden = false
  143.  
  144. addButton.setBackgroundImage(UIImage(named: newAddImageViews[0]), for: UIControl.State.normal)
  145. okButton.setBackgroundImage(UIImage(named: newOKImageViews[0]), for: UIControl.State.normal)
  146. bombButton.setBackgroundImage(UIImage(named: newBombImageViews[0]), for: UIControl.State.normal)
  147.  
  148. }
  149. func gameOver() {
  150. state = GameState.gameOver
  151. pointsLabel.textColor = .black
  152. gamePoints = 0
  153. refreshGameState()
  154. stopwatch.stop()
  155. }
  156.  
  157. func refreshGameState() {
  158. startButton.isHidden = false
  159. for myButton in gameButtons {
  160. myButton.isHidden = false
  161. }
  162. pointsLabel.alpha = 0.4
  163. state = GameState.gameOver
  164. stopwatch.start()
  165. }
  166. func randomCGFloat(_ min: CGFloat, _ max: CGFloat) -> CGFloat {
  167. return CGFloat.random(in: min..<max)
  168.  
  169. }
  170. func randomXCoordinate() -> CGFloat {
  171. let left = view.safeAreaInsets.left + okButton.bounds.width
  172. let right = view.bounds.width - view.safeAreaInsets.right; okButton.bounds.width
  173. return randomCGFloat(left, right)
  174.  
  175. }
  176. func randomYCoordinate() -> CGFloat {
  177. let top = view.safeAreaInsets.top + okButton.bounds.height
  178. let bottom = view.bounds.height - view.safeAreaInsets.bottom - okButton.bounds.height
  179. return randomCGFloat(top, bottom)
  180. }
  181.  
  182. func updatePointsLabel(_ newValue: Int) {
  183. pointsLabel.text = "\(newValue)"
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement