This is the 18th day of my participation in the August Challenge

Remember the legendary classic 90 tank battle? Those nostalgic memories, along with us came the classic tank battle, the beginning of the fear, repeatedly destroyed by the enemy tank scene vividly. Now you don’t have to worry about enemy tanks anymore, you can go on a rampage and wipe out enemy tanks. As a programmer, I can just use my Java knowledge to complete a tank war game. Let’s see how it was designed.

Demand analysis:

First, the game should have a graphical user interface that reflects all the details of the game.

The interface should have tanks, walls, trees, rivers.

Interface to have a “home”, “home” is attacked in the game lost.

There are two types of tanks, enemy and us.

Walls are also divided into two types, ordinary walls that can be penetrated, and iron walls that cannot be penetrated.

There is only one type of tree and one type of river, forest tank can pass through.

Tanks can fire bullets, enemy bullets have the same properties.

Our bullets can kill enemy tanks, and enemy bullets can kill our tanks, but it takes multiple bullets to kill them.

Enemies may not kill each other.

Bullets explode when they hit a tank, but not when they hit a wall.

We can eat health packs to increase health.

The game can pause, restart, game help and other functions. The following figure

Functional design:

1. Design a graphical user interface for the home page, in which all elements of the game can be displayed. The interface can accept user operations and has man-machine interaction functions. Users can choose to restart the game, exit the game, pause the game and game help.

2. The interface includes tanks, trees, rivers, barrier walls, and “homes” that the user of the game has to protect.

3. Tanks: There are two types of tanks, enemy tanks and user-controlled tanks. Both the user and the enemy tanks can fire bullets, change their path, and change their direction when they encounter obstacles such as walls and the boundaries of the game, instead of sticking to obstacles all the time. Tanks cannot cross each other, they will automatically switch directions when they collide.

4. Trees: Trees should be included in the interface as covering objects and modifying objects, so as to increase the elements of the game and make the game more appropriate and humanized. There is no limit to the number of trees, depending on the overall clarity and aesthetics of the game.

5. Rivers: Rivers should be included in the interface. The function of rivers is the same as that of trees.

6. Walls: There are two types of walls, ordinary walls and iron walls. Ordinary walls will be damaged when hit by bullets, while iron walls can block the passage of bullets. Neither tank can penetrate either wall, but both can destroy normal walls. The number of ordinary walls should be properly arranged with other elements, but iron walls should not be too many, otherwise the game will be too difficult and the game will not be able to proceed because tanks cannot pass through iron walls.

7. “home” : the user side has a home to protect in addition to his tank, which is surrounded by ordinary walls. The game ends immediately after the home is attacked by bullets, and the user side loses the game.

8. Bullets: Bullets can be fired by the enemy and the user, and the fired bullets can move in a straight line until they hit obstacles and disappear. Bullets can pass through woods and rivers. An enemy tank will explode and die when hit by a single bullet, after which the tank will disappear. The user side will lose life after being attacked by bullets, and will die after being attacked four times. If there are enemy tanks, the user side will lose the game.

9. Explosion: When a bullet hits a tank, it will explode.

10. Direction: Both tanks and bullets have directions. You can choose four directions: up, down, left, and right, and the direction of bullets belongs to the direction of the tank.

Specific design:

Graphical user interface

Graphical user interface to use Java related knowledge, the need to use abstract window tool set that is AWT and Swing to design and implement, because the game needs functional keys to control the game to restart, exit, pause, help, so in the implementation process, design menu items.

Tanks, rivers, trees, homes, walls, etc. are drawn by calling the draw function in their respective classes. Finally, create a new graphics panel and add all the elements into the panel. The construction of the interface is done in the program by the constructor of the TankClient class, which produces an interface that contains various game elements without constructing a user class. The screen is static, but the screen of our game is dynamic. Tanks and bullets in the game are constantly moving, and ordinary penetrating walls are also “moving”. Therefore, the screen must be constantly updated and redrawn to produce dynamic effects.

