In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use Java to achieve plant war zombie game". In daily operation, I believe many people have doubts about how to use Java to achieve plant war zombie game. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "how to use Java to achieve plant war zombie game". Next, please follow the editor to study!
I. Game Design-rules
There is a Mini Game level in plants vs. zombies, and there is a roller at the top of the screen, which will generate plants randomly. Players are free to choose the lawn to place after selecting plants. Based on this game mode, this paper extracts the level and makes a simple version of plants vs. zombies. A plant card is automatically generated on the left side of the screen, which can be placed on the lawn after clicking to select it. Zombies are automatically generated on the right. Different zombies move at different speeds and have different blood volume, and some zombies have hidden rewards, such as full-screen zombie stillness, full-screen zombie death, and so on. First of all, let's talk about how the game pause function should be done. The simplest way to pause is to move the mouse off the screen and pause the game. So you need to introduce a mouse listener event here. As shown in the following figure:
Of course, this is just a simple way to change the state of the game by listening to the position of the mouse. You can also use a keyboard listener to pause the game when a key is pressed, which is a better user experience, but the principle is the same.
2. Game Design-object
First of all, analyze what objects are in the game. All kinds of plants, all kinds of zombies, all kinds of bullets. Then three parents can be extracted here, namely, plants, zombies, and bullets. In object orientation, the subclass inherits all the properties and methods of the parent class. So you can extract the common properties and methods of the three categories into their respective parent classes. For example, the zombie parent class is shown in the figure:
Plant parents and bullet parents can be obtained in the same way. It is mentioned above that the methods shared by subclasses need to be extracted into the parent class, so what about the methods shared by some subclasses? For example, pea shooters and ice shooters can fire bullets, but the nut wall does not shoot. So the interface needs to be used here. As shown in the figure:
So far, the properties and methods of the game object are basically defined. As for the display of the picture and how to draw the picture, you only need to use the corresponding API, so there is no description here.
3. Game Design-function
Now that we have the objects of the game, it's time to start adding them to the game, then get them moving, and finally let them fight. First of all, I did this by adding objects to the game, and here we take zombies as an example, as shown in the figure:
First of all, when the plant enters the stadium, it is on the roller, and the movement on the roller will involve the problem of chasing and stopping. The way to chase is, of course, to chase the previous plant card, but when the first plant card is selected and placed on the grass, how to chase? The plant collection is divided into the collection on the roller and the collection on the battlefield, as shown in the figure:
Next, it's time to get the subjects moving. It was mentioned earlier that the movement methods in the parent class are abstract methods, and when they are rewritten in their respective subclasses, different objects move in a variety of ways. As shown in the figure:
Looking at the complex traversal of collections in the code, I have to feel that lambda expressions are really a good thing, as shown in the figure:
How to get the objects to fight? Here's an example of zombies attacking plants:
Combined with the picture, the black box P represents the plant, the black box Z represents the plant, and the dotted line refers to the limit distance between the two. When the zombie enters the dotted line, it is guaranteed to attack the plant. At this point, the basic functions of the game have been basically realized. Java is an object-oriented language. Everything is an object, a feature is an attribute, and a behavior is a method. Zombies, plants and lawns that can be seen by the naked eye are all objects. the properties of the object, such as blood volume and movement speed, are attributes, and the behavior of the object, such as movement, attack and death, are all methods.
4. Game design-background music
Bgm is one of the souls of a game. Here, you can add background music to the game. You can create a new thread to parse and play the music, as shown in the figure:
It should be noted here that the API for parsing music in Java only supports files in WAV format, and file format conversion can be done by most music players.
At this point, the study on "how to use Java to achieve plant war zombie game" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un