Tank war of Java curriculum design

Time:2021-4-17

This example for you to share the Java tank war specific code, for your reference, the specific content is as follows

Environmental requirements:

  • Operating system: Windows 10
  • Java virtual machine: above JDK1.8
  • Development environment: Eclipse (above 4.5)

Function tips:

1. The game should have a graphical user interface, which can reflect all the details of the game.
2. There should be tanks, walls, woods and rivers in the interface.
3. There should be a “home” in the interface, and if “home” is attacked, it will lose the game.
There are two kinds of tanks, the enemy and our side.
5. Walls are also divided into two types, ordinary walls that can be penetrated and iron walls that cannot be penetrated.
6. There is only one kind of tree and river, which can be passed by forest tank.
7. The tank can fire bullets, and the bullet attributes of the enemy and us are the same.
8. Our bullets can kill enemy tanks, and enemy bullets can also kill our tanks, but it takes multiple bullets to kill them.
9. Enemies cannot kill each other.
10. A bullet must explode when it hits a tank, but it cannot explode when it hits a wall.
11. We can eat blood to increase life.
12. The game can pause, restart, help and other functions.
13. The game levels are: Level 1, level 2, level 3 and level 4.

Demand analysis:

  • Design a graphical user interface, all the elements of the game can be used in this interface, the interface can accept the user’s operation, with human-computer interaction function. Users can choose to restart the game, exit the game, pause the game and find help for the game.
  • The interface contains tanks, trees, rivers, barrier walls and the “home” that game users want to protect.
  • Tanks: there are two types of tanks: enemy tanks and user controlled tanks. Both the user side and the enemy tank can fire bullets, and they can change the path of walking. When they encounter obstacles such as walls and the boundary of the game, they should change the direction, instead of standing on the obstacles all the time. Tanks can’t cross each other. They will change direction automatically after collision.
  • Tree: the interface should include trees as masking objects and modifying objects, so as to increase the elements of the game and make the game more user-friendly. There is no limit to the number of trees. The number is determined by the clear and beautiful interface of the game.
  • River: River should be included in the interface. The function of river is the same as that of forest, and the number of rivers is unlimited. The number is determined by the overall beauty of the game interface.
  • Wall: there are two kinds of walls: ordinary wall and iron wall. Ordinary wall will be damaged when it is attacked by bullets, while iron wall can prevent bullets from passing through. Tanks on both sides can’t pass through both walls, but they can destroy ordinary walls. The number of ordinary walls should be reasonably arranged with other elements, and the iron wall should not be too much, otherwise the game is too difficult, and even the game can’t go on because the tank can’t pass through the iron wall.
  • “Home”: in addition to a tank, the user also has a home to protect. The home is surrounded by ordinary walls. When the home is attacked by bullets, the game ends immediately, and the user loses the game.
  • Bullet: the bullet can be fired by the enemy and the user, and the shot can move in a straight line until it meets the obstacles and disappears. The bullet can cross the woods and rivers. Enemy tanks will explode when attacked by a bullet, resulting in death. After death, the tanks will disappear. The user’s life will be reduced after being attacked by bullets, and will die after being attacked four times. At this time, if there are enemy tanks, the user will lose the game.
  • Explosion: when the bullet hits the tank, it will produce an explosive effect.
  • Direction: both the tank and the bullet have directions. You can choose up, down, left and right directions, and the direction of the bullet is subordinate to the direction of the tank.  

The effect is shown in the figure

The main interface code is as follows:

