Android _ teach you how to write jump games (1)

Source: Internet
Author: User

Preface:

I have read a lot of blogs about andriod game development on CSDN and found that most of them are about one aspect of knowledge. I don't have a complete development tutorial.

So I wrote a series of blogs to fully describe the entire game development process.

I hope to give you some help and good suggestions. Learn Together ~

The author is just an ordinary andriod beginner. If there is something wrong with the blog, please contact me. I will modify it ~

Now, go to the topic:

This is the first blog. All the things involved in this blog are relatively basic ~~~

1. Game Introduction: This game is a jumping game, similar to graffiti jump. We touch the square through the ball to complete the ball jump. We will develop this game in this blog.

2. Final finished product display:

 

 

This is the final effect of the program. Each small square has many motion modes, horizontal, vertical, static, and circular motion... At the same time, each small square has different attributes. Some will disappear, some will be more flexible, and so on... The specific design method will be detailed in the following articles. You can also think about how to design the game first.

3. This lesson mainly completes the construction of the Basic Framework: We use SurfaceView to implement the game (View is generally used for games that do not need to be refreshed all the time, such as wuziqi and even watching ).

SurfaceView implements dual buffering by default. Therefore, the efficiency is higher.

A simple understanding of double buffering is to draw the pattern on the screen to a canvas (such as Bitmap), and then draw the canvas directly to the screen, this avoids flickering.

Create an android project first.

 



 

Create a GameView. That is, the subclass of SurfaceView. Organization diagram of the project:

 

 

Code attached:

GameActivity. java

[Java] package jumpball. game;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. DisplayMetrics;
Import android. view. Window;
Import android. view. WindowManager;
 
Public class GameActivity extends Activity {
GameView mView;
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );

RequestWindowFeature (Window. FEATURE_NO_TITLE); // set no title
GetWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN,
WindowManager. LayoutParams. FLAG_FULLSCREEN); // sets the full screen mode.
// Obtain the screen attributes of the system.
DisplayMetrics dm = new DisplayMetrics ();
GetWindowManager (). getDefaultDisplay (). getMetrics (dm );

MView = new GameView (this, dm. widthPixels, dm. heightPixels );
// Apply this layout
SetContentView (mView );
}
}
Package jumpball. game;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. DisplayMetrics;
Import android. view. Window;
Import android. view. WindowManager;

Public class GameActivity extends Activity {
GameView mView;
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );

RequestWindowFeature (Window. FEATURE_NO_TITLE); // set no title
GetWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN,
WindowManager. LayoutParams. FLAG_FULLSCREEN); // sets the full screen mode.
// Obtain the screen attributes of the system.
DisplayMetrics dm = new DisplayMetrics ();
GetWindowManager (). getDefaultDisplay (). getMetrics (dm );

MView = new GameView (this, dm. widthPixels, dm. heightPixels );
// Apply this layout
SetContentView (mView );
}
} [Java] GameView. java
GameView. java [java] package jumpball. game;
 
Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. view. SurfaceHolder;
Import android. view. SurfaceView;
Import android. view. SurfaceHolder. Callback;
 
