[Android game development 4] Android game framework (a demo of game role walking on the screen)

Source: Internet
Author: User

 

In fact, the previous article on surfaceview analysis is a simple game framework. Of course, I would like to emphasize that it is a simple game framework, so do not let the experts not spray it out ~

 

This Demo is a demo of image operations and button processing and a simple game framework for a group of kids shoes. We will share it with you here ~

 

Package com. himi;

Import android. content. Context;

Import android. content. res. Resources;

Import android. graphics. Bitmap;

Import android. graphics. BitmapFactory;

Import android. graphics. Canvas;

Import android. graphics. Color;

Import android. graphics. Paint;

Import android. util. Log;

Import android. view. KeyEvent;

Import android. view. SurfaceHolder;

Import android. view. SurfaceView;

Import android. view. SurfaceHolder. Callback;

Public class MySurfaceView extends SurfaceView implements Callback, Runnable {

Private Thread th = new Thread (this );

Private SurfaceHolder sfh;

Private int SH, SW;

Private Canvas canvas;

Private Paint p;

Private Paint p2;

Private Resources res;

Private Bitmap bmp;

Private int BMP _x = 100, BMP _y = 100;

Private boolean UP, DOWN, LEFT, RIGHT;

Private int animation_up [] = {3, 4, 5 };

Private int animation_down [] = {0, 1, 2 };

Private int animation_left [] = {6, 7, 8 };

Private int animation_right [] = {9, 10, 11 };

Private int animation_init [] = animation_down;

Private int frame_count;

Public MySurfaceView (Context context ){

Super (context );

This. setKeepScreenOn (true );

Res = this. getResources ();

Bmp = BitmapFactory. decodeResource (res, R. drawable. enemy1 );

Sfh = this. getHolder ();

Sfh. addCallback (this );

P = new Paint ();

P. setColor (Color. YELLOW );

P2 = new Paint ();

P2.setColor (Color. RED );

P. setAntiAlias (true );

SetFocusable (true); // Note 1

}

Public void surfaceCreated (SurfaceHolder holder ){

SH = this. getHeight ();

SW = this. getWidth ();

Th. start ();

}

Public void draw (){

Canvas = sfh. lockCanvas ();

Canvas. drawRect (0, 0, SW, SH, p); // Note 2

Canvas. save (); // Note 3

Canvas. drawText ("Himi", bmp_x-2, bmp_y-10, p2 );

Canvas. clipRect (bmp _x, bmp _y, bmp _x + bmp. getWidth ()/13, bmp _y + bmp. getHeight ());

If (animation_init = animation_up ){

Canvas. drawBitmap (bmp, bmp _x-animation_up [frame_count] * (bmp. getWidth ()/13), bmp _y, p );

} Else if (animation_init = animation_down ){

Canvas. drawBitmap (bmp, bmp _x-animation_down [frame_count] * (bmp. getWidth ()/13), bmp _y, p );

} Else if (animation_init = animation_left ){

Canvas. drawBitmap (bmp, bmp _x-animation_left [frame_count] * (bmp. getWidth ()/13), bmp _y, p );

} Else if (animation_init = animation_right ){

Canvas. drawBitmap (bmp, bmp _x-animation_right [frame_count] * (bmp. getWidth ()/13), bmp _y, p );

}

Canvas. restore (); // Note 3

Sfh. unlockCanvasAndPost (canvas );

}

Public void cycle (){

If (DOWN ){

BMP _y + = 5;

} Else if (UP ){

BMP _y-= 5;

} Else if (LEFT ){

BMP _x-= 5;

} Else if (RIGHT ){

BMP _x + = 5;

}

If (DOWN | UP | LEFT | RIGHT ){

If (frame_count <2 ){

Frame_count ++;

} Else {

Frame_count = 0;

}

}

If (DOWN = false & UP = false & LEFT = false & RIGHT = false ){

Frame_count = 0;

}

}

@ Override

Public boolean onKeyDown (int key, KeyEvent event ){

If (key = KeyEvent. KEYCODE_DPAD_UP ){

If (UP = false ){

Animation_init = animation_up;

}

UP = true;

} Else if (key = KeyEvent. KEYCODE_DPAD_DOWN ){

If (DOWN = false ){

Animation_init = animation_down;

}

DOWN = true;

} Else if (key = KeyEvent. KEYCODE_DPAD_LEFT ){

If (LEFT = false ){

Animation_init = animation_left;

}

LEFT = true;

} Else if (key = KeyEvent. KEYCODE_DPAD_RIGHT ){

If (RIGHT = false ){

Animation_init = animation_right;

}

RIGHT = true;

}

Return super. onKeyDown (key, event );

}

/* (Non-Javadoc)

* @ See android. view. View # onKeyUp (int, android. view. KeyEvent)

*/

@ Override

Public boolean onKeyUp (int keyCode, KeyEvent event ){

If (DOWN ){

DOWN = false;

} Else if (UP ){

UP = false;

} Else if (LEFT ){

LEFT = false;

} Else if (RIGHT ){

RIGHT = false;

}

Return super. onKeyUp (keyCode, event );

}

@ Override

Public void run (){

// TODO Auto-generated method stub

While (true ){

Draw ();

Cycle ();

Try {

Thread. sleep (100 );

} Catch (Exception ex ){

}

}

}

@ Override

Public void surfaceChanged (SurfaceHolder holder, int format, int width, int height ){

// TODO Auto-generated method stub

}

@ Override

Public void surfaceDestroyed (SurfaceHolder holder ){

// TODO Auto-generated method stub

}

}

 

Note 1

 

This method is used to bind buttons. Different from inheriting views, this method can be used to automatically respond to events without binding keys if you are inheriting views.

 

Note 2

 

Screen swiping is also performed on the screen. In fact, this is only one kind. I also used the drawRGB method in the previous article. Of course, I can also use fillRect to screen swiping.

 

So here I want to talk about it. In the inherited view, the onDraw method is automatically called by the system. Unlike surfaceview, The onDraw method is called continuously in the run, in view, we can use the invalidate ()/postInvalidate () Methods to call the onDraw method. This is also different from surfaceview!

 

Note 3

 

Canvas. save (); and canvas. restore (); match each other to save the canvas state and retrieve the Saved state. Here, I will explain a little bit,

 

When we rotate, zoom, and pan the canvas, we actually want to operate on specific elements, such as slices and rectangles, but when you use the canvas method to perform these operations, the entire canvas is actually operated, and then the elements on the canvas will be affected, therefore, we call canvas before the operation. save () to save the current status of the canvas. After the operation, extract the previously saved status, which will not affect other elements.

 

Source code: http://www.bkjia.com/uploadfile/2011/1113/20111113073453808.rar

 

Himi original, reprinted please note! Thank you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.