// // ViewController.swift // Frogger3 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) var gameOver = true var carSpeedX = CGFloat(3) var frogSpeedX = CGFloat(3) var frogSpeedY = CGFloat(3) // here we define variables to hold the starting location of our frog // we will assign the correct values in the viewDidLoad() method var frogStartX = CGFloat(0) var frogStartY = CGFloat(0) var frogStartWidth = CGFloat(0) var frogStartHeight = CGFloat(0) // here we define variables to hold the starting location of our first car // we will assign the correct values in the viewDidLoad() method var carLeftStartX = CGFloat(0) var carLeftStartY = CGFloat(0) var carLeftStartWidth = CGFloat(0) var carLeftStartHeight = CGFloat(0) // FINISH ME // create a variable called wins and assign // it a value of 0 var ??? = ? // FINISH ME // create a variable called losses and assign // it a value of 0 var losses ? ? // this variable determines which direction // the frog moves in // "NONE", "UP", "RIGHT", or "LEFT" var directionOfFrog = "NONE" // FINISH ME // connect your winsLabel @IBOutlet weak var winsLabel: UILabel! // FINISH ME // connect your lossesLabel @IBOutlet weak var lossesLabel: UILabel! // FINISH ME // connect your title label @IBOutlet weak var frogTitle: UILabel! // FINISH ME // connect your frog @IBOutlet weak var frog: UIImageView! // FINISH ME // connect your road @IBOutlet weak var road: UIImageView! // FINISH ME // connect your first car that is moving left @IBOutlet weak var carLeft1: UIImageView! // FINISH ME // connect your second car that is moving left @IBOutlet weak var carLeft2: UIImageView! // FINISH ME // connect your third car that is moving left @IBOutlet weak var carLeft3: UIImageView! // FINISH ME // connect your first car that is moving right @IBOutlet weak var carRight1: UIImageView! // FINISH ME // connect your second car that is moving right @IBOutlet weak var carRight2: UIImageView! // will hold [carLeft1, carLeft2, carLeft3] // here we create a list that can hold UIImageView objects var leftCar = [UIImageView]() // will hold [carRight1, carRight2] // here we create a list that can hold UIImageView objects var rightCar = [UIImageView]() func moveCarRight(_ car:UIImageView) { var x = car.frame.origin.x // FINISH ME // add carSpeedX to your x value x = ? + ????? if x > screenWidth + 200 { let temp = Int (arc4random_uniform(100)+250) x = CGFloat(-temp) } let y = car.frame.origin.y let width = car.frame.size.width let height = car.frame.size.height // this resets the frame of the UIImageView object car.frame = CGRect(x: x, y: y, width: width, height: height) } func moveCarLeft(_ car:UIImageView) { var x = car.frame.origin.x // FINISH ME // subtract carSpeedX from x x = ? - ?????? // FINISH ME // check and see if x is less than -300 if ? ? ??? { // FINISH ME // assign screenWidth plus 350 to x x = ????? + ?? } // FINISH ME // get the y value of your car let y = car.frame.?.? let width = car.frame.size.width let height = car.frame.size.height // this resets the frame of the UIImageView object car.frame = CGRect(x: x, y: y, width: width, height: height) } func setCar1StartingPosition(_ car:UIImageView) { var x = car.frame.origin.x // FINISH ME // subtract 100 from your x value x = ? - ????? // FINISH ME // get the y value of your car let y = car.frame.?.? let width = car.frame.size.width let height = car.frame.size.height // this resets the frame of the UIImageView object car.frame = CGRect(x: x, y: y, width: width, height: height) } func setCar3StartingPosition(_ car:UIImageView) { var x = car.frame.origin.x // FINISH ME // set to screenWidth - 50 x = ??? - ?? // FINISH ME // get the y value of your car let y = car.frame.?.? let width = car.frame.size.width let height = car.frame.size.height // this resets the frame of the UIImageView object car.frame = CGRect(x: x, y: y, width: width, height: height) } func moveFrogUp() { // this gets the x value of the UIImageView let x = frog.frame.origin.x // FINISH ME // get the frog's y value var y = ???? // FINISH ME // check to see if y is greater than 20 ??????? { // FINISH ME // decrease y by frogSpeedY y = ?????? } // this gets the width and height of the UIImageView let width = frog.frame.size.width let height = frog.frame.size.height // this resets the frame of the UIImageView object frog.frame = CGRect(x: x, y: y, width: width, height: height) } func moveFrogLeft() { // this gets the x value of the UIImageView var x = frog.frame.origin.x // FINISH ME // get the frog's y value let y = ????? // FINISH ME // check to see if x is greater than 4 ???? { // FINISH ME // decrease x by frogSpeedx x = ????? } // this gets the width and height of the UIImageView let width = frog.frame.size.width let height = frog.frame.size.height // this resets the frame of the UIImageView object frog.frame = CGRect(x: x, y: y, width: width, height: height) } func moveFrogRight() { // this gets the x value of the frog's UIImageView var x = frog.frame.origin.x // FINISH ME // get the frog's y value let y = ???? // FINISH ME // check to see if x is less than screenWidth ?????? { // FINISH ME // increase x by frogSpeedX x = ????? } // this gets the width and height of the UIImageView let width = frog.frame.size.width let height = frog.frame.size.height // this resets the frame of the UIImageView object frog.frame = CGRect(x: x, y: y, width: width, height: height) } @IBAction func moveUpButtonEvent(_ sender: UIButton) { // FINISH ME // move the frog up (call a method) ?????? // FINISH ME // set directionOfFrog to "UP" directionOfFrog = ???? } @IBAction func moveLeftButtonEvent(_ sender: UIButton) { // FINISH ME // move the frog left (call a method) ?????? // FINISH ME // set directionOfFrog to "LEFT" directionOfFrog = ????? } @IBAction func moveRightButtonEvent(_ sender: UIButton) { // FINISH ME // move the frog right (call a method) ?????? // FINISH ME // set directionOfFrog to "RIGHT" directionOfFrog = ???? } @IBAction func startButtonEvent(_ sender: UIButton) { gameOver = false // FINISH ME // set directionOfFrog to "NONE" directionOfFrog = ???? setFrogStartingPosition() // reset the cars????? It's up to you timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true) } @IBAction func stopButtonEvent(_ sender: Any) { timer.invalidate() // FINISH ME // set gameOver to true gameOver = ???? // FINISH ME // set directionOfFrog to "NONE" directionOfFrog = ????? // FINISH ME // call the method setFrogStartingPosition() ????? } // ************************************** // ************************************** // ************************************** // main event loop (timer) // ************************************** // ************************************** // ************************************** @objc func timerAction() { // here we check to see if the gameOver variable // is true if gameOver ?? ???? { return // exits (quits) the function } if directionOfFrog == "NONE" { // do nothing } else if directionOfFrog == "UP" { // FINISH ME // call a method ?????? } else if directionOfFrog == "LEFT" { // FINISH ME // call a method ?????? } else if directionOfFrog == "RIGHT" { // FINISH ME // call a method ??????? } // for each car in the array rightCar for car in rightCar { moveCarRight(car) } // for each car in the array leftCar for car in leftCar { moveCarLeft(car) } // now we look for collisions // for each car in the array rightCar for car in rightCar { // FINISH ME // see if the frog's frame intersects the car's frame if frog.frame.??????????(car.frame) { // FINISH ME // set gameOver to true gameOver = ????? // FINISH ME // add 1 to your losses variable losses = ?????? // FINISH ME // set the text of the lossesLabel to "Losses: \(losses)" lossesLabel.text = ???????? timer.invalidate() return } } // for each car in the array leftCar for car in leftCar { if frog.frame.intersects(car.frame) { // FINISH ME // set gameOver to true gameOver = ????? // FINISH ME // add 1 to your losses variable losses = ?????? // FINISH ME // set the text of the lossesLabel to "Losses: \(losses)" lossesLabel.text = ?????? timer.invalidate() return } } // see if we got across the street if frog.frame.origin.y + frog.frame.size.height < road.frame.origin.y { // FINISH ME // set gameOver to true gameOver = ????? // FINISH ME // add 1 to your wins variable wins = ?????? // FINISH ME // set the text of the winsLabel to "Losses: \(losses)" winsLabel.text = ?????? timer.invalidate() return } } func setCarLeftYValue(_ car:UIImageView) { let x = car.frame.origin.x // FINISH ME // get the y value of the car var y = car.frame.????? let roadY = road.frame.origin.y y = roadY + 20 let width = car.frame.size.width let height = car.frame.size.height // this resets the frame of the UIImageView object car.frame = CGRect(x: x, y: y, width: width, height: height) } func setCarRightYValue(_ car:UIImageView) { let x = car.frame.origin.x let width = car.frame.size.width let height = car.frame.size.height let roadHeight = road.frame.size.height let y = road.frame.origin.y + roadHeight/2 + 20 // this resets the frame of the UIImageView object car.frame = CGRect(x: x, y: y, width: width, height: height) } func setFrogStartingPosition() { // FINISH ME // set x to frogStartX let x = ????? // FINISH ME // set y to frogStartY let y = ?????? // FINISH ME // set width to frogStartWidth let width = ??????? // FINISH ME // set height to frogStartHeight let height = ??????? // this resets the frame of the frog's UIImageView object frog.frame = CGRect(x: x, y: y, width: width, height: height) } func attachRoadToScreenEdges() { let x = CGFloat(0) let y = road.frame.origin.y let width = CGFloat(screenWidth) let height = road.frame.size.height // this resets the frame of the UIImageView object road.frame = CGRect(x: x, y: y, width: width, height: height) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. screenWidth = CGFloat(screenSize.width) screenHeight = CGFloat(screenSize.height) // here we get the starting position of the frog frogStartX = frog.frame.origin.x frogStartY = frog.frame.origin.y frogStartWidth = frog.frame.size.width frogStartHeight = frog.frame.size.height // here we get the starting position of the left car carLeftStartX = carLeft1.frame.origin.x - 100 carLeftStartY = frog.frame.origin.y carLeftStartWidth = frog.frame.size.width carLeftStartWidth = frog.frame.size.width setCar1StartingPosition(carLeft1) setCar3StartingPosition(carLeft3) attachRoadToScreenEdges() setCarLeftYValue(carLeft1) // FINISH ME // append(carLeft1) to the leftCar array leftCar.??????? setCarLeftYValue(carLeft2) // FINISH ME // append(carLeft2) to the leftCar array leftCar.??????? setCarLeftYValue(carLeft3) // FINISH ME // append(carLeft3) to the leftCar array leftCar.append(carLeft3) rightCar.append(carRight1) rightCar.append(carRight2) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }