A simple game source code
Author: Chen yuefeng from: http://blog.csdn.net/mailbomb to achieve a simple flop game, each open up to two numbers, if the number is the same then disappear, otherwise the two numbers are not displayed, you can continue the game. The game rules are as follows: 1. Game data is stored using two one-dimensional arrays: u Map stores Logical Data U displaymap stores drawn data.-1 indicates that the data has been opened, and 0 indicates that the data has not been opened, if the value is greater than 0, the system is opened. 2. Drawing Rules: U draws a graph in the center of the screen. U. The width and height of each cell are 20, and the interval between each cell is 43. The game end discrimination rule is as follows: the code for deleting all the U cells or only one remaining cell is as follows: Package simplegame; import Java. util. random; import javax. microedition. lcdui. *;/*** graphic game * game data is stored in two arrays respectively. * 1. array map stores the actual map data, the Data Length displayed in each row is the number of levels + 1*2. The opened map data is stored in the displaymap array.-1 indicates that the data has been opened, and 0 indicates that the data has not been opened, if the value is greater than 0, the map data has been opened * @ author Chen yuefeng * @ version 1.0 */public class simplegamecanvas extends canvas implements runnable {/**. The length of the map data is (Leve L + 1) * (LEVEL + 1) */private int [] map;/** the data displayed. 0 indicates that the data is not displayed, and-1 indicates that the data disappears, if the value is greater than 0, the opened data */private int [] displaymap;/** level */private int level = 1; /** Number of opened numbers */INT num = 0;/** select the sequence number of the box. The numbering rule is from left to right, from top to bottom */INT Index = 0; /** the first opened block */INT firstindex = 0;/** the second opened block */INT secondindex = 0; /** screen width */private int width;/** screen height */private int height;/** width of each cell */private final int tile_width = 20; /** height of each cell */priva Te final int tile_height = 20;/** interval between each cell */private final int inner_width = 4;/*** default constructor */Public simplegamecanvas () {// initialize data width = This. getwidth (); Height = This. getheight (); Init (level); // start the new thread (this ). start ();} protected void paint (Graphics g) {// clear screen G. setcolor (0 xffffff); G. fillrect (0, 0, width, height); G. setcolor (0); // draw the number drawnumber (g); // draw the level number G. drawstring ("level" + level + "Off", this. getwidth ()/2, 15, graphics. baseline | graphics. hcenter);} private void drawnumber (Graphics g) {// screen Center Coordinate int xcenter = width/2; int ycenter = height/2; // The upper left coordinate int x = xcenter-(LEVEL + 1) * tile_width + (LEVEL + 1)-1) * inner_width)/2; int y = ycenter-(LEVEL + 1) * tile_height + (LEVEL + 1)-1) * inner_width)/2; // draw cyclically for (INT I = 0; I <displaymap. length; I ++ ){ // Draw the border and number G. setcolor (0); If (displaymap [I]> = 0) {G. fillrect (x + (I % (LEVEL + 1) * (tile_width + inner_width), Y + (I/(LEVEL + 1) * (tile_height + inner_width), tile_width, tile_height); // draw the internal number if (displaymap [I]> 0) {G. setcolor (0 xffffff); G. drawstring (string. valueof (displaymap [I]), X + (I % (LEVEL + 1) * (tile_width + inner_width) + 7, Y + (I/(LEVEL + 1 )) * (tile_height + inner_widt H) + 2, graphics. top | graphics. left) ;}}// select the draw box G. setcolor (0xff0000); G. drawrect (x + index % (LEVEL + 1) * (tile_width + inner_width)-1, Y + (index/(LEVEL + 1) * (tile_height + inner_width)-1, tile_width + 1, tile_height + 1);}/*** initialize map data based on the level * @ Param level */private void Init (INT level) {// initialize map data size map = new int [(LEVEL + 1) * (LEVEL + 1)]; // initialize map data for (INT I = 0; I <map. Length; I ++) {map [I] = I % (map. length/2) + 1;} // random disturbance map data random ran = new random (); int Index = 0; int temp; For (INT I = 0; I <map. length; I ++) {Index = math. ABS (ran. nextint () % map. length); // exchange data temp = map [I]; Map [I] = map [Index]; Map [Index] = temp ;} // initialize the display map data size displaymap = new int [(LEVEL + 1) * (LEVEL + 1)];} public void keypressed (INT keycode) {int action = This. getgameaction (K Eycode); Switch (Action) {Case up: If (index-level-1> = 0) {Index = index-level-1;} break; case down: if (index + level + 1 <= (LEVEL + 1) * (LEVEL + 1)-1) {Index = index + level + 1;} break; case left: if (index> 0) {index --;} break; case right: if (index <(LEVEL + 1) * (LEVEL + 1)-1) {index ++ ;} break; Case fire: // confirm if (Num <2 & displaymap [Index]> = 0) {// The number of opened windows is less than 2 num + +; // Increase the number of opened indexes. 1 // record the index value if (num = 1) {firstindex = index;} else if (num = 2 & Index! = Firstindex) {secondindex = index;} // display this number displaymap [Index] = map [Index] ;}} repaint ();} /*** whether to upgrade * If the remaining one or all disappears, the upgrade * @ return true indicates the upgrade */private Boolean islevelup () {int n = 0; // store the number of unopened blocks for (INT I = 0; I <displaymap. length; I ++) {If (displaymap [I]! =-1) {n ++ ;}}if (n> 1) {return false ;}else {return true ;}} public void run () {try {While (true) {// pause thread. sleep (100); // logical processing // processing logic if (num = 2) {try {thread. sleep (500);} catch (exception e) {} If (Map [firstindex] = map [secondindex]) {// displaymap [firstindex] =-1; displaymap [secondindex] =-1;} else {displaymap [firstindex] = 0; displaymap [secondindex] = 0;} num = 0;} // upgrade if (islevelup ()) {level ++; // LEVEL + 1 Init (level); // initialize the map data num = 0; // The number of opened items returns to zero Index = 0; // select the first square by default} // repaint () ;}} catch (exception e ){}}}