Design and implementation of J2ME submarine war game

Source: Internet
Author: User
Tags array exit interface thread
Design

Summary

This paper gives an aircraft shooting class (submarine) game design process and the corresponding flow chart, and provides a demo version of the source code download address, the game in the Sun WTK v2.2 debugging Pass.

Disclaimer: Most of the resources used in this game are not owned by authors, and the code is for non-commercial use only.

Here are a few screenshots of the game



Game Design

1 Game Operation Flow:

The game's initial screen is a implusive list format, select New game can start the game, the game is open in Full-screen mode, but there are still for pause/start and Exit command response function keys. In the game the suspension, sea water and marine life will continue to move, but the enemy submarine and all torpedoes will be static, the player's control will not work until the game is started again. Exit from the game state will return to the menu interface, select Exit again to exit the program.

The following is a general flowchart of the game:




2 algorithm Processing

The game itself is a single State structure (Singletone), which is handled by a controller to handle the screen and state that should now appear. The main display classes include the main screen, the game canvas, the help screen, and some auxiliary alert, the most important of which is the game screen. The game is running in MIDP 2, so the screen inherits the Gamecanvas. The role layers in the game are all loaded into a layer manager that is much larger than the size of the phone screen. When the player position is moved, the layer manager calculates the player's relative position synchronously and moves the current layer's display position.

This game is the essence of the MIDlet after the start of the entire canvas as a thread, every 25MS scan user button operation, every 50ms scan an enemy and torpedo and other objects of the state of motion, for more demanding bullet injection real-time response, then inherited the Keypressed method.

When scanning the player's mobile operation, the key code is passed to the player's submarine response method. In the sweep of the enemy and torpedo movement, the two-step completion.

The first step is to perform the moving methods of each object's picture. The program does not open a new thread for each new torpedo or enemy. Too many threads cause the program's performance to plummet; instead, keep a vector array for each object type, and whenever a new object is generated, he will first be added to the array to which it belongs, and then draw on the layer, and when the object is eliminated, In addition to being eliminated on the layer, you need to remove it from the response array. This forms a registration mechanism, all game object generation and elimination need to register to the canvas, the canvas has all the "list" of different types of objects, so when the move command, only need to traverse the array of types, and then execute each of the elements of the Move method.

The second step, is the general meaning of the redraw, where the use of flushgraphics () instead of the traditional repaint (), because this game is to import pictures as the operating object, picture location changes have been implemented in the first step, so there is no how to involve pixel-level repaint, It is only necessary to load the corresponding layers of each object array into the new layer manager when the game is initialized.


Drawing of Canvas trigger process:

(because there is no rose on hand, only temporarily use the ECLIPSEUML flowchart to draw a sketch, a number of problematic areas need to be discussed, but also hope to be good at this to friends)



3 class Structure

The following lists the meaning structures of the main constituent classes and lists a class association diagram centered on the game canvas.

Controller: Control class that controls which Displayable class is displayed on the current screen.

Subobject: interface type. Removable layer types such as player submarines, enemy submarines, and torpedoes are extended to this interface.

Sub: Player submarine. Includes the location, life values and other member variables as well as moving, firing and other methods.

Enemysub, Moveablesprite, Tinfish: Enemy submarines, marine life, torpedoes. Are removable layer types that are registered in the canvas once they are generated and maintain their movement and life status by the time trigger.

Subcanvas: Game main canvas. Store the game map information, game status, player information, synchronous maintenance of enemy submarines, torpedoes, marine life movement and survival status.

Subcanvas Association Class Diagram:





4 source Code

The source code of this program, including Jad, confused jar and a RAR file, all can be downloaded to the Http://www.hyweb.net/BrowseFiles.aspx?Folder=Public/My%20Project, the code contains more detailed comments. This program is only a demo version, there are some legacy problems (Player life value display, performance optimization) still need to be further improved, have any good suggestions, please be sure to contact me. ^_^




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.