openobject.org

Bird Landing6

From Physical Programming

Media:BirdLand6.zip

Coding

When the program runs the code, it would know what are the code refer to:

  • int - is for integer
  • int frame - having first frame 0
  • int frame2 - (We make this just to random the frame for new bird to appear)
  • int numFrames - use for images of the bird (3 images facing left and another 3 facing right)
  • int countLandingBird - use to count how many birds stop at the tree, first it starts from zero
  • int maximumBird - use for how many birds can be on the tree
  • PImage[] - to create an image object (which would be loaded further in the code)
  • new PImage - to call out new images
  • [numFrames] - (parameter to 3 images)
  • int i - refer to it as a integer no.
  • BirdList birdList - we create a method called birdList, (which its function would be further in the code)
int frame = 0;
int frame2 = 0;
int numFrames = 3;
int countLandingBird = 0;
int maximumBird = 15;
PImage[] images1 = new PImage[numFrames];
PImage[] images2 = new PImage[numFrames];
PImage bg;
int i;
BirdList birdList;


  • void setup() - to tell variables for the environment
void setup()
{
  frameRate(25);         //number of frame per second
  size(663,500);         //size of screen (x,y) pixel
  bg = loadImage("Black_Tree.gif");       //load images
  images1[0] = loadImage("0001.gif");  //images should be in the file 
  images1[1] = loadImage("0002.gif");  //called "data" in current sketch
  images1[2] = loadImage("0003.gif");
  images2[0] = loadImage("0011.gif");  //there are 2 collections of images (images1 & images2)
  images2[1] = loadImage("0021.gif");  //images1 are images of bird facing right
  images2[2] = loadImage("0031.gif");  //images2 are images of bird facing left
  birdList = new BirdList();  //call out new class called Birdlist
  for(i=0;i<15;i++)       //add new bird 15 times 
  {                       //(in another word there will be 15 birds at the start)
    birdList.addBird(new Bird(random(0,663),random(0,500),int(random(0,2))));
  }                       //random the bird anywhere in the screen, using any frame of the bird
  
}


  • void mousePressed() - when mouse is press
void mousePressed()        
{                                                     //there will be one new bird 
  birdList.addBird(new Bird(mouseX,mouseY,frame));   //at the mouse press position
}


  • void draw() - function drawing, working frame by frame, keeps on looping til program is stopped.
void draw(){
 
  background(255,255,255); //colour of the background (R,G,B)
  image(bg,0,200);         //putting image called bg at 0,200 (x,y)
  birdList.run();          // we make birdList as a method above. Now asking it to run
  frame = (frame+1)%numFrames;     //calling out frame 0, 1, 2 (continuously in order)
  frame2 = (frame2+1)%200;         //(same as above) frame from 0 - 199
  if(frame2 == int(random(0,100)))    
                                    //if frame2 (above) is equal to the random number from 0-100
    birdList.addBird(new Bird(random(-30,-20),random(0,400),frame));    
                                    //add new bird random offscreen of left side
  if(frame2 == int(random(101,200)))
                                    //same as above, but random offscreen of right side
    birdList.addBird(new Bird(random(670,700),random(0,400),frame));
  println(birdList.list.size());       //show no. of birds in the whole animation
  println(countLandingBird);      //show no. of birds on the tree
}


There are two class: BirdList and Bird. BirdList is for controlling the bird, while Bird is for giving variables to the coding in BirdList.

class BirdList
{
  ArrayList list;  //to manage list of birds (to make them function individually)
  
  BirdList()
  {
    list = new ArrayList();   // list is a new set of ArrayList
  }
 
  void addBird(Bird b)    //telling that "addBird" is to add 1 more "list" 
  {                                      //(which "list" is a new set of "ArrayList")
    list.add(b);
  }


This is really messy. Try to read it line by line; might help.

