Android game development: My little game 1-drawing chess pieces and boards in Game 5

Source: Internet
Author: User
Tags android games

Few technical blogs have been written before. If not, please forgive me!

First, I want to record some small exercises I have done, in this way, you can recall your learning experience at a certain time in the future as a reference, second, I hope that some of my work can give some reference to others who want to learn. After all, I have also referenced many examples of others on the road of learning and developing, I also learned a lot from others' experiences, so I want to pass on the spirit of this sharing on the Internet. Third, I feel that I have reached the entry level in programming, I want to give myself some motivation through my own sharing and record, so that I can stick to it better and spend more time on learning every day. First, I will position this blog as a series. In this series, I will explain all the development steps of Android games in the form of instances, it is estimated that there will be 8-10 games that will meet with you one after another. Since you are currently at work, there is not enough time. Generally, you will update your blog every Sunday, each week, at least all related content of a game is updated.


First, you need to write a game in Android. You need to understand the two types of views and surfaceviews that android uses during game development. In addition, glsurfaceview is mainly used to compile 3D games, I am not involved in this case, so I won't talk about it here.

What is the difference between view and surfaceview? I think you can find a lot of relevant answers on the Internet. I will repeat other people's summaries here. Of course, it is also my own experience in development.

The most essential difference between surfaceview and view is that surfaceview can re-draw the screen in a new separate thread, and view must update the screen in the main thread of the UI, so there is a problem, when it takes a long time to redraw the screen, updating the screen in the main UI thread may cause problems. For example, it takes too long for you to update the screen, then your main UI thread will be blocked by the function you are painting. Then, messages such as buttons and touch screens cannot be responded.

When surfaceview is used to update the screen in a new thread, it will not block your main UI thread. But this also brings about another problem, that is, event synchronization. For example, if you touch the screen, you need to process the thread in surfaceview. Generally, you need an event queue design to save the touch event. This is a little more complicated because it involves thread synchronization.

Based on the above, the game features are generally divided into two categories.

1. passively update the image. For example, you can use view for chess. Because the screen update is updated by touch screen, you can directly use invalidate or postinvalidate (the difference between the two functions will be explained in detail later), because in this case, this touch screen and the next touch screen take a long time and will not be affected.

2. Actively update. For example, a person is always running. This requires a separate thread to repeatedly repaint the state of the person to avoid blocking the main UI thread. Obviously, the view is not suitable and needs surfaceview for control.

Therefore, wuziqi should use view to write.

Start the code below:

It is mainly used to draw the board and chess pieces.

Public class gameview extends view {// screen width and height private int screenwidth = 0; private int screenheight = 0; // draw the start position of the Board private int startx = 0; private int starty = 0; // the height and width of each grid in the chessboard private int grid_width = 40; private int grid_num = 12; // number of lines in the board to be painted private paint = NULL; // represents the two-dimensional array of the pawns, each element in the array represents a private int [] [] chess = new int [grid_num] [grid_num]; private int chess_black = 1; // indicates the color of the pawns. 1 indicates black, 2 indicates white, 0 indicates no pawns. Private int chess_white = 2; private int chess_flag = 0; // It is used to record the color of the last chess piece. The value 1 is black, the value 2 is white, and the value 0 is the first time the chess piece was played. The last time the chess piece was not played, the public gameview (context) {super (context); paint = new paint (); // instantiate a paint brush. setcolor (0xff000000); // you can specify the paint color. setantialias (true); // sets the paint brush to be deprecated. Without this statement, the line drawn or the image is not smooth.} @ overrideprotected void ondraw (canvas) {// override this method in the view. This method is mainly used for drawing. Every time it is refreshed, the super method is called once. ondraw (canvas); canvas. drawcolor (0xffd700); // draws the background color of the screen in yellow. It is not only used to paint the screen in this color, but also to paint the screen. setcolor (0x458b00); // here, the paint brush is green, and the Board is drawn to green for (INT I = 0; I <grid_num; I ++) {// draw a canvas. drawline (startx, starty + I * grid_width, startx + (GRID_NUM-1) * grid_width, starty + I * grid_width, paint); // draw a canvas. drawline (startx + I * grid_width, starty, startx + I * grid_width, starty + (GRID_NUM-1) * grid_width, paint);} // draw the pawn for (INT I = 0; I <grid_num; I ++) {for (Int J = 0; j <grid_num; j ++) {If (chess [I] [J] = chess_black) {paint. setcolor (0xff000000); // a black paint brush that draws a Black canvas. drawcircle (startx + I * grid_width, starty + J * grid_width, 15, paint);} If (chess [I] [J] = chess_white) {paint. setcolor (0 xffffffff); // a white paint brush that draws a white canvas. drawcircle (startx + I * grid_width, starty + J * grid_width, 15, paint );}}}} // override the method for listening to touch events of view @ overridepublic Boolean ontouchevent (motionevent event) {float touchx = event. getx (); float touchy = event. gety (); If (touchx <startx | touchx> startx + (GRID_NUM-1) * grid_width | touchy <starty | touchy> starty + (GRID_NUM-1) * grid_width) {// click the system outside the chessboard. out. println ("...... BUDDY: It's incorrect, huh, ");} else {// Based on the clicked position, which is the position on the board, that is, the int index_x = math. round (touchx-startx)/grid_width); int index_y = math. round (touchy-starty)/grid_width); system. out. println ("... "+ index_x + "... "+ index_y); system. out. println ("... startx "+ startx + "... touchx "+ touchx); If (chess_flag = 0) {// This sentence indicates that at the beginning of the game, chess [index_x] [index_y] = chess_black; chess_flag = chess_black;} else if (chess_flag = chess_black & chess [index_x] [index_y] = 0) {chess [index_x] [index_y] = chess_white; chess_flag = chess_white;} else if (chess_flag = chess_white & chess [index_x] [index_y] = 0) {chess [index_x] [index_y] = chess_black; chess_flag = chess_black ;}} invalidate (); // after clicking finish, the ondraw method return Super will be executed again after the notification is repainted. ontouchevent (event );}}

 

In the function, if (chess_flag = chess_black & chess [index_x] [index_y] = 0) in the statement, chess [index_x] [index_y] = 0 is mainly used to determine whether there are pawns at the current position. If so, the pawns cannot be placed at this position.

 

:

 

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.