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. //
      //  ViewController.swift
      //  FlappyBirds
      //
      //  Created by Matt Memmo on 7/15/15.
      //  Copyright (c) 2015 RWS. All rights reserved.
      //
      
      import UIKit
      
      class ViewController: UIViewController {
       
          //1) place this code under class declaration (class variables)
          var tubeTimer = Timer()
          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 birdY=CGFloat()
         
          
          
          
          @IBOutlet weak var tube: UIImageView!
          
          
          
          
          @IBOutlet weak var bird: UIImageView!
          
          
        
          
          
        
          
          
          
          
          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.
          }
          
        
          
          
          
          override func viewDidLoad() {
              super.viewDidLoad()
              // Do any additional setup after loading the view, typically from a nib.
              self.view.backgroundColor = UIColor(patternImage: UIImage(named: "sky.png")!)
              sizeObjects()
              moveThings()
          }//end of viewdid
      
          override func didReceiveMemoryWarning() {
              super.didReceiveMemoryWarning()
              // Dispose of any resources that can be recreated.
          }
      
          
          //2) call this method in start Game IBAction
          func moveThings(){
              tubeTimer = Timer.scheduledTimer(timeInterval: 0.06, target:self, selector: #selector(ViewController.moveTubes), userInfo: nil, repeats: true)
              
              birdTimer = Timer.scheduledTimer(timeInterval: 0.04, target:self, selector: #selector(ViewController.moveBird), userInfo: nil, repeats: true)
          }
          
          func moveTubes(){
              //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()
              }
              
          }
          
          func moveBird(){
              //will fill this in with code to move the bird
              //will fill this in with code to move the bird
              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);
          }
          
          
          override func touchesBegan(_ touches: Set, with event: UIEvent?) {
              if let touch = touches.first {
                  let position :CGPoint = touch.location(in: view)
                  print(position.x)
                  print(position.y)
                  print("touch")
                  birdY=birdY-10
                  goUp=true
      
                  
              }
          }
      
          
          
          
          func gameOver(){
              tubeTimer.invalidate()
              birdTimer.invalidate()
          }
      
      }//end of class
      
      
                          


    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!