  void run()
  {
    for (int i = 0; i < list.size(); i++)  //it would do these(below) to every lists of bird
    {
      Bird b = (Bird) list.get(i); //take a list of bird and do it individually
      if(b.originX <= 230)  //b.originX is the x position of bird when it first run
      {                //if b.originX is less than 230, which is the left of the tree
        if(b.landPCT >= 1)   //b.landPCT is taken from the equation of drawing the curve for bird to land
        {               //b.landPCT equal to 1 when it stop at the tree
          if(b.action.equals("Landing") && countLandingBird >= maximumBird)  
          {             //if its action is stated to be "Landing" already and there are more than 15 birds
            b.action = "flyingOut";  //make its action to flyingOut
          }
          else if(b.action.equals("")) //if there is not statement for its action
          {
            b.action = "Landing";     //state that it is "Landing" and add one to countLandingBird
            countLandingBird++;
          }
          if(countLandingBird < maximumBird && !b.action.equals("flyingOut")) 
          {                    //if there is lesser than 15 birds and its action is not "flyingOut"
            image(images1[2], b.endX, b.endY);  //use an image "images1[2]"
            b.age++;                         //add1 to b.age (to count how long is the bird stay on the tree)
          }
          else if(b.action.equals("flyingOut"))  //if it is "flyingOut"
          {
            if(b.flyPCT == -0.01)        //b.flyPCT is for the bird going to fly off the tree
            {
              b.landOriginX = b.endX;           //create a new name for its x and y
              b.landOriginY = b.endY;           //b.end is b.landOrigin
              b.endX = random(430,500);    //random a destination to fly out                      
              b.endY = random(20,150);
              b.distX = b.endX - b.landOriginX;
              b.distY = b.endY - b.landOriginY;
              b.flyPCT = b.flyPCT + 0.01;    //add 0.01 to make its PCT a 0
              b.action = "flyingOut";             //state its action as "flyingOut"
              countLandingBird--;                //deduct one countLandingBird
            }  
            else if(b.flyPCT >= 0 && b.flyPCT < 1)  //if the bird is flying use this path
            {
              b.currentX = b.landOriginX + (b.flyPCT * b.distX);  
              b.currentY = b.landOriginY + (pow(b.flyPCT, b.exponent) * b.distY);
              b.flyPCT += b.step;
              image(images1[(b.birthframe+frame)%numFrames], b.currentX, b.currentY); 
            }
            else if(b.flyPCT >= 1)  //if the bird reach its destination
            {
              b.enterZone = false;  //state that it is not in the tree zone
              b.age = 0;                    //state its age on the tree as zero
              b.landPCT = 0;           //make its landPCT zero
            }
          }
        }
        else
        {
          image(images1[(b.birthframe+frame)%numFrames], b.currentX, b.currentY);
                     // continue flying
          if(((b.currentX > 40 && b.currentX < 420 && b.currentY > 150 && b.currentY < 410) || b.enterZone == true || b.age >= 750) && !b.action.equals("flyingOut"))
          {         //(if it is in the enterzone or if it hasn't land for 750 frame) and it is not in the process of flying out
            if(b.enterZone == false)  //if its enterZone is false 
            {
              b.landOriginX = b.currentX;             
              b.landOriginY = b.currentY;
              b.endX = random(b.currentX,420);   //random its landing destination
              b.endY = random(abs(sqrt(sq(190)-sq(b.endX-230))-410),390); //random in tree area
              b.distX = b.endX - b.landOriginX;
              b.distY = b.endY - b.landOriginY;
              b.enterZone = true;  //make enterZone true
            }
            else  //if b.enterZone is true, appear in this curve path
            {
              b.currentX = b.landOriginX + (b.landPCT * b.distX);
              b.currentY = b.landOriginY + (pow(b.landPCT, b.exponent) * b.distY);
              b.landPCT += b.step; //add landPCT 
            }
          }
          else if(b.enterZone == false)  //if b.enterZone is false
          {
            if(b.currentX > 700)  //if it is off the screen (right side)
            {
              if(b.action.equals(""))  //if there is no action
              {
                b.currentX = -30;  //make it reappear in (left side)
                b.currentY += int(random(-3, 3));  //add current y position (random from -3 to 3)
              }
              else if(b.action.equals("flyingOut")) // if the action is flyOut
              {
                list.remove(i); // remove it from the list
              }
            }
            else //if it is not off the screen, continue flying forward
            {  
              b.currentX += 2;  
              b.currentY += int(random(-3, 3));
            }       
            b.age+=1;  //add its age for flying
          }
       }
    }
      else if(b.originX > 230) // these (below) is just another set of action for bird from right side
      {
         if(b.landPCT >= 1)
         {
          if(b.action.equals("Landing") && countLandingBird >= maximumBird)
          {
            b.action = "flyingOut";
          }
          else if(b.action.equals(""))
          {
            b.action = "Landing";
            countLandingBird++;
          }
          if(countLandingBird < maximumBird && !b.action.equals("flyingOut"))  
          {  
            image(images2[2], b.endX, b.endY);
            b.age++;
          }
          else if(b.action.equals("flyingOut"))
          {
            if(b.flyPCT == -0.01)
            {
              b.landOriginX = b.endX;
              b.landOriginY = b.endY;
              b.endX = random(0,30);
              b.endY = random(20,150);
              b.distX = b.endX - b.landOriginX;
              b.distY = b.endY - b.landOriginY;
              b.flyPCT = b.flyPCT + 0.01;
              b.action = "flyingOut";
              countLandingBird--;
            }  
            else if(b.flyPCT >= 0 && b.flyPCT < 1)
            {
              b.currentX = b.landOriginX + (b.flyPCT * b.distX);
              b.currentY = b.landOriginY + (pow(b.flyPCT, b.exponent) * b.distY);
              b.flyPCT += b.step;
              image(images2[(b.birthframe+frame)%numFrames], b.currentX, b.currentY);
            }
            else if(b.flyPCT >= 1)
            {
              b.enterZone = false;
              b.age = 0;
              b.landPCT = 0;
            }
          }
        }
          //image(images2[2], b.endX, b.endY);  
         else
         {
          image(images2[(b.birthframe+frame)%numFrames], b.currentX, b.currentY);
          if(((b.currentX > 40 && b.currentX < 420 && b.currentY > 150 && b.currentY < 410) || b.enterZone == true || b.age >= 750) && !b.action.equals("flyingOut"))
          {
            if(b.enterZone == false)
            {
              b.landOriginX = b.currentX;
              b.landOriginY = b.currentY;
              b.endX = random(40,b.currentX);
              b.endY = random(abs(sqrt(sq(190)-sq(b.endX-230))-410),390);
              b.distX = b.endX - b.landOriginX;
              b.distY = b.endY - b.landOriginY;
              b.enterZone = true;
            }
            else
            {
              b.currentX = (b.landOriginX + (b.landPCT * b.distX));
              b.currentY = (b.landOriginY + (pow(b.landPCT, b.exponent) * b.distY));
              b.landPCT += b.step;           
            }
          }
          else if(b.enterZone == false)
          {
            if(b.currentX < -30)
            {
              if(b.action.equals(""))
              {
                b.currentX = 700;
                b.currentY += int(random(-3, 3));
              }
              else if(b.action.equals("flyingOut"))
              {
                list.remove(i);
              }
            } 
            else
            {   
              b.currentX -= 2;
              b.currentY += int(random(-3, 3));
            }
            b.age+=1;
          }         
        }
      }
    }
  }
}


  • float - datatype for a decimal point number
  • boolean - datatype for only true or flase
  • String - a method to state what it is as a quote (using for comparing or searching)
class Bird
{
  int age = 0;         // counting time for its action (on tree and flying) 
  float originX = 0;          //its position when it first runs
  float originY = 0;
  float landOriginX = 0;  //its position when it is landing
  float landOriginY = 0;  
  float currentX = 0;     //its current position
  float currentY = 0;     
  int birthframe = 0;     //its first frame when it runs
  float endX = 0;          //its destination
  float endY = 0;
  float distX = 0;  
  float distY = 0;  
  float exponent = 4;   
  float step = 0.01;     //for steps of PCT
  float landPCT = 0;    //PCT >0 is a start of going to the destination
                             //PCT=1 is when it reaches the destination
  float flyPCT = -0.01;
  boolean enterZone = false; 
  String action = "";
 
  Bird(float originX, float originY,int birthframe)
  {
    this.originX = originX;
    this.originY = originY;
    this.birthframe = birthframe;
    this.currentX = originX;
    this.currentY = originY;
  }
 
}