// // ViewController.swift // Improve Your Typing import UIKit import AVFoundation class ViewController: UIViewController { // ********************************* // ********************************* // ********************************* // We will define our global variables // below (instance variables) // ********************************* // ********************************* // ********************************* let synth = AVSpeechSynthesizer() // for speach // FINISH ME // create a variable called timer var ???? = Timer() var timerInterval = 0.03 // defines a variable for the screen size let screenSize:CGRect = UIScreen.main.bounds // defines variables for the screen height and screen width // FINISH ME // create a variable caled screenHeight var ?????? = CGFloat(0) // FINISH ME // create a variable caled screenWidth var ?????? = CGFloat(0) // FINISH ME // create a variable called gameOver and assign // it a value of true var ???? = ???? // FINISH ME // create a variable called score and assign // it a value of 0 var ???? = ? let wordSpeed = CGFloat(3) // FINISH ME // make sure that you have an image called explosion.png // in your project // we load the image of an explosion to be used later let explosionImage = UIImage(named: "explosion.png"); // FINISH ME // create a variable called words // to refer to the list of all the words var ????? = [UILabel]() // FINISH ME // create a variable called listOfWords // to refer to the list of all the words to be used for the Labels var ?????? = [String]() // FINISH ME // create a variable called explosions // to refer to the list of all the explosions var ????????? = [UIImageView]() // FINISH ME // create a variable called explosionsTimes // to refer to the list of all the explosionsTimes // a list of all the explosions time left on screen var ???????? = [Double]() // ********************************* // ********************************* // ********************************* // We will connect our IBOutlet variables // below (instance variables, reference // variables) // ********************************* // ********************************* // ********************************* // FINISH ME // this defines the variable nightSky // connect it to your UIImage object @IBOutlet weak var nightSky: UIImageView! // FINISH ME // this defines the variable scoreLabel // connect it to your Label object // that is on the UIViewController which // says "Score: 0" on it. @IBOutlet weak var scoreLabel: UILabel! // FINISH ME // this defines the variable word1 // connect it to your UILabel object // that is on the UIViewController @IBOutlet weak var word1: UILabel! // FINISH ME // this defines the variable word2 // connect it to your UILabel object // that is on the UIViewController @IBOutlet weak var word2: UILabel! // FINISH ME // this defines the variable word3 // connect it to your UILabel object // that is on the UIViewController @IBOutlet weak var word3: UILabel! // FINISH ME // this defines the variable wordTextField // connect it to your UITextField object // that is on the UIViewController @IBOutlet weak var wordTextField: UITextField! // FINISH ME // this defines the variable startButton // connect it to your UIButton object // that is on the UIViewController @IBOutlet weak var startButton: UIButton! // FINISH ME // this defines the variable stopButton // connect it to your UIButton object // that is on the UIViewController @IBOutlet weak var stopButton: UIButton! // ********************************* // ********************************* // ********************************* // Here come the functions!!!!!!!!!! // ********************************* // ********************************* // ********************************* func speak(_ message:String) { let speech = AVSpeechUtterance(string: message) synth.speak(speech) } // FINISH ME // this defines the function moveSkyNightLeft func moveSkyNightLeft() { // FINISH ME // this gets the x and y value of the UIImageView var x = nightSky.frame.?????.? - 0.2 let y = nightSky.frame.?????.? // FINISH ME // check to see if x is less than -200 ?? ??????? { // FINISH ME // set x by 0 ???? } // FINISH ME // this gets the width and height value of the UIImageView let width = nightSky.frame.????.???? let height = nightSky.frame.?????.????? // this resets the frame of the UIImageView object nightSky.frame = CGRect(x: x, y: y, width: width, height: height) } // this defines the function resetSkyNight func resetSkyNight() { let x = CGFloat(0) let y = CGFloat(50) let width = screenWidth * 2 let height = screenHeight // this resets the frame of the UIImageView object nightSky.frame = CGRect(x: x, y: y, width: width, height: height) } // FINISH ME // this defines the function resetStartButton func resetStartButton() { // FINISH ME // this gets the x and y value of the Button let x = wordTextField.frame.?????.? let y = CGFloat(screenHeight) - stopButton.frame.size.height - 50 // FINISH ME // this gets the width and height value of the startButton let width = startButton.frame.?????.???? let height = startButton.frame.????.???? // this resets the frame of the UIImageView object startButton.frame = CGRect(x: x, y: y, width: width, height: height) } // this defines the function resetStopButton func resetStopButton() { // this gets the x and y value of the UIImageView let x = wordTextField.frame.origin.x + wordTextField.frame.size.width - stopButton.frame.size.width + 20 //(20 padding) let y = CGFloat(screenHeight) - stopButton.frame.size.height - 50 // FINISH ME // this gets the width and height value of the startButton let width = stopButton.??????.????.???? let height = stopButton.??????.????.???? // this resets the frame of the UIImageView object stopButton.frame = CGRect(x: x, y: y, width: width, height: height) } // this defines the function resetScoreLabelPosition func resetScoreLabelPosition() { // this gets the x and y value of the scoreLabel let x = screenWidth/2 - scoreLabel.frame.size.width/2 //(20 padding) let y = CGFloat(screenHeight) - stopButton.frame.size.height - 50 // FINISH ME // this gets the width and height value of the scoreLabel let width = scoreLabel.??????.????.???? let height = scoreLabel.??????.????.???? // this resets the frame of the scoreLabel object scoreLabel.frame = CGRect(x: x, y: y, width: width, height: height) } // this defines the function randomlySelectWord func randomlySelectWord() -> String { // gets a random x value from 0 to listOfWords.count-1 let x = Int(arc4random_uniform(UInt32(listOfWords.count))) if x >= 0 && x < listOfWords.count { return listOfWords[x] } else { return listOfWords[0] } } // this defines the function resetWordLocation() func resetWordTextFieldLocation() { let width = wordTextField.frame.size.width let height = wordTextField.frame.size.height // this gets the x and y value of the UITextField let x = screenWidth * 0.5 - width/2 let y = CGFloat(screenHeight) - wordTextField.frame.size.height - startButton.frame.size.height - 100 // this resets the frame of the UIImageView object wordTextField.frame = CGRect(x: x, y: y, width: width, height: height) } func moveWordDown(_ word:UILabel) { // FINISH ME // get the x and y values from the // word's frame let x = word.??????.????.? var y = word.??????.????.? let width = word.frame.size.width let height = word.frame.size.height // FINISH ME // increase y by wordSpeed y = ??????? word.frame = CGRect(x: x, y: y, width: width, height: height) } func moveWordOffTopOfScreen(_ word:UILabel) { // FINISH ME // get the x and y values from the // word's frame var x = word.??????.????.? var y = word.??????.????.? let width = word.frame.size.width let height = word.frame.size.height // gets a random x value from 0 to screenWidth x = CGFloat(arc4random_uniform(UInt32(screenWidth))) - width if x < 0 { x = 0 } // FINISH ME // set y to -350 y = ???? word.frame = CGRect(x: x, y: y, width: width, height: height) word.text = randomlySelectWord() } func checkWordForMatch(_ word:UILabel) -> Bool { let wordFromLabel = word.text! let wordFromTextField = wordTextField.text! if wordFromLabel == wordFromTextField { // they typed in the correct word // so create an explosion createAnExplosion(word) // FINISH ME // move the word off the top of the screen // call a function ????????????(word) return true } return false } // explosions and explosionsTimes func updateAndRemoveExplosionsIfTimeIsUp() { // FINISH ME // create a variable called i and set to 0 var ? = ? // FINISH ME // loop through each explosion // i must be less than explosions.count while i < ???????? { let explosion = explosions[i] explosionsTimes[i] = explosionsTimes[i] - timerInterval let explosionTime = explosionsTimes[i] // FINISH ME // see if the explosionTime is less than or equal to 0 ??????????? { explosion.removeFromSuperview() explosions.remove(at:i) explosionsTimes.remove(at:i) } else { // FINISH ME // increase i by adding 1 ??????? } } } // end of method removeAlienBulletsGoingOffTheScreen() func removeWordsGoingOffTheScreen() { // FINISH ME // loop through each word // and check for going off the screen for ???? in words { // see if the word goes off the screen if word.frame.origin.y > screenHeight { // FINISH ME // move this word off the top of the screen // hint: call a method and send it the word ???????????(word) // FINISH ME // subtract 5 from your score score = ??????? } } } // we will increase the size of the explosion func moveExplosion(_ explosion:UIImageView) { // get the object's coordinates and // adjust them let x = explosion.frame.origin.x - 0.1 let y = explosion.frame.origin.y - 0.1 let width = explosion.frame.size.width + 0.2 let height = explosion.frame.size.height + 0.2 // this resets the frame of the UIImageView object explosion.frame = CGRect(x: x, y: y, width: width, height: height) } func moveExplosions() { // FINISH ME // loop through all explosions for explosion in ??????? { // FINISH ME // Move the explosion by increasing the size // HINT: Call a method and // send to it the explosion ?????????(explosion) } } func moveWordsDown() { // FINISH ME // loop through all words for word in ??????? { // FINISH ME // word is referring to 1 word from the words list // each time through the loop // Move this word down // HINT: Call a method (function) // and send to it word ????????(word) } } // creates an explosion object // over the sent object and // adds it the explosions list func createAnExplosion(_ object:UILabel) { // get the object's coordinates let x = object.frame.origin.x let y = object.frame.origin.y let width = object.frame.size.width let height = object.frame.size.height let explosion = UIImageView(frame: CGRect(x:x, y:y, width:width, height:height)) explosion.image = explosionImage self.view.addSubview(explosion) // FINISH ME // append (add) this explosion to // explosions explosions.?????(???????) // FINISH ME // append (add) 1.0 to // explosionsTimes explosionsTimes.??????(???) } // ****************************** // ****************************** // ****************************** // main timer loop (event) // this gets called every 0.01 seconds // i.e it runs every 0.01 seconds // ****************************** // ****************************** // ****************************** @objc func timerAction() { // FINISH ME // here we check to see if the gameOver variable // is true if gameOver == ???? { return // exits (quits) the function } // ********************************* // ********************************* // ********************************* // We will move things below // remove stuff // and create stuff below // ********************************* // ********************************* // ********************************* moveSkyNightLeft() moveExplosions() updateAndRemoveExplosionsIfTimeIsUp() // FINISH ME // move the words down // HINT: Call a method (function) ?????? // FINISH ME // Remove words going off the screen // HINT: Call a method (function) ???????? // ********************************* // ********************************* // ********************************* // We will check for a win or a loss // and update the screen below // ********************************* // ********************************* // ********************************* for word in words { if checkWordForMatch(word) { // FINISH ME // add 10 to your score score = ??????? // FINISH ME // clear the wordTextField (set it to "") wordTextField.???? = "" } } // FINISH ME // update the scoreLabel // set scoreLabel.text to "Score:\(score)" scoreLabel.???? = ???????? // FINISH ME // check to see if the score is less than or // equal to -50 ???????? { // FINISH ME // set the variable gameOver to true gameOver = ???? // FINISH ME // set the scoreLabel.text to "You Lose" scoreLabel.???? = ?????? speak("Ouch! You Lose!") // we will stop the animation (timer) timer.invalidate() return } // FINISH ME // check to see if the score is greater than or // equal to 50 ?????????? { // FINISH ME // set the variable gameOver to true gameOver = ????? // FINISH ME // set the scoreLabel.text to "You Win" scoreLabel.???? = ??????? speak("Yeah! You Win!") // this shuts down the timer timer.invalidate() return } } func resetGame() { // FINISH ME // set the score to 0 ??????? resetSkyNight() resetWordTextFieldLocation() // FINISH ME // clear the wordTextField (set it to "") wordTextField.???? = ?? // FINISH ME // set your score to 0 ?????? scoreLabel.text = "Score:\(score)" // FINISH ME // set the scoreLabel to "Score:\(score)" // notice that we are using interpolation to show // the value of the variable score scoreLabel.???? = ?????? // FINISH ME // reset the words on the screen // for each word, // set the word.isHidden field to the value true for ???? in words { // FINISH ME word.?????? = ???? } // remove each explosion from the apple draw list for explosion in explosions { explosion.removeFromSuperview() } // FINISH ME // remove all explosions from the explosions list ?????????.removeAll() // FINISH ME // remove all explosionsTimes from the explosionsTimes list ???????.removeAll() // FINISH ME // set the variable gameOver to the value true gameOver = ???? } // FINISH ME // connect your StartButton here // Control + drag @IBAction func startButtonEvent(_ sender: UIButton) { timer.invalidate() // just in case this button is tapped multiple times resetGame() // FINISH ME // loop through each word in the list words for word in ????? { // FINISH ME // set the word isHidden property to false word.?????? = ????? } // FINISH ME // loop through each word in the list words for word in ????? { // FINISH ME // Move this word off the top of the screen // HINT: call a method ?????????(word) } // FINISH ME // set the variable gameOver to false gameOver = ????? // start the timer // and call timerAction() every 0.02 seconds timer = Timer.scheduledTimer(timeInterval: timerInterval, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true) } // FINISH ME // connect your StopButton here // Control + drag @IBAction func stopButtonEvent(_ sender: UIButton) { // we turn the timer off timer.invalidate() resetGame() } // DO NOT ERASE THIS FUNCTION // APPLE CREATED IT FOR YOU override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // get the width and height of the screen screenWidth = CGFloat(screenSize.width) screenHeight = CGFloat(screenSize.height) self.view.sendSubviewToBack(nightSky) resetSkyNight() resetStartButton() resetStopButton() resetScoreLabelPosition() // these must be in order from bottom up resetWordTextFieldLocation() words.append(word1) words.append(word2) words.append(word3) listOfWords.append("cat") listOfWords.append("dog") listOfWords.append("hat") listOfWords.append("shoe") listOfWords.append("good") listOfWords.append("bye") listOfWords.append("hello") listOfWords.append("mouse") // FINISH ME // here we get a random word from listOfWords // by calling the function randomlySelectWord() word1.text = randomlySelectWord() word2.text = randomlySelectWord() word3.text = randomlySelectWord() } // end of viewDidLoad() } // end of class