// It is a Runnable interface.
Public class GameView extends SurfaceView implements Callback, Runnable {
Public int width, height;
Private Canvas mCanvas;
// Refresh the interface thread
Private Thread mThread;
// Processor
Private SurfaceHolder mSurfaceHolder;
Private boolean mIsRunning = false;
Private int TIME_IN_FRAME = 50;
GameActivity gameActivity;
 
Public GameView (Context context, int width, int height ){
Super (context );
// SetFocusable (true );
// Activity is a subclass of context.
GameActivity = (GameActivity) context;
This. width = width;
This. height = height;

MSurfaceHolder = getHolder ();
MSurfaceHolder. addCallback (this );
}
 
Public void mDraw (){
// Set the canvas color
MCanvas. drawColor (Color. WHITE );
DrawBG (mCanvas );
}
 
Public void drawBG (Canvas mCanvas ){
Paint mPaint = new Paint ();
MPaint. setColor (Color. BLACK );
// Set transparency
MPaint. setAlpha (50 );
// Set anti-aliasing
MPaint. setAntiAlias (true );
Float h = height * 0.01666667f;
For (int I = 0; I MCanvas. drawLine (0, I, width, I, mPaint );
}
}
Public void surfaceChanged (SurfaceHolder holder, int format, int width,
Int height ){
}
 
Public void surfaceCreated (SurfaceHolder holder ){
MIsRunning = true;
// Create a screen flushing thread
MThread = new Thread (this );
MThread. start ();
}
 
Public void surfaceDestroyed (SurfaceHolder holder ){
// Destroy this,
MIsRunning = false;
}
 
Public void run (){
// Determines whether the thread continues execution.
While (mIsRunning ){
Long startTime = System. currentTimeMillis ();
// Call mDraw to draw
Synchronized (mSurfaceHolder ){
MCanvas = mSurfaceHolder. lockCanvas ();
MDraw ();
MSurfaceHolder. unlockCanvasAndPost (mCanvas );
}

Long endTime = System. currentTimeMillis ();

Int diffTime = (int) (endTime-startTime );

While (diffTime <TIME_IN_FRAME ){
DiffTime = (int) (System. currentTimeMillis ()-startTime );
Thread. yield ();
}
}
}
}
Package jumpball. game;

Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. view. SurfaceHolder;
Import android. view. SurfaceView;
Import android. view. SurfaceHolder. Callback;

// It is a Runnable interface.
Public class GameView extends SurfaceView implements Callback, Runnable {
Public int width, height;
Private Canvas mCanvas;
// Refresh the interface thread
Private Thread mThread;
// Processor
Private SurfaceHolder mSurfaceHolder;
Private boolean mIsRunning = false;
Private int TIME_IN_FRAME = 50;
GameActivity gameActivity;

Public GameView (Context context, int width, int height ){
Super (context );
// SetFocusable (true );
// Activity is a subclass of context www.2cto.com.
GameActivity = (GameActivity) context;
This. width = width;
This. height = height;

MSurfaceHolder = getHolder ();
MSurfaceHolder. addCallback (this );
}

Public void mDraw (){
// Set the canvas color
MCanvas. drawColor (Color. WHITE );
DrawBG (mCanvas );
}

Public void drawBG (Canvas mCanvas ){
Paint mPaint = new Paint ();
MPaint. setColor (Color. BLACK );
// Set transparency
MPaint. setAlpha (50 );
// Set anti-aliasing
MPaint. setAntiAlias (true );
Float h = height * 0.01666667f;
For (int I = 0; I MCanvas. drawLine (0, I, width, I, mPaint );
}
}
Public void surfaceChanged (SurfaceHolder holder, int format, int width,
Int height ){
}

Public void surfaceCreated (SurfaceHolder holder ){
MIsRunning = true;
// Create a screen flushing thread
MThread = new Thread (this );
MThread. start ();
}

Public void surfaceDestroyed (SurfaceHolder holder ){
// Destroy this,
MIsRunning = false;
}

Public void run (){
// Determines whether the thread continues execution.
While (mIsRunning ){
Long startTime = System. currentTimeMillis ();
// Call mDraw to draw
Synchronized (mSurfaceHolder ){
MCanvas = mSurfaceHolder. lockCanvas ();
MDraw ();
MSurfaceHolder. unlockCanvasAndPost (mCanvas );
}

Long endTime = System. currentTimeMillis ();

Int diffTime = (int) (endTime-startTime );

While (diffTime <TIME_IN_FRAME ){
DiffTime = (int) (System. currentTimeMillis ()-startTime );
Thread. yield ();
}
}
}
}
 
Last final run:

 

For the source code, see the final version!

From the EaSy Column
 

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.