public TankClient() {
 // printable = false;
 //Creating menus and menu options
 jmb = new MenuBar();
 Jm1 = new menu ("game");
 JM2 = new menu;
 JM3 = new menu;
 JM4 = new menu ("game level");
 jm1.setFont(new Font("TimesRoman",  Font.BOLD , 15); // set the font for menu display
 jm2.setFont(new Font("TimesRoman",  Font.BOLD , 15); // set the font for menu display
 jm3.setFont(new Font("TimesRoman",  Font.BOLD , 15); // set the font for menu display
 jm4.setFont(new Font("TimesRoman",  Font.BOLD , 15); // set the font for menu display
 
 Jmi1 = new MenuItem;
 Jmi2 = new MenuItem ("exit");
 Jmi3 = new MenuItem ("pause");
 Jmi4 = new MenuItem ("continue");
 Jmi5 = new MenuItem ("Game Description");
 Jmi6 = new MenuItem ("level 1");
 Jmi7 = new MenuItem ("level 2");
 Jmi8 = new MenuItem ("level 3");
 Jmi9 = new MenuItem ("level 4");
 jmi1.setFont(new Font("TimesRoman", Font.BOLD, 15));
 jmi2.setFont(new Font("TimesRoman", Font.BOLD, 15));
 jmi3.setFont(new Font("TimesRoman", Font.BOLD, 15));
 jmi4.setFont(new Font("TimesRoman", Font.BOLD, 15));
 jmi5.setFont(new Font("TimesRoman", Font.BOLD, 15));
 
 jm1.add(jmi1);
 jm1.add(jmi2);
 jm2.add(jmi3);
 jm2.add(jmi4);
 jm3.add(jmi5);
 jm4.add(jmi6);
 jm4.add(jmi7);
 jm4.add(jmi8);
 jm4.add(jmi9);
 
 jmb.add(jm1);
 jmb.add(jm2);
 
 jmb.add(jm4);
 jmb.add(jm3);
 
 jmi1.addActionListener(this);
 jmi1.setActionCommand("NewGame");
 jmi2.addActionListener(this);
 jmi2.setActionCommand("Exit");
 jmi3.addActionListener(this);
 jmi3.setActionCommand("Stop");
 jmi4.addActionListener(this);
 jmi4.setActionCommand("Continue");
 jmi5.addActionListener(this);
 jmi5.setActionCommand("help");
 jmi6.addActionListener(this);
 jmi6.setActionCommand("level1");
 jmi7.addActionListener(this);
 jmi7.setActionCommand("level2");
 jmi8.addActionListener(this);
 jmi8.setActionCommand("level3");
 jmi9.addActionListener(this);
 jmi9.setActionCommand("level4");
 
 this.setMenuBar (JMB); // put the menu bar on JFrame
 this.setVisible(true);
 
 For (int i = 0; I < 10; I + +) {// the pattern of family
  if (i < 4)
  homeWall.add(new CommonWall(350, 580 - 21 * i, this));
  else if (i < 7)
  homeWall.add(new CommonWall(372 + 22 * (i - 4), 517, this));
  else
  homeWall.add(new CommonWall(416, 538 + (i - 7) * 21, this));
 
 }
 
 for (int i = 0; i < 32; i++) {
  if (i < 16) {
  otherWall.add (New commonwall (220 + 20 * I, 300, this)); // common wall layout
  otherWall.add(new CommonWall(500 + 20 * i, 180, this));
  otherWall.add(new CommonWall(200, 400 + 20 * i, this));
  otherWall.add(new CommonWall(500, 400 + 20 * i, this));
  } else if (i < 32) {
  otherWall.add(new CommonWall(220 + 20 * (i - 16), 320, this));
  otherWall.add(new CommonWall(500 + 20 * (i - 16), 220, this));
  otherWall.add(new CommonWall(220, 400 + 20 * (i - 16), this));
  otherWall.add(new CommonWall(520, 400 + 20 * (i - 16), this));
  }
 }
 
 For (int i = 0; I < 20; I + +) {// metal wall layout
  if (i < 10) {
  metalWall.add(new MetalWall(140 + 30 * i, 150, this));
  metalWall.add(new MetalWall(600, 400 + 20 * (i), this));
  } else if (i < 20)
  metalWall.add(new MetalWall(140 + 30 * (i - 10), 180, this));
  else
  metalWall.add(new MetalWall(500 + 30 * (i - 10), 160, this));
 }
 
 For (int i = 0; I < 4; I + +) {// tree layout
  if (i < 4) {
  trees.add(new Tree(0 + 30 * i, 360, this));
  trees.add(new Tree(220 + 30 * i, 360, this));
  trees.add(new Tree(440 + 30 * i, 360, this));
  trees.add(new Tree(660 + 30 * i, 360, this));
  }
 
 }
 
 theRiver.add(new River(85, 100, this));
 
 For (int i = 0; I < 20; I + +) {// initialize 20 tanks
  If (I < 9) // set the position where the tank appears
  tanks.add(new Tank(150 + 70 * i, 40, false, Direction.D, this));
  else if (i < 15)
  tanks.add(new Tank(700, 140 + 50 * (i - 6), false, Direction.D,
   this));
  else
  tanks
   .add(new Tank(10, 50 * (i - 12), false, Direction.D,
    this));
 }
 
 this.setSize (Fram_ width, Fram_ Length); // set the interface size
 this.setLocation (280, 50); // set the position of the interface
 this
  . settitle ("tank war -- (restart: R key fire: F key)";
 
 this.addWindowListener (New windowadapter () {// window listening is closed
   public void windowClosing(WindowEvent e) {
   System.exit(0);
   }
  });
 this.setResizable(false);
 this.setBackground(Color.GREEN);
 this.setVisible(true);
 
 this.addKeyListener (New keymonitor()); // keyboard monitoring
 New thread (New paintthread()). Start(); // thread starts
 }
 
 public static void main(String[] args) {
 New tankclient(); // instantiate
 }

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]