logo
TOOLS TO HELP YOU LEARN
teachers, students, everyone
Tools | Blog | Account | Learn
Login Status

Flappy Bird Clone Swift 3
How to Make a Flappy Birds Clone using Xcode and Swift 3
Jan 17, 2017

    1. Create a new single view project. Call it Flappy Bird Clone.
    2. Download the following images and then drag them into your project (below the ViewController.swift). Check the Copy Items if needed check box that popsup when you drop the image into your project.
      Bird Image | Tube Image | Sky image
    3. Add following code to the ViewDidLoad method. This will add a background
      self.view.backgroundColor = UIColor(patternImage: UIImage(named: "sky.png")!)     
    4. Run the code and you should see a background of the sky.png image
    5. Go to the Main.Storyboard and add two images to the view (bird and tube). Create outlets by dragging this imageViews to the ViewController.swift in split screen mode. Call the bird outlet "bird" and the tube "tube"
    6. Add the following variables to your code. Paste them near the top of the page under the "class ViewController: UIViewController{" line
      var birdTimer = Timer()
      var goUp=false
      var counter=0
      var randY=Int()
      var gap=650;

      var screenWidth:CGFloat=0.0;
      var screenHeight:CGFloat=0.0;
      var tTubeX=CGFloat()
      var tTubeY=CGFloat() var gameOn = false;
      var birdY=CGFloat()
    7. Add the following method to your code. This detects a touch on the screen. The first touch starts the NSTimer which starts the game.
                      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
      if let touch = touches.first {
      let position :CGPoint = touch.location(in: view)
      if (gameOn == false){
      gameOn = true;
      birdTimer = Timer.scheduledTimer(timeInterval: 0.04, target:self, selector: #selector(ViewController.moveBird), userInfo: nil, repeats: true)

      } else {
      print("touch")
      birdY=birdY-10
      goUp=true

      }
      }
      }
    8. Add this line to the viewDidLoad method - "sizeObjects()"
    9. Add sizeObject method to the class. This function gets the screen width and height and placing things on the view.
                      func sizeObjects(){

      randY = Int(arc4random_uniform(UInt32(Int(screenHeight))));
      //randY=(-randY)


      let screenSize: CGRect = UIScreen.main.bounds
      screenWidth = screenSize.width;
      screenHeight = screenSize.height;

      tTubeX=screenWidth-50;

      tube.frame.size=CGSize(width: 30, height: 100)
      tube.frame.origin = CGPoint(x: screenWidth-50, y: CGFloat(randY))

      self.view.addSubview(tube)//you may need to add outlet to view again.

      birdY=150

      bird.frame.size=CGSize(width: 55, height: 55)
      bird.frame.origin = CGPoint(x: 30, y: birdY )
      self.view.addSubview(bird)//you may need to add outlet to view again.
      }
    10. The moveBird method moves the bird according to your touch. It also includes the action needed to move the tubes.
                        func moveBird(){

      if (goUp==false){
      birdY=birdY+5;
      }else{
      counter += 1
      if(counter>6){
      goUp=false
      counter=0
      }else{
      birdY=birdY-5;
      }
      }
      self.bird.center=CGPoint(x: 50,y: birdY);
      //will fill this in with code to move the tubes
      if(tTubeX<(-10)){
      tTubeX=screenWidth;
      randY = Int(arc4random_uniform(UInt32(Int(screenHeight))));
      //randY=(-randY)
      print(randY);
      }
      tTubeX=tTubeX-5
      tube.frame.origin = CGPoint(x: tTubeX, y: CGFloat(randY))
      self.view.addSubview(tube)//you may need to add outlet to view again.

      //check to bird crashing into tube
      if(bird.frame.intersects(tube.frame)){
      print("crash")
      gameOver()
      }
      }
    11. GameOver method stops motion on the screen
    func gameOver(){

    birdTimer.invalidate()
    }


    Comments:




    No comments posted

blog

Past Posts

Dropbox Public Folder Alternative
Jun 4, 2017

Getting Started with NodeJS
May 1, 2017

Scratch Intro: Racing Game
Jan 15, 2017

Five Best Educational Tools For Teachers
Feb 3, 2017

Coding Gravity
Feb 22, 2017

Drones in Eduction
March 2, 2017

Javascript Intro
April 1, 2017

iPhone Streaming Audio App
April 15, 2017

Installing MySQL
May 15, 2017

 

 


 

Thank You!