10 basic principles that should always be adhered to in Java game development

Source: Internet
Author: User
Tags thread

About the two fictional concepts involved in the article:

First, the plotter: as we all know, the Java GUI is drawing with paint to repaint for image refresh, and the drawing components used in the continuous process of repaint and paint, which I call the plotter. In my personal experience, the timing of the drawing should always be in the repaint after the paint, that is, through the repaint trigger refresh after the implementation, when the specific logic of the completion of its corresponding image drawing, and then through the unified interface to insert its image into the paint, in order to match the needs, The drawing device should always be implemented as an interface.

Second, the listener: Here is the listener, not specifically a listener component, but includes all the listeners required in the Java game collection. Because of the possibility of switching to different game modes in Java games, the mouse or keyboard events that need to be handled in different modes of play vary. So in Java game development, we need a set of replaceable listeners to change different listening events in different game modes, and listeners should always be implemented as interfaces in order to match needs.

Text, about the development of Java game should always adhere to the 10 Basic principles:

1, always maintain the uniqueness of the canvas.

In real life, the nutrients ingested by the human mouth and digestive tract can be absorbed by the heart, liver, spleen, lung, kidney and other visceral absorption, but no one will want to their heart, liver, spleen, lungs, kidney also get a mouth, because the consistency of the function of the realization as long as one is enough. However, sometimes we inadvertently add in the game, remove different panel or canvas in order to change the behavior of the screen, is tantamount to the game of the heart, liver, spleen, lungs, kidney-loaded mouth of the unwise move, should not be Java GUI are drawn out, redrawing the good, there is no need to switch components, otherwise thankless.

2, always the interface to transform the monitoring and processing image rendering, be sure to separate the view and logic layer.

For some mixed types of games, such as Slg+avg, RPG+STG, we will face the different modes of game monitor and plotter switching problems.

At this time the simplest choice is for each game type to order a corresponding panel to switch, so although the surface of worry, but it is also the most laborious and not flattering, and do not say flashing problem needs to be resolved alone, resource occupancy problem, light redundant code is enough people headaches.

The second is in a panel for different types of game using switch to switch the monitoring and mapping, the number of events can be small, efficiency is good, but a little more than I am afraid it is not so simple, more often only depressed, the same do not recommend use.

In my opinion, the best way to solve this problem is to follow the MVC pattern, the interface constructs the plotter and the listener, when the game changes, we only need to switch the listener and the drawing interface, can quickly change the game content, but does not need to discriminate the different implementation, thus avoids the component switching flicker and the delay , but also streamlined the code, more conducive to the development of the module division.

3, always static way to load the game commonly used resources, caching common objects, and timely release of useless resources.

Even if the history of today, Java is still not completely rid of the odious face of its system resource killer, the GC mechanism also causes us to be unable to release the resources in time, the more new, the system also becomes slower, this is especially deadly to the game which uses the graph resources massively. So we're going to do everything we can to make a common resource static to a unique instance to avoid repeated invocations, and to quickly null the resources that are not reused or rarely used after the call to wait for the GC to recycle automatically. Otherwise, you will find that your game is so close to the memory overflow ...

4, always in a circular manner to expand the game, the use of thread control game flow, to avoid the phenomenon of stiffness.

In fact, the so-called game development, to some extent, is only made by the programmer of a variety of graphics algorithms, to timely display the various resources of the slide program; the only difference is that the ordinary slide program in the human-computer interaction is weak, and the game of human-machine interaction is strong.

As we all know, the slide show in the display in any case to jump the display page, there must be a fixed beginning and end page, but also will be able to repeat the beginning and end of the sequence of its begin and end, to form a slide.

In fact, game making is the same, no matter how the game process changes, the game will have to have a main process, or a main loop body, so that we can start from the game to the end of the game, rather than from one end to the other end, that is, regardless of the details of the game in the Its main process and judgment must also be in order. In view of this characteristic, we decided that we should take the game main code as a large loop body, and then use the thread to control the game progress in the loop body, so as to better conform to this process. In short, we should use each of the graphics in the loop as a frame in flash, and the various control of the thread as a time axis, to adjust the playback speed of different frames and call time to complete a variety of different event interaction.

5, always in the processing of complex drawings directly prepare the map rather than by the program to draw.

We all know that Java drawings are actually GDI implementations, so the efficiency of drawing complex images can be imagined. Usually under strong conditions, unless the current effect is not programmable, or it caused by the loss of resources is really small to negligible, otherwise the best way is to use space for efficiency, ready to paste the picture directly, rather increase the volume of the program, do not let the players wait for the anger and greetings to your ancestors eight generations.

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.