2014年5月24日 星期六

Breaking the source code (Floppy Bird) (Part II)


This article continues the previous one, which broke the source code of Floppy Bird. This part is slightly difficult since it involves more logical flow. It focuses on the moving of the "Pillars" and the detection of death of the "Bird". Of course, we have created the "Bird". Now, just finish the remained code.

Step 1:
Create a class which holds all the property of "pillar".
Basically, it should have x-y-position. My idea is using width of the pillar, hole's space for the bird passing through, x-axis location and the bottom point on the upper pillar.


Step 2:
Writing constructor and functions.
  1. Constructor: initialize the starting point of the pillar. Don't worry about the location, it would be concerned in the main program.
  2. ran_the_gap: randomly generate the location of the hole when it touches x=800.
  3. show_pillar: get the canvas as reference and then draw the pillar at that canvas.
  4. move: of course, x should be added in order to make moving.
  5. detection: if bird's x-location matches the x-location of this pillar, make a deep consideration on their y-location. If they make an impact to each other, return false. You could make your design here. Maybe the hitting causes the death of the "bird" and shows "game over". It just returns true or false in my code while the action is made in the main program.

Step 3:
modify the code in floppy_crack.java. The additional codes or modified codes are highlighted. Highlighted by blue means that they are used to run the "pillar" whereas the orange one indicates the codes for boundary of the "Bird".

Macroscopically, after the game starts, it immediately adds ten pillars in the 1000 pixels far away. For 0.05 second, it make a movement and detection for one time.

Conclusion:
Although the design of this simple project is raw and ugly, it fully shows the elementary structure of those simple games. Assume that my "Upward Bird" and "Downward Bird" could designed better, the "Pillars" could be made by a colorful paint and I could set a wonderful background for the game screen. All of the codes below, which are less than 150 lines, would show you the completed "floppy bird".

Is it worth putting more than a month to break the record in this type of game? Many people waste their time on that game while it is made by less than one hour programming time.

In the next part, I would make an unbelievable design based on this game.

pillar.java:
public class pillar {
     private final float width_of_pillar=50;
     private final float gap_for_life=200;
    
     private float position_y;
     private float position_x;
    
     private Paint paint;
    
     public pillar(float starting_x)
     {
         this.position_x=starting_x;
         this.position_y=100;
        
         paint=new Paint();
         paint.setColor(Color.BLACK);
     }
    
     public void ran_the_gap(float screen_height)
     {
         if(position_x==800)
              this.position_y=(float) (Math.random()*(screen_height-gap_for_life));
     }
    
     public void show_pillar(Canvas canvas_reference)
     {
         canvas_reference.drawRect(position_x, 0, position_x+width_of_pillar, position_y, paint);
         canvas_reference.drawRect(position_x, position_y+gap_for_life, position_x+width_of_pillar, canvas_reference.getHeight(), paint);
     }
    
     public void move()
     {
         position_x-=5;
         if(position_x==-100)
              position_x+=2000;
     }
    
     public boolean detection(float location_bird_x,float location_bird_y,float width_bird,float height_bird)
     {
         if(position_x>location_bird_x-width_of_pillar
                  &&position_x<location_bird_x+width_bird)
         {
              if(location_bird_y>position_y&&location_bird_y+height_bird<position_y+gap_for_life)
                  return true;
              else
                  return false;
         }
         return true;
     }
}

floppy_crack.java:
public class floppy_crack extends View{
    
     private float x=0;
     private float y=0;
     private int speed;
    
     private Paint paint;
     private Bitmap bird_a_upward;
     private Bitmap bird_b_downward;
    
     private long previous;
    
     private pillar resistance[];
    
     public floppy_crack(Context context) {
         super(context);
         y=200;
        
         paint=new Paint();
        
         speed=0;
        
         bird_a_upward=BitmapFactory.decodeResource(getResources(), R.drawable.upward_bird);
         bird_b_downward=BitmapFactory.decodeResource(getResources(), R.drawable.downward_bird);
        
         Date temp=new Date();
         previous=temp.getTime();
        
         resistance=new pillar[10];
         for(int i=0;i<10;i++)
         {
              resistance[i]=new pillar((float)i*200+1000);
         }
     }

     public void upward()
     {
         speed=-20;
     }
    
     @Override
     protected void onDraw(Canvas canvas) {
         // TODO Auto-generated method stub
         super.onDraw(canvas);
        
         Date temp=new Date();
         long now=temp.getTime();
        
         if(now-previous>50)
         {
              previous=now;
              if(y>canvas.getHeight()-bird_b_downward.getHeight()-100)
              {
                  y-=5;
                  speed=-20;
              }
              else
                  if(y<0)
                  {
                       y+=5;
                       speed=20;
                  }
                  else
                       y+=speed;
             
              speed+=5;
             
              boolean move_or_not=true;
              for(int i=0;i<10;i++)
              {   
                  resistance[i].ran_the_gap(canvas.getHeight());
                  if(!resistance[i].detection(x, y, bird_b_downward.getWidth(),bird_b_downward.getHeight()))
                  {
                       move_or_not=false;
                       break;
                  }
              }
              if(move_or_not)
                  for(int i=0;i<10;i++)
                       resistance[i].move();
         }
         if(speed>0)
         {
              canvas.drawBitmap(bird_b_downward, x, y, paint);
         }
         else
         {
              canvas.drawBitmap(bird_a_upward, x, y, paint);
         }
         for(int i=0;i<10;i++)
         {
              resistance[i].show_pillar(canvas);
         }
         invalidate();
     }
}

MainActivity.java: (remain unchanged)
public class MainActivity extends Activity {

     floppy_crack bird;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         bird=new floppy_crack(this);
         setContentView(bird);
     }
     @Override
     public boolean onTouchEvent(MotionEvent event) {
         // TODO Auto-generated method stub
         bird.upward();
         return super.onTouchEvent(event);
     }   
}

沒有留言:

張貼留言