// // ViewController.swift // The Other Side // // You are trying to get to the top of // the screen to win or survive for a // certain amount of time. // If you hit a witch, cat, or bat // you lose (or the logic is in // here to adjust points as well) import UIKit class ViewController: UIViewController { var timer = Timer() // defines a variable for the screen size let screenSize:CGRect = UIScreen.main.bounds // defines variables for the screen // height and screen width // which are set in the viewDidLoad() var screenHeight = CGFloat(0) var screenWidth = CGFloat(0) // defines variables to hold the // original ship coordinates which will // be set in the viewDidLoad() var shipStartX = CGFloat(0) var shipStartY = CGFloat(0) var shipStartWidth = CGFloat(0) var shipStartHeight = CGFloat(0) var shipSpeedX = CGFloat(1) var shipSpeedY = CGFloat(2) // defines variables to hold the // original ship coordinates which will // be set in the viewDidLoad() var witchSpeedX = CGFloat(2) var catSpeedX = CGFloat(1) var batSpeedX = CGFloat(3) // 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 ??? = ? // FINISH ME // create a variable called wins and assign // it a value of 0 ???? // FINISH ME // create a variable called losses and assign // it a value of 0 ???? // this variable determines which direction // the ship moves in // "NONE", "UP", "RIGHT", or "LEFT" var directionOfShip = "NONE" // 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 winsLabel // connect it to your Label object // that is on the UIViewController which // says "Wins: 0" on it. @IBOutlet weak var winsLabel: UILabel! // FINISH ME // this defines the variable lossesLabel // connect it to your Label object // that is on the UIViewController which // says "Losses: 0" on it. @IBOutlet weak var lossesLabel: UILabel! // FINISH ME // this defines the variable witch // connect it to your UIImageView object // that is on the UIViewController @IBOutlet weak var witch: UIImageView! // FINISH ME // this defines the variable cat // connect it to your UIImageView object // that is on the UIViewController @IBOutlet weak var cat: UIImageView! // FINISH ME // this defines the variable bat // connect it to your UIImageView object // that is on the UIViewController @IBOutlet weak var bat: UIImageView! // FINISH ME // this defines the variable ship // connect it to your UIImageView object // that is on the UIViewController @IBOutlet weak var ship: UIImageView! func moveShipUp() { // this gets the x value of the UIImageView let x = ship.frame.origin.x // FINISH ME // get the ship's y value var y = ???? // FINISH ME // check to see if y is greater than 20 if ???? { // FINISH ME // decrease y by shipSpeedY 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) } @IBAction func moveUp(_ sender: UIButton) { // FINISH ME // call the method moveShipUp() ????? directionOfShip = "UP" } func moveShipLeft() { // FINISH ME // 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 greater than shipSpeedX if ?????? { // FINISH ME // decrease x by shipSpeedX x = ???? } // 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 // call the method moveShipLeft() ??? directionOfShip = "LEFT" } func moveShipRight() { // FINISH ME // this should get the x and y value of the ship's UIImageView var x = ??? let y = ??? // FINISH ME // check to see if x is less than screenWidth ????? { // FINISH ME // increase x by shipSpeedX ???? } // 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) } @IBAction func moveRight(_ sender: UIButton) { // FINISH ME // call the method moveShipRight() ???? directionOfShip = "RIGHT" } func moveWitch() { var x = witch.frame.origin.x // FINISH ME // add witchSpeedX to your x value ????? if x > screenWidth { // set x to -100 ???? } let y = witch.frame.origin.y let width = witch.frame.size.width let height = witch.frame.size.height // this resets the frame of the UIImageView object witch.frame = CGRect(x: x, y: y, width: width, height: height) } func moveWitchOffScreen() { var x = witch.frame.origin.x // FINISH ME // set x to -600 ???? let y = witch.frame.origin.y let width = witch.frame.size.width let height = witch.frame.size.height // this resets the frame of the UIImageView object witch.frame = CGRect(x: x, y: y, width: width, height: height) } func moveCat() { var x = cat.frame.origin.x // FINISH ME // add catSpeedX to your x value ????? if x > screenWidth { // FINISH ME // set x to -100 ????? } let y = cat.frame.origin.y let width = cat.frame.size.width let height = cat.frame.size.height // this resets the frame of the UIImageView object cat.frame = CGRect(x: x, y: y, width: width, height: height) } func moveCatOffScreen() { var x = cat.frame.origin.x // FINISH ME // set x to -200 ???? let y = cat.frame.origin.y let width = cat.frame.size.width let height = cat.frame.size.height // this resets the frame of the UIImageView object cat.frame = CGRect(x: x, y: y, width: width, height: height) } func moveBat() { var x = bat.frame.origin.x // FINISH ME // add batSpeedX to your x value ???? // FINISH ME // check to see if your x value is greater than your screenWidth if ??? { // FINISH ME // set x to -100 ??? } let y = bat.frame.origin.y let width = bat.frame.size.width let height = bat.frame.size.height // this resets the frame of the UIImageView object bat.frame = CGRect(x: x, y: y, width: width, height: height) } func moveBatOffScreen() { var x = bat.frame.origin.x // FINISH ME // set x to -400 ????? let y = bat.frame.origin.y let width = bat.frame.size.width let height = bat.frame.size.height // this resets the frame of the UIImageView object bat.frame = CGRect(x: x, y: y, width: width, height: height) } @IBAction func startButtonEvent(_ sender: UIButton) { timer.invalidate() // just in case this button is tapped multiple times // FINISH ME // set your score to 0 ??? // FINISH ME // set the scoreLabel.text to "Score:\(score)" scoreLabel.text = ??? // FINISH ME // set witch.isHidden to false witch.isHidden = ???? moveWitchOffScreen() // FINISH ME // set cat.isHidden to false ????? moveCatOffScreen() // FINISH ME // set bat.isHidden to false ??? moveBatOffScreen() // FINISH ME // set the variable gameOver to false gameOver ? ???? directionOfShip = "NONE" // reset the ship to the original coordinates ship.frame = CGRect(x: shipStartX, y: shipStartY, width: shipStartWidth, height: shipStartHeight) // start the timer // and call timerAction() every 0.02 seconds timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true) } @IBAction func stopButtonEvent(_ sender: UIButton) { // we turn the timer off timer.invalidate() // 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 scoreLabel.text ? ??????? // FINISH ME // reset the witch, cat, and bat on the screen // set the witch.isHidden to the value true // and set the cat.isHidden to the value true // and set the bat.isHidden to the value true ??? ??? ??? // move the witch, cat, and bat // off the screen // FINISH ME // call the method moveWitchOffScreen() ??? moveCatOffScreen() moveBatOffScreen() // reset the ship to the original coordinates ship.frame = CGRect(x: shipStartX, y: shipStartY, width: shipStartWidth, height: shipStartHeight) // FINISH ME // set the variable gameOver to the value true ???? } // this gets called (it runs the code) every 0.01 seconds @objc func timerAction() { // here we check to see if the gameOver variable // is true if gameOver { return // exits (quits) the function } // FINISH ME // move the witch // hint: call a method ??? // FINISH ME // move the cat // hint: call a method ??? // FINISH ME // move the bat // hint: call a method ??? if directionOfShip == "NONE" { // do nothing } else if directionOfShip == "UP" { // FINISH ME // call a method ??? } else if directionOfShip == "LEFT" { // FINISH ME // call a method ??? } else if directionOfShip == "RIGHT" { // FINISH ME // call a method ???? } if ship.frame.intersects(witch.frame) { // FINISH ME // subtract 1000 from your score score = ??? - ???? // FINISH ME // move the witch off the screen // hint: call a method ???? } if ship.frame.intersects(cat.frame) { // FINISH ME // subtract 1000 from your score score = ?????? // FINISH ME // move the cat off the screen // hint: call a method ????? } if ship.frame.intersects(bat.frame) { // FINISH ME // subtract 1000 from your score ??? = ??? - ???? // FINISH ME // move the bat off the screen // hint: call a method ???? } if witch.frame.origin.x > screenWidth { // FINISH ME // move the witch off the screen // hint: call a method ??? } if cat.frame.origin.x > screenWidth { // FINISH ME // move the witch off the screen // hint: call a method ??? } if bat.frame.origin.x > screenWidth { // FINISH ME // move the witch off the screen // hint: call a method ??? } // FINISH ME // add 1 to your score since you are still alive score = ??? + ? // FINISH ME // set scoreLabel.text to "Score:\(score)" scoreLabel.text ? ???? // FINISH ME // check to see if the score is less than or // equal to -100 if score <= -100 { // FINISH ME // set the variable gameOver to true ??? // FINISH ME // set the scoreLabel.text to "You Lose" ??? // FINISH ME // add 1 to your losses ??? // FINISH ME // set lossesLabel.text to "Losses:\(losses)" ??? timer.invalidate() return } // FINISH ME // check to see if the score is greater than or // equal to 20000 or it's y value is less than 24 if score >= 200000 || ship.frame.origin.y < 24 { // FINISH ME // set the variable gameOver to true ??? // FINISH ME // set the scoreLabel.text to "You Win" ??? // FINISH ME // add 1 to your wins ???? // FINISH ME // set the winsLabel.text to "Wins:\(wins)" ??? // this shuts down the timer timer.invalidate() return } } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // here we get our screenWidth and screenHeight screenWidth = CGFloat(screenSize.width) screenHeight = CGFloat(screenSize.height) // here we get the starting location of our ship shipStartX = ship.frame.origin.x shipStartY = ship.frame.origin.y shipStartWidth = ship.frame.size.width shipStartHeight = ship.frame.size.width } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }