// // ViewController.swift // AlienInvaders2 // // // EXTRA CREDIT and HELP /* How to add barricades. 1) Drag an image file of a barricade into the files area (png). 2) Drag two UIImageView objects to your screen. 3) Add the image to both of your UIImageView objects. 4) Add two IBOutlet variables to your variables section. (Put them under alien1, alien2, and alien3 @IBOutlet weak var barricade1: UIImageView! @IBOutlet weak var barricade2: UIImageView! 5) Connect your UIImageView objects to the variables. 6) Define a barricades list variable in your variable section. Put it under var aliens = [UIImageView]() var barricades = [UIImageView]() 7) In your viewDidLoad method, add: barricades.append(barricade1) barricades.append(barricade2) 8) Look at how alienBullets intersected with shipBullets. Make two copies of that method. Modify the method so that you check for alienBullets Intersecting with barricades. Modify the other method so that you check for aliens Intersecting with barricades. Call these functions in the timerAction() function. Do this in the same area where the other intersect functions are located. */ import UIKit class ViewController: UIViewController { // ********************************* // ********************************* // ********************************* // We will define our global variables // below (instance variables) // ********************************* // ********************************* // ********************************* // 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 alienSpeed = CGFloat(2) let alienBulletSpeed = CGFloat(3) let shipBulletSpeed = CGFloat(4) // we load the image of a bullet to be used later let shipBulletImage = UIImage(named: "bullet.png"); // we load the image of an explosion to be used later let explosionImage = UIImage(named: "explosion.png"); // FINISH ME // create a variable called aliens // to refer to the list of all the aliens (UIImageViews) var ??????? = [UIImageView]() // FINISH ME // create a variable called alienBullets // to refer to the list of all the alienBullets var ???????? = [UIImageView]() // FINISH ME // create a variable called shipBullets // to refer to the list of all the shipBullets var ???????? = [UIImageView]() // 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) // ********************************* // ********************************* // ********************************* @IBOutlet weak var nightSky: UIImageView! @IBOutlet weak var shipUpButton: UIButton! @IBOutlet weak var shipRightButton: UIButton! @IBOutlet weak var shipLeftButton: UIButton! @IBOutlet weak var shipDownButton: UIButton! @IBOutlet weak var shipFireButton: UIButton! @IBOutlet weak var startButton: UIButton! @IBOutlet weak var stopButton: UIButton! // 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 alien1 // connect it to your UIImageView object // that is on the UIViewController @IBOutlet weak var alien1: UIImageView! // FINISH ME // this defines the variable alien2 // connect it to your UIImageView object // that is on the UIViewController @IBOutlet weak var alien2: UIImageView! // FINISH ME // this defines the variable alien3 // connect it to your UIImageView object // that is on the UIViewController @IBOutlet weak var alien3: UIImageView! // FINISH ME // this defines the variable ship // connect it to your UIImageView object // that is on the UIViewController @IBOutlet weak var ship: UIImageView! // ********************************* // ********************************* // ********************************* // We will connect our IBAction functions // (events) below // ********************************* // ********************************* // ********************************* // FINISH ME // this defines the function moveUp // connect it to your Move Up Button object // that is on the UIViewController @IBAction func moveUp(_ sender: UIButton) { // FINISH ME // this gets the x and y value of the UIImageView let x = ship.frame.origin.x var y = ship.frame.?????.? // FINISH ME // check to see if y is greater than 10 if ? > ?? { // FINISH ME // decrease y by 10 y = ? - ?? } // FINISH ME // this gets the width and height of the UIImageView let width = ship.frame.size.???? let height = ship.frame.size.?????? // this resets the frame of the UIImageView object ship.frame = CGRect(x: x, y: y, width: width, height: height) } // FINISH ME // this defines the function moveDown // connect it to your Move Down Button object // that is on the UIViewController @IBAction func moveDown(_ sender: UIButton) { // FINISH ME // this gets the x and y value of the UIImageView let x = ship.frame.origin.? var y = ship.frame.?????.? // FINISH ME // check to see if y is less than screenHeight if ? < ???????? { // FINISH ME // increase y by 20 y = ?????? } // FINISH ME // this gets the width and height of the UIImageView let width = ship.frame.size.????? let height = ship.frame.size.????? // this resets the frame of the UIImageView object ship.frame = CGRect(x: x, y: y, width: width, height: height) } // FINISH ME // this defines the function moveRight // connect it to your Move Right Button object // that is on the UIViewController @IBAction func moveRight(_ sender: UIButton) { // FINISH ME // this gets the x and y value of the UIImageView var x = ship.?????? let y = ship.?????? // FINISH ME // check to see if x is less than screenWidth if ??????? { // FINISH ME // increase x by 20 x = ??????? } // FINISH ME // this gets the width and height value of the UIImageView let width = ship.frame.size.????? let height = ship.frame.????.???? // this resets the frame of the UIImageView object ship.frame = CGRect(x: x, y: y, width: width, height: height) } // FINISH ME // this defines the function moveLeft // connect it to your Move Left Button object // that is on the UIViewController @IBAction func moveLeft(_ sender: UIButton) { // FINISH ME // this gets the x and y value of the UIImageView var x = ship.????? let y = ship.????? // FINISH ME // check to see if x is greater than 20 ??????? { // FINISH ME // decrease x by 20 x = ?????? } // FINISH ME // this gets the width and height value of the UIImageView let width = ship.frame.size.???? let height = ship.frame.????.???? // this resets the frame of the UIImageView object ship.frame = CGRect(x: x, y: y, width: width, height: height) } // 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.origin.? - 0.2 let y = nightSky.frame.origin.? // 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.size.????? 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(20) 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 resetSkyNight func resetStartButton() { let x = screenWidth * 0.1 let y = CGFloat(screenHeight) - startButton.frame.size.height - 30 // FINISH ME // this gets the width and height value of the startButton let width = startButton.frame.size.???? let height = startButton.frame.????.???? // this resets the frame of the UIImageView object startButton.frame = CGRect(x: x, y: y, width: width, height: height) } // FINISH ME // this defines the function resetSkyNight func resetStopButton() { let x = screenWidth * 0.8 let y = CGFloat(screenHeight) - stopButton.frame.size.height - 30 // FINISH ME // this gets the width and height value of the startButton let width = stopButton.frame.size.????? let height = stopButton.frame.????.????? // this resets the frame of the UIImageView object stopButton.frame = CGRect(x: x, y: y, width: width, height: height) } // FINISH ME // this defines the function resetShipLocation() func resetShipLocation() { // FINISH ME // this defines the function resetShipUpButton() let width = ship.frame.size.????? let height = ship.frame.????.????? let x = screenWidth * 0.5 - width/2 let y = CGFloat(screenHeight) - ship.frame.size.height - 30 // this resets the frame of the UIImageView object ship.frame = CGRect(x: x, y: y, width: width, height: height) } // FINISH ME // this defines the function resetShipUpButton() func resetShipUpButton() { // FINISH ME // get the width and height of the shipUpButton let width = shipUpButton.frame.size.???? let height = shipUpButton.frame.????.????? let x = screenWidth * 0.5 - width/2 let dist1 = ship.frame.size.height + 30 let dist2 = shipDownButton.frame.size.height + 20 let dist3 = shipFireButton.frame.size.height + 10 let dist4 = shipUpButton.frame.size.height + 10 let y = CGFloat(screenHeight) - dist1 - dist2 - dist3 - dist4 // this resets the frame of the UIImageView object shipUpButton.frame = CGRect(x: x, y: y, width: width, height: height) } // FINISH ME // this defines the function resetShipFireButton() func resetShipFireButton() { // FINISH ME // this gets the width and height value of the UIImageView let width = shipFireButton.frame.????.?????? let height = shipFireButton.frame.????.?????? let x = screenWidth * 0.5 - width/2 let dist1 = ship.frame.size.height + 30 let dist2 = shipDownButton.frame.size.height + 20 let dist3 = shipFireButton.frame.size.height + 10 let y = CGFloat(screenHeight) - dist1 - dist2 - dist3 // this resets the frame of the UIImageView object shipFireButton.frame = CGRect(x: x, y: y, width: width, height: height) } // FINISH ME // this defines the function resetShipFireButton() func resetShipLeftButton() { // FINISH ME // this gets the width and height value of the UIImageView let width = shipLeftButton.frame.size.?????? let height = shipLeftButton.frame.size.?????? let x = screenWidth * 0.5 - shipFireButton.frame.size.width/2 - 20 - width let dist1 = ship.frame.size.height + 30 let dist2 = shipDownButton.frame.size.height + 20 let dist3 = shipFireButton.frame.size.height + 10 let y = CGFloat(screenHeight) - dist1 - dist2 - dist3 // this resets the frame of the UIImageView object shipLeftButton.frame = CGRect(x: x, y: y, width: width, height: height) } // FINISH ME // this defines the function resetShipFireButton() func resetShipRightButton() { // FINISH ME // this gets the width and height value of the UIImageView let width = shipRightButton.frame.size.????? let height = shipRightButton.frame.size.????? let x = screenWidth * 0.5 + shipFireButton.frame.size.width/2 + 20 let dist1 = ship.frame.size.height + 30 let dist2 = shipDownButton.frame.size.height + 20 let dist3 = shipFireButton.frame.size.height + 10 let y = CGFloat(screenHeight) - dist1 - dist2 - dist3 // this resets the frame of the UIImageView object shipRightButton.frame = CGRect(x: x, y: y, width: width, height: height) } func resetShipDownButton() { // FINISH ME // this gets the width and height value of the UIImageView let width = shipDownButton.frame.size.????? let height = shipDownButton.frame.size.????? let x = screenWidth * 0.5 - width/2 let dist1 = ship.frame.size.height + 30 let dist2 = shipDownButton.frame.size.height + 20 let y = CGFloat(screenHeight) - dist1 - dist2 // this resets the frame of the UIImageView object shipDownButton.frame = CGRect(x: x, y: y, width: width, height: height) } // ********************************* // ********************************* // ********************************* // We will write our own functions // below // ********************************* // ********************************* // ********************************* func moveShipBulletUp(_ bullet:UIImageView) { // FINISH ME // get the x and y values from the // bullet's frame let x = bullet.frame.?????.? var y = bullet.frame.?????.? // FINISH ME // get the width and height values from the // bullet's frame let width = bullet.frame.????.????? let height = bullet.frame.????.????? // FINISH ME // decrease y by shipBulletSpeed y = ? - ???????? bullet.frame = CGRect(x: x, y: y, width: width, height: height) } // moveAlienBulletDown(alienBullet) func moveAlienBulletDown(_ alienBullet:UIImageView) { // FINISH ME // get the x and y values from the // alienBullet's frame let x = alienBullet.frame.????.? var y = alienBullet.frame.????.? // FINISH ME // get the width and height values from the // alienBullet's frame let width = alienBullet.frame.????.???? let height = alienBullet.frame.????.???? // FINISH ME // increase y by alienBulletSpeed y = ?????? // reset the frame alienBullet.frame = CGRect(x: x, y: y, width: width, height: height) } func moveAlienDown(_ alien:UIImageView) { // FINISH ME // get the x and y values from the // alien's frame let x = alien.frame.?????.? var y = alien.frame.?????.? // FINISH ME // get the width and height values from the // alien's frame let width = alien.frame.????.??? let height = alien.frame.???.??? // FINISH ME // increase y by alienSpeed y = ? + ?????? alien.frame = CGRect(x: x, y: y, width: width, height: height) } func moveAlienOffTopOfScreen(_ alien:UIImageView) { // FINISH ME // get the x and y values from the // bullet's frame var x = alien.frame.?????.? var y = alien.frame.?????.? let width = alien.frame.size.width let height = alien.frame.size.height // gets a random x value from 0 to screenWidth x = CGFloat(arc4random_uniform(UInt32(screenWidth))) - width // FINISH ME // see if x is less than 0 ??????? { // FINISH ME // set x to 0 ????? } // FINISH ME // set y to -350 ?????? alien.frame = CGRect(x: x, y: y, width: width, height: height) } func removeShipBulletsGoingOffTheScreen() { // FINISH ME // loop through each shipBullet in the shipBullets list // and check for going off the screen var i = 0 while i < ???????s.count { // see if the alien goes off the screen let shipBullet = shipBullets[i] if shipBullet.frame.origin.y + shipBullet.frame.size.height < 0 { shipBullet.removeFromSuperview() shipBullets.remove(at:i) // FINISH ME // subtract 1 from your score ?????????????? } else { // FINISH ME // increase i by adding 1 ?????????????? } } } // end of method removeShipBulletsGoingOffTheScreen() // 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 < ??????????.count { // this gets the ith explosion in the explosions list 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 i = ????????? } } } // end of method removeAlienBulletsGoingOffTheScreen() func removeAlienBulletsGoingOffTheScreen() { // FINISH ME // create a variable called i and set to 0 var ??????? // FINISH ME // loop through each alienBullet // i must be less than alienBullets.count while i < ????????.count { // FINISH ME // get the ith alienBullet in the alienBullets list let alienBullet = ??????????[i] // FINISH ME // see if the alienBullet goes off the screen // alienBullets are moving down if alienBullet.frame.origin.? > ????????? { alienBullet.removeFromSuperview() alienBullets.remove(at:i) // FINISH ME // subtract 1 from your score score = ?????????? } else { // FINISH ME // increase i by adding 1 i = ???????? } } } // end of method removeAlienBulletsGoingOffTheScreen() func removeAliensGoingOffTheScreen() { // FINISH ME // loop through each alien // and check for going off the screen for alien in ?????????? { // FINISH ME // see if the alien goes off the bottom of the screen if alien.frame.origin.? > ????????? { // FINISH ME // move this alien off the top of the screen // hint: call a method and send it the alien ??????????????(alien) // FINISH ME // subtract 2 from your score score = ???????????? } } } func checkForCollisionsBetweenShipAndAliens() { // FINISH ME // loop through each alien // and check for an intersection with the ship for alien in ??????? { if ship.frame.intersects(alien.frame) { // FINISH ME // subtract 1 from your score score = ???????? // FINISH ME // move alien off the top of the screen // hint: call a method ????????????(alien) // create an explosion // over the ship // HINT: Call a method (function) // and send to it ship ???????????(ship) } } } func checkForCollisionsBetweenShipAndAlienBullets() { // FINISH ME // create a variable i and set it to zero // this variable represents the index position // of the alienBullet in the array var ?????? // FINISH ME // loop through each alienBullet // and check for an intersection with the ship // i must be less than alienBullets.count while i < ??????????.count { // FINISH ME // assign alienBullets[i] let alienBullet = ????????[?] if ship.frame.intersects(alienBullet.frame) { // FINISH ME // subtract 1 from your score score = ????????? alienBullet.removeFromSuperview() alienBullets.remove(at: i) // FINISH ME // create an explosion // over the ship // HINT: Call a method (function) // and send to it ship ??????????????(ship) } else { // FINISH ME // increase i by 1 i = ???????? } } } func checkForCollisionsBetweenShipBulletsAndAliens() { // FINISH ME // loop through each alien for alien in ???????? { // FINISH ME // create a variable called i and set it to 0 // this is used as an index var ?????? // FINISH ME // we will loop through each ship bullet // i must be less than shipBullets.count while i < ????????.count { // FINISH ME // access shipBullets[i] let shipBullet = ?????????[?] if shipBullet.frame.intersects(alien.frame) { // FINISH ME // add 5 to your score score = ??????????? // FINISH ME // move alien off the top of the screen // hint: call a method ????????????????(alien) // create an explosion // over the shipBullet // HINT: Call a method (function) // and send to it shipBullet ???????????(shipBullet) shipBullet.removeFromSuperview() shipBullets.remove(at: i) } else { // FINISH ME // increase i by adding 1 i = ??????? } } // end of while i < } // end of for alien in aliens } // end of func func checkForCollisionsBetweenShipBulletsAndAlienBullets() { // FINISH ME // create a variable k and set to 0 var ? ? ? // FINISH ME // loop through each of the alienBullets // k needs to be less than alienBullets.count while k < ?????????.count { // FINISH ME // create a variable called i and set it to 0 // this is used as an index var ? = ? // FINISH ME // we will loop through each ship bullet // i must be less than shipBullets.count while i < shipBullets.count { let alienBullet = alienBullets[k] // FINISH ME // access shipBullets[i] let shipBullet = ??????????? if shipBullet.frame.intersects(alienBullet.frame) { // FINISH ME // add 2 to your score score = ????????? // FINISH ME // create an explosion // over the shipBullet // HINT: Call a method (function) // and send to it shipBullet ?????????????(shipBullet) shipBullet.removeFromSuperview() shipBullets.remove(at: i) alienBullet.removeFromSuperview() alienBullets.remove(at: k) break } else { // FINISH ME // increase i by adding 1 i = ???????? } } // end of while i < // FINISH ME // add 1 to your k value k = ???????? // move to the next alien bullet } //end of while i < alienBullets.count } // end of func // we will increase the size of this explosion func moveExplosion(_ explosion:UIImageView) { // FINISH ME // get the object's coordinates and // adjust them let x = explosion.frame.origin.? - 0.1 let y = explosion.frame.origin.? - 0.1 let width = explosion.frame.size.????? + 0.2 let height = explosion.frame.size.????? + 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 moveShipBulletsUp() { // FINISH ME // loop through all shipBullets // shipBullet will refer to one ship bullet // in the shipBullets array // each time through the loop for shipBullet in ????????? { // FINISH ME // Move the shipBullet up // HINT: Call a method and // send to it the shipBullet ?????????(shipBullet) } } func moveAlienBulletsDown() { // FINISH ME // loop through all alienBullets for alienBullet in ??????????? { // FINISH ME // alienBullet is referring to 1 alienBullet // each time through the loop // Move this alienBullet down // HINT: Call a method (function) // and send to it alienBullet ??????????????(alienBullet) } } func moveAliensDown() { // FINISH ME // loop through all aliens for alien in ????? { // FINISH ME // alien is referring to 1 alien // each time through the loop // Move this alien down // HINT: Call a method (function) // and send to it alien ?????????(????) } } func createShipBullet() { // FINISH ME // get the ship's coordinates var x = ship.frame.origin.? let shipWidth = ship.frame.size.width var y = ship.frame.?????.? // we will set the width and height let width = CGFloat(25) let height = CGFloat(40) // we want to put the bullet above // the ship and centered x = x + shipWidth/2 - 12 // center of ship y = y - 42 // subtract height of bullet + 2 let shipBullet = UIImageView(frame: CGRect(x:x, y:y, width:width, height:height)) // we add the image to the shipBullet object shipBullet.image = shipBulletImage // we add the shipBullet to the Apple draw List self.view.addSubview(shipBullet) // FINISH ME // append (add) this shipBullet to // shipBullets ????????.?????(shipBullet) } // creates an explosion object // over the sent object and // adds it the explosions list func createAnExplosion(_ object:UIImageView) { // FINISH ME // get the object's coordinates let x = object.frame.origin.? let y = object.frame.?????.? let width = object.frame.size.???? let height = object.frame.????.???? let explosion = UIImageView(frame: CGRect(x:x, y:y, width:width, height:height)) explosion.image = explosionImage // this adds the explosion to the Apple draw list self.view.addSubview(explosion) // FINISH ME // append (add) this explosion to // explosions ???????.???????(explosion) // FINISH ME // append (add) 1.0 to // explosionsTimes ????????????/.?????(1.0) } func createAlienBullet(_ alien:UIImageView) { // get the alien's coordinates // FINISH ME // get the alien's x value var x = alien.frame.origin.? let alienWidth = alien.frame.size.width // FINISH ME // get the alien's y value var y = alien.frame.?????.? let alienHeight = alien.frame.size.height let bulletWidth = CGFloat(25) let bulletHeight = CGFloat(40) // we want to put the bullet below // the alien and centered x = x + alienWidth/2 - 12 // center of alien y = y + alienHeight + 2 // add height of alien plus 2 let alienBullet = UIImageView(frame: CGRect(x:x, y:y, width:bulletWidth, height:bulletHeight)) alienBullet.image = shipBulletImage self.view.addSubview(alienBullet) // FINISH ME // append (add) the alienBullet to // the alienBullets array ?????????.??????(alienBullet) } func randomlyCreateAlienBullets() { // FINISH ME // loop through all of the aliens for alien in ?????? { let r = CGFloat(arc4random_uniform(UInt32(700))) // FINISH ME // see if r is equal to 2 ?????????? { // FINISH ME // create an alien bullet // call a method (function) ????????????????? } } } // FINISH ME // connect your Fire button here @IBAction func fireButtonEvent(_ sender: UIButton) { // FINISH ME // call a method to create a ship bullet ??????????????? } // ****************************** // ****************************** // ****************************** // main timer loop (event) // this gets called every 0.01 seconds // i.e it runs every 0.01 seconds // (or whatever it is set to) // ****************************** // ****************************** // ****************************** @objc func timerAction() { // FINISH ME // here we check to see if the gameOver variable // is true ??????????????? { return // exits (quits) the function } // ********************************* // ********************************* // ********************************* // We will move things below // remove stuff // and create stuff below // ********************************* // ********************************* // ********************************* moveSkyNightLeft() moveExplosions() updateAndRemoveExplosionsIfTimeIsUp() randomlyCreateAlienBullets() // FINISH ME // move the alienBullets down // HINT: Call a method (function) ???????????????????? removeAlienBulletsGoingOffTheScreen() // FINISH ME // move the aliens down // HINT: Call a method (function) ???????????????? // FINISH ME // Remove aliens going off the screen // HINT: Call a method (function) ?????????????????? // FINISH ME // Move the ship bullets up // HINT: Call a method (function) ??????????????????? // FINISH ME // Remove a ship bullet going off the screen // Call a method (function) ????????????????????????? // ********************************* // ********************************* // ********************************* // We will check for collisions below // ********************************* // ********************************* // ********************************* // we check for collisions (intersections) // between ship bullets and aliens checkForCollisionsBetweenShipBulletsAndAliens() // FINISH ME // Check for collisions between the ship // and aliens // HINT: Call a method (function) ????????????????????????????? // FINISH ME // Check for collisions between the ship bullets // and the alienBullets // HINT: Call a method (function) ??????????????????????????????? // FINISH ME // Check for collisions between the ship // and the alienBullets // HINT: Call a method (function) ??????????????????????????????? // ********************************* // ********************************* // ********************************* // We will check for a win or a loss // and update the screen below // ********************************* // ********************************* // ********************************* // 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 -300 ??????????? { // FINISH ME // set the variable gameOver to true gameOver ? ?????? // FINISH ME // set the scoreLabel.text to "You Lose" scoreLabel.???? ? ???????? // 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.???? ? ?????????? // this shuts down the timer timer.invalidate() return } } func resetGame() { // FINISH ME // set the score to 0 // (DO NOT USE var or let) It has already been created. ??????? resetSkyNight() resetShipLocation() // FINISH ME // set the scoreLabel to "Score:\(score)" // notice that we are using interpolation to show // the value of the variable score scoreLabel.text ? ??????????? // FINISH ME // reset the aliens on the screen // for each alien, // set the alien.isHidden to the value true for alien in aliens { // FINISH ME alien.isHidden ? ??????? } // FINISH ME // Loop through all alienBullets // remove each alien bullet from the apple draw list for alienBullet in ?????????? { alienBullet.removeFromSuperview() } // FINISH ME // remove all alienBullets from the alienBullets list // call the alienBullets removeAll() method alienBullets.????????? // FINISH ME // Loop through all shipBullets // remove each ship bullet from the apple draw list for shipBullet in ???????? { shipBullet.removeFromSuperview() } // FINISH ME // remove all shipBullets from the shipBullets list shipBullets.????????? // remove each explosion from the explosions list for explosion in ????????? { explosion.removeFromSuperview() } // FINISH ME // remove all explosions from the explosions list explosions.??????? // FINISH ME // remove all explosionsTimes from the explosionsTimes list explosionsTimes.?????? // FINISH ME // set the variable gameOver to the value true gameOver ? ????? } // ********************************* // ********************************* // ********************************* // We will connect our startButtonEvent, // and stopButtonEvent below // ********************************* // ********************************* // ********************************* // 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 alien in the list aliens for alien in ?????? { // FINISH ME // set the alien isHidden property to false alien.isHidden ? ???? } // FINISH ME // loop through each alien in the list aliens for alien in ????? { // FINISH ME // Move this alien off the top of the screen // HINT: call a method (function) // send alien to the method (function) ????? } // 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() } 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() // these must be in order from bottom up resetShipLocation() resetShipDownButton() resetShipFireButton() resetShipUpButton() resetShipLeftButton() resetShipRightButton() // FINISH ME // here we add each alien to the list aliens // add alien2 and alien3 to the aliens list aliens.append(alien1) ????? ????? } }