2014年5月23日 星期五

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


This article is to crack the source code of Floppy Bird, Flappy Bird or another game with the same type. This type of game is extremely easy to write. Why not to make one?

Step 1:
Start up the Eclipse and then create a project for “Floppy Crack”.
Prepare two pictures for the “Bird”: one is “Upward Bird”; another one is “Downward Bird”.


Step 2:
The customized view “floppy_crack” would be created in the next step. Before doing that, we need to set “floppy_crack” to the content view such that your screen make a connection to what you create.

Key code in MainActivity.java:
         bird=new floppy_crack(this);
         setContentView(bird);

Step 3:
Create a class “floppy_crack”, which extends “View”. Define three important object members: x, y and speed. ‘x’ and ‘y’ mean the location of the “Bird” in the x-y-axis. “speed” is to control speed of the “Bird”. Since there is gravity in the real world, there is acceleration or deceleration applied to the “Bird” in the sky.

Of course, we also need to define the “Paint” and the two “Bitmap” such that the “Bird” could be shown. Also, make the override method “onDraw” to draw your “Bird”.

Key code in the constructor of the new class “floppy_crack”:
bird_a_downward=BitmapFactory.decodeResource(getResources(), R.drawable.downward_bird);
bird_b_upward=BitmapFactory.decodeResource(getResources(), R.drawable.upward_bird);
That is to decode the pictures of “Upward Bird” and “Downward Bird”.


Key code in the “onDraw” of the class “floppy_crack”:
if(speed>0)
              canvas.drawBitmap(bird_a_downward, x, y, paint);
         else
              canvas.drawBitmap(bird_b_upward, x, y, paint);
         invalidate();
“speed = 0” means that it reaches the maximum height. “speed > 0” means that it goes downward. “speed < 0” means that it goes upward. Moreover, “invalidate()” is essential for updating the location of the “Bird”.

Step 4:
Detect the click of player. One click would order the “Bird” jump for one times.

Key code in MainActivity.java:
                @Override
          public boolean onTouchEvent(MotionEvent event) {
              // TODO Auto-generated method stub
              bird.upward();
              return super.onTouchEvent(event);
          }

Key code in floppy_crack.java:
                public void upward()
          {
               speed=-20;
          }

Step 5:
Without the time control, it may be fine in the emulator. But the speed of the “Bird” would be very fast in the screen of your real phone. Use of “Date” is a simple method to implement time control.

Key code in floppy_crack.java:
         temp=new Date();
          long now=temp.getTime();
        
         if(now-previous>50)
         {
              previous=now;
             
              y+=speed;
              speed+=5;
         }
When there is over than 0.05 second, move the “Bird” for one time.

Macroscopically, it is extremely easy to write this type of game. You could use less than 50 lines to activate the “Bird”. Why not to make one?

If you are interested, see the next article, which continues this topic.


Source code of MainActivity:

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) {
         bird.upward();
         return super.onTouchEvent(event);
     }   
}

Source code of floppy_crack:

public class floppy_crack extends View{
     private final float x=20;
     private float y
     private Paint paint;
     private int speed;
     private Bitmap bird_a_downward;
     private Bitmap bird_b_upward;
     private long previous;
     public floppy_crack(Context context) {
         super(context);
         y=200;
         paint=new Paint();
         paint.setColor(Color.BLUE);
         speed=0;
         bird_a_downward=BitmapFactory.decodeResource(getResources(), R.drawable.downward_bird);
         bird_b_upward=BitmapFactory.decodeResource(getResources(), R.drawable.upward_bird);
         Date temp=new Date();
         long now=temp.getTime();
         previous=now;
     }
     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;
              y+=speed;
              speed+=5;
         }
         if(speed>0)
              canvas.drawBitmap(bird_a_downward, x, y, paint);
         else
              canvas.drawBitmap(bird_b_upward, x, y, paint);
         invalidate();
     }
}


Tips: Ctrl + Alt + O is a shortcut to import something automatically.

沒有留言:

張貼留言