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

Source: Internet
Author: User

This is the second blog. In this tutorial, We will write a small ball.

First, let's take a look at the class diagrams of the small ball class:

 

 

Mainly determine the coordinates of the ball: x, y. Radius of the ball: r. And the motion function of the ball and the draw () function on the screen.

Difficulty: move () function. We will simulate a physical environment to make the ball look elastic. For details about this part, I will answer it in the next article.

The code for Ball. java is given below.

[Java] package jumpball. game;
 
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
 
Public class Ball {
Float r, x, y;
Private Paint mPaint;
// The maximum vertical speed direction is up to 16.
Float MAXVERTICALSPEED =-16, MAXVERTICALA = 1;
// Default maximum height
Float defaultJumpHight;
// The distance to move the ball in the vertical direction!
Float verticalMove;
Float ha = 0, va = 1f, v0v, vtv, v0h, vth;
GameView gameView;
 
Public Ball (GameView gameView ){
This. gameView = gameView;
MPaint = new Paint ();
MPaint. setColor (Color. BLACK );
DefaultJumpHight = gameView. height/3;
R = gameView. width/36;
Y = gameView. height-r * 2;
X = gameView. width/2-r;
// Calculated based on the acceleration formula Vt ^ 2-V0 ^ 2 = 2AX
MAXVERTICALSPEED =-(int) (float) Math. sqrt (2 * gameView. height/3)-1 );
VerticalMove = defajumjumphight;
V0v = MAXVERTICALSPEED;
}
 
Public void draw (Canvas canvas ){
Move ();
Canvas. drawCircle (x, y, r, mPaint );
}
 
Public void move (){
// Vt = V0 + aT current speed = initial speed + acceleration * Time
Vtv = v0v + va;
// When the descent speed reaches a certain degree, set the acceleration to 0.4f.
If (vtv>-5 * MAXVERTICALSPEED/8 ){
Va = 0.4f;
} Else {
Va = MAXVERTICALA;
}
// When the current height to be increased is greater than the default height, the speed continues to maintain the maximum speed, vtv <0 indicates the direction up
If (verticalMove> defaultJumpHight & vtv <0 ){
Vtv = MAXVERTICALSPEED;
}
Float vMove = (v0v + vtv)/2; // This time the vertical height is moved.
VerticalMove = verticalMove + vMove; // reduces the number of hours, indicating that the Internet has moved
Y = y + vMove;
V0v = vtv;
If (y> gameView. height) {// touched the ground
Y = gameView. height-this. r;
V0v = MAXVERTICALSPEED;
VerticalMove = defajumjumphight;
}
}
}
Package jumpball. game;

Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;

Public class Ball {
Float r, x, y;
Private Paint mPaint;
// The maximum vertical speed direction is up to 16.
Float MAXVERTICALSPEED =-16, MAXVERTICALA = 1;
// Default maximum height
Float defaultJumpHight;
// The distance to move the ball in the vertical direction!
Float verticalMove;
Float ha = 0, va = 1f, v0v, vtv, v0h, vth;
GameView gameView;

Public Ball (GameView gameView ){
This. gameView = gameView;
MPaint = new Paint ();
MPaint. setColor (Color. BLACK );
DefaultJumpHight = gameView. height/3;
R = gameView. width/36;
Y = gameView. height-r * 2;
X = gameView. width/2-r;
// Calculated based on the acceleration formula Vt ^ 2-V0 ^ 2 = 2AX
MAXVERTICALSPEED =-(int) (float) Math. sqrt (2 * gameView. height/3)-1 );
VerticalMove = defajumjumphight;
V0v = MAXVERTICALSPEED;
}

Public void draw (Canvas canvas ){
Move ();
Canvas. drawCircle (x, y, r, mPaint );
}

