Android game development-nancyglines Design

Source: Internet
Author: User

Today, we migrated the previous Python-implemented nancyglines game to Android. Although it is still a rough version, the interface is ugly and its functions are not complete enough, the entire framework has been established, in addition, the basic functions of the game have been implemented. See:

 

Game rules:

1. Touch a ball and select a place without a ball to be moved.

2. After the ball moves over, if the ball meets the requirements of horizontal, vertical, oblique with the color of the ball is greater than or equal to 5, then eliminate these balls with the same color score.

3. If there is no elimination, three new balls will be dropped.

4. The game is over until the Board has no position to hold the new ball.

The following is layout:

<? XML version = "1.0" encoding = "UTF-8" ?>
< Framelayout Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: layout_width = "Fill_parent"
Android: layout_height = "Fill_parent" >
< Com. coderzh. nancyglines. glinesview
Android: ID = "@ + ID/glines"
Android: layout_width = "Fill_parent"
Android: layout_height = "Fill_parent" />
< Relativelayout
Android: layout_width = "Fill_parent"
Android: layout_height = "Fill_parent"   >
< Textview
Android: ID = "@ + ID/text"
Android: Text = "@ String/app_name"
Android: visibility = "Visible"
Android: layout_width = "Wrap_content"
Android: layout_height = "Wrap_content"
Android: layout_centerinparent = "True"
Android: gravity = "Center_horizontal"
Android: textcolor = "#88 ffffff"
Android: textsize = "24sp" />
</ Relativelayout >
</ Framelayout >

 

Well, I used a custom view-glinesview. The prototype of glinesview is as follows:

Public ClassGlinesviewExtendsSurfaceviewImplementsSurfaceholder. Callback {

}

Surfaceview is inherited here because surfaceview has some advantages in game production. Next, I refer to the lunarlander in the sample.CodeA surfaceview Internal Thread class is created to process the game logic and draw game images.

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> Public class glinesview extends surfaceview implements surfaceholder. callback {

class glinesthread extends thread {
Public void initgame () {
}

Public VoidSetrunning (BooleanRunning ){
Mrun=Running;
}

@ Override
Public VoidRun (){
Updatecanvas ();

}
}

Public Glinesview (context, attributeset attrs ){
Super (Context, attrs );
Surfaceholder holder = Getholder ();
Holder. addcallback ( This );
Thread =   New Glinesthread (holder, context, New Handler (){
@ Override
Public   Void Handlemessage (message m ){
Mstatustext. setvisibility (M. getdata (). getint ( " VIZ " ));
Mstatustext. settext (M. getdata (). getstring ( " Text " ));
}
});
}

@ Override
Public VoidSurfacecreated (surfaceholder holder ){
Thread. initgame ();
Thread. setrunning (True);
Thread. Start ();
}
}

When the surfacecreated event occurs, the game starts to be triggered. initgame () performs some initial game settings, setrunning sets the current status of the game, and start runs the thread.

Because I don't need to refresh the screen in real time, I didn't use a while loop in the thread's run method, but just called the method updatecanvas () for refreshing the screen ();

When you touch the screen, the ontouchevent method of glinesview is triggered. Therefore, add the Code:

@ Override
Public   Boolean Ontouchevent (motionevent event ){
Float X = Event. getx ();
Float Y = Event. Gety ();
Thread. dotouch (x, y );
Return   Super . Ontouchevent (event );
}

 

Then, implement the dotouch method of glinesthread:

Public   Void Dotouch ( Float Posx, Float Posy ){
// Activate or move a ball
}

 

I will use a two-dimensional array to save the status of each grid on the Board:

Private   Int Mstatus [] [];

For example, mstatus [0] [1] = color. Blue indicates that the grid in the second column of the First row contains a blue ball.

When I want to move a ball, first we need to implement the shortest path.AlgorithmBecause it cannot be moved if there are other obstacles to the ball. Therefore, I used a similar Dijkstra shortest path algorithm to implement the ball moving function:

Private   Void Moveball ( Int Currentx, Int Currenty, Int Targetx,
Int Targety ){
}

 

Then, after moving the ball, you also need to determine whether it meets the conditions where the horizontal and vertical skew is greater than or equal to 5. If yes, the balls will be eliminated. Therefore, add the clearbils method:

Private   Boolean Clearbils (ball ){
}

When the score criteria are not met, you need to drop three new balls. Therefore, the getthreebils method is implemented:

Private   Void Getthreebils (){
}

 

In fact, the entire framework has been set up here. The overall principle is to modify the mstatus data structure of the checker status through some operations. After the operation is complete, call updatecanvas () to refresh the screen.

After the above method is implemented, the game can run. The effect shown above. After that, I also need to do some page beautification, adding menus and levels.

Attached:

Complete code:/files/coderzh/code/nancyglines.rar

Experience APK file:/files/coderzh/code/nancyglines.apk.rar

I hope you will give us some valuable comments and I will continue to improve this game.

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.