// // ViewController.swift // AlienInvaders2 // import UIKit class ViewController: UIViewController { // ********************************* // ********************************* // ********************************* // We will define our global variables // below (instance variables) // ********************************* // ********************************* // ********************************* // FINISH ME // create a variable called timer ??? ????? = Timer() var timerInterval = 0.01 // 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 ??? ??????? = CGFloat(0) // FINISH ME // create a variable caled screenWidth ??? ??????? = CGFloat(0) // FINISH ME // create a variable called gameOver and assign // it a value of true ??? ????? = ???? // FINISH ME // create a variable called score and assign // it a value of 0 ??? ????? = ? 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 ??? ????? = [UIImageView]() // FINISH ME // create a variable called alienBullets // to refer to the list of all the alienBullets ??? ????????? = [UIImageView]() // FINISH ME // create a variable called shipBullets // to refer to the list of all the shipBullets ??? ??????? = [UIImageView]() // FINISH ME // create a variable called explosions // to refer to the list of all the explosions ??? ????????? = [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 ??? ????? = [Double]() // ********************************* // ********************************* // ********************************* // We will connect our IBOutlet variables // below (instance variables, reference // variables) // ********************************* // ********************************* // ********************************* // 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) { // this gets the x and y value of the UIImageView let x = ship.frame.origin.x var y = ship.frame.origin.y // FINISH ME // check to see if y is greater than 10 if ? ? ?? { // FINISH ME // decrease y by 10 y = ? - ?? } // this gets the width and height of the UIImageView let width = ship.frame.size.width let height = ship.frame.size.height // 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) { // this gets the x and y value of the UIImageView let x = ship.frame.origin.? var y = ship.frame.origin.? // FINISH ME // check to see if y is less than screenHeight if ? ? ??????? { // FINISH ME // increase y by 20 y = ?????? } // this gets the width and height of the UIImageView let width = ship.frame.size.width let height = ship.frame.size.height // 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) { // this gets the x and y value of the UIImageView var x = ship.frame.origin.x let y = ship.frame.origin.y // FINISH ME // check to see if x is less than screenWidth if ?????? { // FINISH ME // increase x by 20 ??????? } // this gets the width and height value of the UIImageView let width = ship.frame.size.width let height = ship.frame.size.height // 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.frame.origin.? let y = ship.frame.origin.? // FINISH ME // check to see if x is greater than 20 if ???? { // FINISH ME // decrease x by 20 ???????? } // FINISH ME // this gets the width and height value of the UIImageView let width = ship.frame.size.width let height = ship.frame.size.height // this resets the frame of the UIImageView object ship.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.origin.? var y = bullet.frame.origin.? let width = bullet.frame.size.width let height = bullet.frame.size.height // FINISH ME // decrease y by shipBulletSpeed ???????? 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.origin.? var y = alienBullet.?.?.? let width = alienBullet.frame.size.width let height = alienBullet.frame.size.height // FINISH ME // increase y by alienBulletSpeed ???? // 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.origin.? var y = alien.????? let width = alien.frame.size.width let height = alien.frame.size.height // FINISH ME // increase y by alienSpeed ??????? 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.??? var y = alien.??? 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 if x < 0 { x = 0 } // FINISH ME // set y to -350 ???? alien.frame = CGRect(x: x, y: y, width: width, height: height) } func removeShipBulletsGoingOffTheScreen() { var i = 0 // FINISH ME // loop through each shipBullet // and check for going off the screen // i will need to be less than shipBullets.count while ???? { // 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 ???? // FINISH ME // loop through each explosion // i must be less than explosions.count while ???? { 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 if ?????? { explosion.removeFromSuperview() explosions.remove(at:i) explosionsTimes.remove(at:i) } else { // FINISH ME // increase i by adding 1 ???? } } } // end of method removeAlienBulletsGoingOffTheScreen() func removeAlienBulletsGoingOffTheScreen() { // FINISH ME // create a variable called i and set to 0 ?????? // FINISH ME // loop through each alienBullet // i must be less than alienBullets.count while ??????? { // see if the alien goes off the screen let alienBullet = alienBullets[i] if alienBullet.frame.origin.y > screenHeight { alienBullet.removeFromSuperview() alienBullets.remove(at:i) // FINISH ME // subtract 1 from your score ????? } else { // FINISH ME // increase i by adding 1 ?????? } } } // end of method removeAlienBulletsGoingOffTheScreen() func removeAliensGoingOffTheScreen() { // FINISH ME // loop through each alien // and check for going off the screen for ????? in aliens { // see if the alien goes off the screen if alien.frame.origin.y > screenHeight { // 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 ?????? } } } func checkForCollisionsBetweenShipAndAliens() { // FINISH ME // loop through each alien // and check for an intersection with the ship for ????? in aliens { if ship.frame.intersects(alien.frame) { // FINISH ME // subtract 1 from your 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 ??????? // FINISH ME // loop through each alienBullet // and check for an intersection with the ship // i must be less than alienBullets.count while ???????? { // FINISH ME // assign alienBullets[i] to alienBullet let ???????? = alienBullets[i] if ship.frame.intersects(alienBullet.frame) { // FINISH ME // subtract 1 from your 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 ?????? } } } func checkForCollisionsBetweenShipBulletsAndAliens() { // FINISH ME // loop through each alien for alien in aliens { // FINISH ME // create a variable called i and set it to 0 // this is used as an index ?????? // FINISH ME // we will loop through each ship bullet // i must be less than shipBullets.count while ??????? { // FINISH ME // assign shipBullets[i] to shipBullet let shipBullet = ?????? if shipBullet.frame.intersects(alien.frame) { // FINISH ME // add 5 to your 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 ????????? } } // 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 ???? // FINISH ME // loop through each of the alienBullets // k needs to be less than alienBullets.count while ???????? { // FINISH ME // create a variable called i and set it to 0 // this is used as an index ??????? // 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 // assign shipBullets[i] let shipBullet = ??????? if shipBullet.frame.intersects(alienBullet.frame) { // FINISH ME // add 2 to your 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 ?????? } } // end of while i < // FINISH ME // add 1 to k ?????? // moves to the next alien bullet } //end of while i < alienBullets.count } // end of func // 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 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 ?????????(alien) } } func createShipBullet() { // get the ship's coordinates var x = ship.frame.origin.x var y = ship.frame.origin.y let width = CGFloat(25) let height = CGFloat(40) // we want to put the bullet above // the ship and centered x = x + width/2 - 12 // center of ship y = y - 50 // subtract height of bullet let shipBullet = UIImageView(frame: CGRect(x:x, y:y, width:width, height:height)) shipBullet.image = shipBulletImage self.view.addSubview(shipBullet) // FINISH ME // append (add) this shipBullet to // shipBullets shipBullets.?????(shipBullet) } // creates an explosion object // over the sent object and // adds it the explosions list func createAnExplosion(_ object:UIImageView) { // 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 ????????.??????(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.? // FINISH ME // get the alien's y value var y = alien.frame.origin.? let width = CGFloat(25) let height = CGFloat(40) // we want to put the bullet below // the alien and centered x = x + width/2 - 12 // center of alien y = y + height + 2 // add height of alien plus 2 let alienBullet = UIImageView(frame: CGRect(x:x, y:y, width:width, height:height)) 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 aliens { let r = CGFloat(arc4random_uniform(UInt32(700))) // FINISH ME // see if r is equal to 2 if ???????? { createAlienBullet(alien) } } } // 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 // ****************************** // ****************************** // ****************************** @objc func timerAction() { // FINISH ME // here we check to see if the gameOver variable // is false if ?????? { return // exits (quits) the function } // ********************************* // ********************************* // ********************************* // We will move things below // remove stuff // and create stuff below // ********************************* // ********************************* // ********************************* 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)" ?????????????? // FINISH ME // check to see if the score is less than or // equal to -300 if ???????? { // FINISH ME // set the variable gameOver to true ?????? // FINISH ME // set the scoreLabel.text to "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 if ??????? { // FINISH ME // set the variable gameOver to true ???????? // FINISH ME // set the scoreLabel.text to "You Win" ????????? // this shuts down the timer timer.invalidate() return } } func resetGame() { // FINISH ME // set the score to 0 ?????? // FINISH ME // set the scoreLabel to "Score:\(score)" // notice that we are using interpolation to show // the value of the variable score ???????? // FINISH ME // reset the aliens on the screen // for each alien, // set the alien.isHidden to the value true for alien in ??????? { // FINISH ME ??????? } // remove each alien bullet from the apple draw list for alienBullet in alienBullets { alienBullet.removeFromSuperview() } // FINISH ME // remove all alienBullets from the alienBullets list // call the alienBullets removeAll() method alienBullets.????? // remove each ship bullet from the apple draw list for shipBullet in shipBullets { shipBullet.removeFromSuperview() } // FINISH ME // remove all shipBullets from the shipBullets list shipBullets.?????? // remove each explosion from the apple draw list for explosion in explosions { 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 ??????? } // ********************************* // ********************************* // ********************************* // 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 ??????? } // 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 ??????????(alien) } // FINISH ME // set the variable gameOver to false ?????? // 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) // FINISH ME // here we add each alien to the list aliens // add alien2 and alien3 to the aliens list aliens.append(alien1) ??????? ??????? } }