In the program, this function is also implemented by the TankClient class update and framPaint functions, which redraw elements that are “moving” and do not redraw elements that are not moving, such as menu items. This is why menu items are designed into the TankClient class constructor. Of course, graphical user interfaces also include design for the size, position, and color of various interface elements.

Elements in the interface

The elements in the interface are added using the add method of the interface instance object during the construction of the interface, and the “moving” elements are constantly updated and redrawn.

tanks

Tanks are mainly implemented by the Tank class. Tank attributes: speed (X and Y speed), tank size, new location (X and Y coordinates), whether the tank is still alive, direction of action, etc. All of these properties have an initialization value and are ready to run at the start of the game.

During the design process, the location of tanks is determined by coordinates. The position behind the user side is monitored by the keyboard in the direction of the specified direction at a constant speed forward – this speed is a global static variable, when not to accept the keyboard control, it will remain static. Enemy tanks use random numbers to control their random directions and paths. When an enemy Tank hits an obstacle, it will return to its previous position. This function is implemented by the changToOldDir () method in the Tank class.

The direction of the Tank and the firing of bullets and the restart of the game are controlled by the keyboard, so these functions must be implemented in the Tank class.

The Tank class’s keyPressed() method is used to accept the keyboard’s keypressing, and fire() is used when a keyboard message such as F is received, so the Tank class’s fire() method is not passed because the direction of the bullet is always dependent on the Tank’s direction and position.

Since tanks change direction when hitting obstacles such as walls, interface boundaries, and “homes”, there must be a strategy for each obstacle in the Tank, and the solution is to move the next step to the previous one.

User Tank can also eat hearts to increase their health, get a heart, add 100 health. So in the program tanks must have a way to determine how their health increases when they touch hearts. Program using the eat () method to implement the “eat” and increases the life value of hearts, of course, which will make the tank of 200 and not more than his own life limit so judgment, when life is less than or equal to 100, 100 HP, directly but when life values greater than 100, can only make life value up to the limit of 200. Of course, when it comes to health, it must be displayed in the graphical user interface, so in Tank class must design a method to draw the process of life, in Tank class is DrawBloodbBar() to depict.

The woods

The forest is mainly used as a decorative object, with opacity. Represented by a Tree class, Tree has two attributes, position and length and width. The tree class has its own constructor and, of course, the requisite draw() method.

The river

A river acts like a forest.

The wall

The walls are divided into ordinary walls and iron walls. Ordinary walls can be damaged by bullets, but iron walls can’t.

Ordinary wall

A normal wall has the following properties: the fixed length and width of the wall, the position coordinates of the wall, and the wall is represented by images added to the GRAPHICAL user interface. The CommonWall class is used to describe a normal wall. This class has a constructor for passing parameters, a draw() method for drawing the wall at the specified position, and a getRect() method for constructing a rectangle instance.

Metal wall

The parameters of metal walls are exactly the same as those of normal walls, except that metal walls cannot be pierced by bullets, but this property will be included in the next discussion of bullet properties, since the normal wall properties are given above, so we will not repeat the properties of metal walls here.

home

Home is the Home class to abstract, specific properties are: the size of the house, the location of the Home, the Home state of survival, like forests and rivers, Home and the draw () and its construction method, function, as same as the previous but there are also some new methods, can be in the game to start the game because it is over, so be set method lets the Home “live” again, Of course, sometimes the isLive() and setLive() methods are necessary to determine the current state of the home. On top of that, there’s a game end screen cleanup and a hint, which the gameover() method solves.

The bullet

Bullet attributes: bullet’s X and Y speed — initial speed is 10, bullet’s length and width — initial length and width are 10, bullet’s position, bullet’s direction, whether the bullet is live, etc. Since bullets with different directions are actually pictures, we need to consider the selection of different pictures with the corresponding direction, so we need to set up Map key pairs, using the direction of the String attribute to specify different pictures. Such as:imgs.put(“L”, bulletImages[0]); Class convention has a constructor that defaults to passing position and direction, of course using isomorphism, and another constructor that gets the state and interface of the bullet. Darw () and move() methods are used to control bullets and move bullets. The next step is to take into account the reaction of bullets on each element. Enemy bullets do not kill their enemies when they hit them, so just return true. Let alone call the explosion to show the effect of the explosion.