Public void move (){
// Vt = V0 + aT current speed = initial speed + acceleration * Time
Vtv = v0v + va;
// When the descent speed reaches a certain degree, set the acceleration to 0.4f.
If (vtv>-5 * MAXVERTICALSPEED/8 ){
Va = 0.4f;
} Else {
Va = MAXVERTICALA;
}
// When the current height to be increased is greater than the default height, the speed continues to maintain the maximum speed, vtv <0 indicates the direction up
If (verticalMove> defaultJumpHight & vtv <0 ){
Vtv = MAXVERTICALSPEED;
}
Float vMove = (v0v + vtv)/2; // This time the vertical height is moved.
VerticalMove = verticalMove + vMove; // reduces the number of hours, indicating that the Internet has moved
Y = y + vMove;
V0v = vtv;
If (y> gameView. height) {// touched the ground
Y = gameView. height-this. r;
V0v = MAXVERTICALSPEED;
VerticalMove = defajumjumphight;
}
}
}
 
Define a small ball object in the GameVIew class.

[Java] Ball ball;
Ball ball;
Instantiate the GameView constructor.

[Html] ball = new Ball (this );
Ball = new Ball (this );
Add the following in the mDraw function of GameView:

[Html] ball. draw (mCanvas );
Ball. draw (mCanvas );
In this way, we have completed the ball object... Wahaha ~ (The game looks a little better !)

Run the program. We can see an elastic ball ~~~~

 

Finally, we move the left and right sides of the ball. This part is relatively simple ~~~

Two boolean variables are defined in GameView to indicate whether the left and right keys are pressed.

Rewrite two methods at the same time

[Html] public boolean onKeyDown (int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_BACK ){
GameActivity. finish ();
Return true;
} Else if (keyCode = KeyEvent. KEYCODE_DPAD_LEFT ){
LeftKeyDown = true;
} Else if (keyCode = KeyEvent. KEYCODE_DPAD_RIGHT ){
RightKeyDown = true;
}
Return true;
}
Public boolean onKeyUp (int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_DPAD_LEFT ){
LeftKeyDown = false;
} Else if (keyCode = KeyEvent. KEYCODE_DPAD_RIGHT ){
RightKeyDown = false;
}
Return true;
}
Public boolean onKeyDown (int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_BACK ){
GameActivity. finish ();
Return true;
} Else if (keyCode = KeyEvent. KEYCODE_DPAD_LEFT ){
LeftKeyDown = true;
} Else if (keyCode = KeyEvent. KEYCODE_DPAD_RIGHT ){
RightKeyDown = true;
}
Return true;
}
Public boolean onKeyUp (int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_DPAD_LEFT ){
LeftKeyDown = false;
} Else if (keyCode = KeyEvent. KEYCODE_DPAD_RIGHT ){
RightKeyDown = false;
}
Return true;
}
Then add the following in the move method of the ball:

[Html] // horizontal movement settings
If (gameView. leftKeyDown = true ){
Xx = X-10;
}
If (gameView. rightKeyDown = true ){
Xx = x + 10;
}
If (x <r ){
X = r;
}
If (x> gameView. width-r ){
X = gameView. width-r;
}
// Horizontal movement settings
If (gameView. leftKeyDown = true ){
X = X-10;
}
If (gameView. rightKeyDown = true ){
X = x + 10;
}
If (x <r ){
X = r;
}
If (x> gameView. width-r ){
X = gameView. width-r;
}
The left and right movements of the ball have been completed here, but the actual operation has found that the ball cannot be moved at all ~~~

I checked the information and found the problem here:

For a custom View, the onKeyDown method is called only after the focus is obtained ~ The onKeyDown method of Activty is called only when all empty controls are not processed.

Therefore, we add a sentence in the composition method of GameView:

[Html] setFocusable (true );
SetFocusable (true );
Okay... Success ~~~~ In this way, we can control the movement of the ball !!!

For more information about the move function, see the video ~~~

The following shows how to run the program: and a structure of the project:

 

 

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.