Specific code:

Public Boolean hitTanks(List<Tank> tanks) {public Boolean hitTanks(List<Tank> tanks) {for (int I = 0; i < tanks.size(); I ++) {if (hitTank(tanks. Get (I))) {// For each tank, call hitTank return true; } } return false; }Copy the code

HitTank (Tank t) when bullets hit other tanks, hitWall(CommonWall w) when bullets hit walls, hitWall(MetalWall w) when bullets hit metal walls, hitHome hitHome(), of course in these methods, Remove () is also called in the method to remove it. For example, if you hit home, set the life of home to false to end the game. The implementation is as follows:

Public Boolean hitHome() {if (this.live && this.getRect().intersects(tC.home.getRect ())) {this.live = false;  this.tc.home.setLive(false); Return true; } return false; }Copy the code

In addition, when the bullet hits the opponent (user vs enemy), when the enemy hits the user, the user will reduce the health. If the enemy does not receive a shot, the user will execute t.setlife (T.goetlife () -50). // Take one bullet and your life will be reduced by 50. Take four shots and you will die. The total health is 200. End the game.

The explosion

The explosion effect of the tank is described independently by a class, the attributes of the explosion: location and survival state, in addition, to draw the explosion effect to obtain user interface control, so we need to define the private static Toolkittk = Toolkit.getDefaultToolkit(a); Other methods are more or less the same.

The menu features

Menu functions include restarting, pausing, exiting, etc. The user interface for these functions is in the TankClient constructor, using the panel class and corresponding listening functions, which are easy to implement.

Game screenshots:

Menu module

Menu items can choose to start a new game, exit, pause, continue, help function, for starting a new game, exit two functions, to give hints, users really want to continue to operate!

Menu renderings:

Start a new game:

Click “Confirm” to start a new game, and click “Cancel” to return to the original page

Game pause:

Help module:

Exit module:

Game information module 

Game title bar information

Effect of bullets exploding after hitting opposing tanks:

When a bullet hits a normal wall, it will make a piece of the wall disappear, and the red arrow is drawn in the picture. The walls were damaged.

The metal walls were hit by bullets,

Metal walls will not be damaged by bullet attacks, but will be damaged when the bullet hits a metal wall in the program.

Original code snippet:

public boolean hitWall(MetalWall w) {       // The bullet hit the metal wall
		if (this.live && this.getRect().intersects(w.getRect())) {
			this.live = false;
			return true;
		}
		return false;
	}
Copy the code

Modified code snippet:

public boolean hitWall(MetalWall w) {       // The bullet hit the metal wall

		if (this.live && this.getRect().intersects(w.getRect())) {

		this.live = false;

		this.tc.metalWall.remove(w); // Bullets can now go through metal walls

		return true;

		}

		return false;

		}
Copy the code

Modified effect test: Modified metal walls can also be destroyed

Blood pack effect: The blood pack will move randomly. Here are the two different positions of the blood pack:

Position 1

Position 2

Tanks eat health packs to increase their health

Replenish health after eating health pack:

Run out of blood after being attacked end the game

 

Conclusion:

Here the whole process and function design of the game is almost introduced, this tank war project is suitable for students to use as a reference for Java graduation or course design study.

Through the experiment course design, make originally don’t know anything about Java me understanding of Java knowledge to master, there are also some problems in the experiment has not solve or need a complete functions need to be solved, such as the game can add voice, because when trying to fail, so there is no to implement these functions in the program. Features such as storing game results can be optimized or added, or done at the end, to make the Java language more powerful and continue to be attracted to it.

Download full source code address

JavaSwing series project recommendation:

Based on the design and implementation of JavaSwing ATM system

Design and implementation of library management system based on JavaSwing+mysql

Design and implementation of student association management system based on JavaSwing+mysql

Javascript project updates 4/100

Everyone can like, like, follow and comment on me now