Classical reappearance based on Java Platform Development tank war game _java

Source: Internet
Author: User
Tags gety stub

I. Description of requirements
1. Functional Requirements
in the functional requirements analysis phase, our main task is to specify which services the system must provide, define what functionality the software accomplishes, and provide it to those who use it, which is a fundamental requirement of software development and an essential part of the requirement analysis. Tank War is a classic game, this game learned some predecessors of the experience, overall speaking, the game is divided into two sides, mainly engaged in fighting tanks have player control, enemy tanks can be intelligent random appearance on the screen, and mobile, firing a certain number of bullets; players can move tanks in a defined area, When a bullet hits the player, player dead, game over, enemy tank intelligent operation, enemy tank because of the need to have a certain intelligence, random appearance on the screen, free rotation For example: hit the border to know the steering; the operation of the bullet: the tank has player control, firing bullets according to different tank positions, if hit the target, Will have an explosion, and will disappear on the screen.

2. System Performance requirements
The game is based on the performance of the computer system configuration requirements are guaranteed to make the program fast, stable operation, the runtime can timely response, when the game player tank was destroyed, timely prompts the game failure information, can respond in time to meet the rules of the game needs, and also to ensure that the game, The size of the main window can not be changed arbitrarily, guaranteed to have the ability to play.

3. Function-Solving problems
The game needs code utilization is very good, after all, is a real-time operation, every millisecond will have a lot of bullets fired, as well as a lot of tank coordinates to move, countless times to compare whether the bullet hit the tank, for the keyboard monitoring events, as well as the implementation of threading this interface, drawing redraw refresh , the logical sense needs strong, and the object-oriented display of the incisively and vividly; even the relationship is not clear, it is easy to have an unexpected situation; In order to add beauty to the game, adding to the phenomenon of explosion, then this need to be when the enemy tank died in the outside of the panel to implement a temporary rapid rotation of the local picture To achieve an explosion effect;

Overall for the tank war to complete the basic functions are: graphical interface, enemy tank style differences, tanks can launch bullets attack each other, but can not attack teammates, the tank set a certain life value;

Second, the main function analysis
In the tank war game development process, the realization of the main function, provided to the user to use, so first draw out a tank.
1. Draw the player tank: need to set a piece of paper on the JPanel Panel paint () and brush draw out the approximate shape of the tank;
2. The player's tank can be moved: if tank movement, need to change the tank coordinates, and constantly redraw the panel, but when let the tank move, you can in the Panel's class to implement event monitoring mechanism (keyboard monitoring) interface, when the player press w/d/s/a key can be corresponding up and down moving;
3. and draw the enemy tank on my Paper paint (), because the enemy tank and the player tank in the same interface, need to draw on the same artboard;
4. The player's tank can fire bullets: the player wants to fire a bullet, to have an event source, that is, when the user press the J key, event monitoring when the immediate launch of a bullet, but there is a need to show the user in front of the effect, so the panel above paint () draw a bullet; So do not have to change the coordinates of the bullets, but also in the speed of the bullet appropriate, so need to use the thread to achieve, and let the panel of the continuous redrawing, see the effect;
5. Player bullets can be bursts and send up to five stars: to control the number of bullets, players need to press the J key when the player to come out of the current number of surviving bullets is less than 5, when the conditions, the creation of a bullet, when the player launched a bullet size of more than five, then no longer call the system's firing function Since the firing of a bullet is not only one, so set up a collection of bullets, fire once to produce a bullet, know that the bullets hit the boundary of Death is a collection of overflow bullets;
6. The player tank hits the enemy tank and disappears, and the explosion effect; First of all to determine whether the player tank bullets hit the enemy, so there must be a constant judge hit the enemy function, when the bullets run to the enemy tanks, tank life needs to end, and produce a bomb, and in paint () draw a bomb;
7. Enemy tanks can be smart to move: If the tank can only move, the need to generate a random direction of movement, due to non-stop movement, so the method is written to the thread of the run function, and control the thread sleep to achieve the speed problem;
8. Enemy tanks to be able to fire bullets: in the drawing of the enemy at the same time, the thread set a certain sleep in different directions, different quantities of tank bullets, and in the panel to remove each bullet, and draw;
9. Enemy bullets hit the player, the player disappears: constantly take out the enemy's every bullet, and the player tank for comparison, when the bullets touch the player, the player exploded disappear;

Iii. Outline Design
• Role attribute Settings

Tanks: Tank positions, different types of tank colors, tank life identifiers, tank movement speed, and tanks in different directions;

Player tank: After inheriting the basic properties of the tank, on the basis of the second, the player tank is free to move up and down, and the player tank has the function of firing bullets;

Enemy Tank: After inheriting the basic tank properties, it is necessary to realize the intelligent movement of the enemy tank, and also to launch a bullet thread at different positions of the tank;

Bullet: The bullet must have coordinates the position, the speed, the life value, as well as the bullet should be able to move, as well as the bullet also wants the death function method realization;

Bombs: The coordinates of bombs, life, and the vanishing effect of life methods function;

• Feature property settings

Paper: Paint () should draw the player tank, enemy tank, and show the death tank explosion effect;

Event Monitor: When the user press WDSA, the corresponding player tank should be changed in the direction, when the user launches J is a bullet in the direction of launch, and should always run until death, unless hit the enemy;

Hit tank: When a bullet hits an enemy tank, an explosion is added to the explosion concentration, and the explosion information is drawn onto the face paper;

The enemy hit the player: Take out the game so the enemy, as well as each enemy's bullets and my tank to match, judge whether hit;

Players hit the enemy: remove each player's bullets, with each enemy tank to match, judge whether hit, and will hit the tank-related judgments, put into the thread run () inside the continuous concurrent judgment;

The detailed function analysis diagram is as follows:

• Tank role Attribute Analysis chart

Functional Artboard Analysis Diagram

Overall analysis diagram of tank Xmind

Tank role Attribute Xmind diagram

Tank function Attribute Xmind diagram

Iv. System Implementation
Members.java

 Members.java package Mytank9;

Import java.util.*;
  Class Bomb {//define bomb coordinates int x, y;//bomb life int lives = 9;
  Boolean islive = true;
    public Bomb (int x, int y) {this.x = x;
  This.y = y;
    }//Reduce the life value public void Lifedown () {if (> 0) {life--;
    } else{this.islive = false;
  }} class Shot implements Runnable {int x;
  int y;
  int direct;
  int speed = 1;
  Boolean islive = true;
    public Shot (int x, int y, int direct) {this.x = x;
    This.y = y;
  This.direct = direct;
      public void Run () {while (true) {try{thread.sleep (50);
      }catch (Exception e) {e.printstacktrace ();
          Switch (direct) {case 0:y-=speed;
        Break
          Case 1:x+=speed;
        Break
          Case 2:y+=speed;
        Break
          Case 3:x-=speed;
      Break //Determine if the field touches the edge if (x<0| | x>400| | Y<0| |
        y>300) {this.islive = false;
      Break Class Tank {//tank coordinate int x = 0;//g ordinate int y = 0;////tank direction//0 means on, 1 for right, 2 for 3 for left INT direct
= 0;
Tank speed int speed = 1;
  tank color int color;

  Boolean islive = true;
  public int GetColor () {return Color;
  public void SetColor (int color) {color = color;
  public int getspeed () {return speed;
  The public void setspeed (int speed) {this.speed = speed;
  public int Getdirect () {return direct;
  public void Setdirect (int direct) {this.direct = direct; 
    public Tank (int x, int y) {this.x = x;
  This.y = y;
  public int GetX () {return x;
  public void SetX (int x) {this.x = x;
  public int GetY () {return y;
  The public void sety (int y) {this.y = y; }//Enemy tank class Enemytank extends Tank implements Runnable {int times = 0;//define a vector that can store enemy bullets Vector<shot &GT;SS = new Vector<sHot> (); 
  The enemy added bullets should have just created tanks and enemy tank bullets after the death of public Enemytank (int x, int y) {super (x, y); @Override public void Run () {//TODO auto-generated Method Stub (True) {switch (This.direc T) {case 0://Description tank is going up for (int i = 0; i < i++) {//enemy tank in my
            To move if (y>0) {y-=speed;
            try {thread.sleep (50);
            catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
        }} break; Case 1:for (int i = 0; i < i++) {if (x<400) {X+=speed
            ;
            try {thread.sleep (50);
            catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
         } } break; Case 2:for (int i = 0; i < i++) {if (y<300) {Y+=speed
            ;
            try {thread.sleep (50);
            catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
        }} break; Case 3:for (int i = 0; i < i++) {if (x > 0) {x-=speed
            ;
            try {thread.sleep (50);
            catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
        }} break;
        //Determine if a new bullet this.times++ is required to be added to the tank;
                if (times%2==0) {if (islive) {if (Ss.size () <5) {
              Shot s =null;  Switch (direct) {case 0://Create a bullet s = new Shot (x+10,
  Y, 0);
                  Add the bullet to the vector ss.add (s);
                Break
                  Case 1:s = new Shot (x+30, y+10, 1);
                  Ss.add (s);
                Break
                  Case 2:s = new Shot (x+10, y+30, 2);
                  Ss.add (s);
                Break
                  Case 3:s = new Shot (x, y+10, 3);
                  Ss.add (s);
                Break
                }//Start bullet threads thread t = new thread (s);

              T.start ();
Let the tank randomly generate a new direction this.direct = (int) (Math.random () *4);
        Determine if the enemy tank is dead if (this.islive = = False) {//Let tank die, exit process break; }}//My tank class Hero extends Tank {vector<shot> ss = newVector<shot> ();
  Shot s = null;
  public Hero (int x, int y) {super (x, y); //Fire public void Shotenemy () {switch (this.direct) {case 0://Create a bullet s = new Shot (x+10, y
, 0);
      Add the bullet to the vector ss.add (s);
    Break
      Case 1:s = new Shot (x+30, y+10, 1);
      Ss.add (s);
    Break
      Case 2:s = new Shot (x+10, y+30, 2);
      Ss.add (s);
    Break
      Case 3:s = new Shot (x, y+10, 3);
      Ss.add (s);
    Break
    Thread t = new thread (s);
  T.start ();
  //tank up to move public void MoveUP () {y-=speed;
  //tank to the right move public void MoveRight () {x+=speed;
  public void MoveDown () {y+=speed;
  public void MoveLeft () {x-=speed;

 }
}

Mytankgame4.java

 Mytankgame4.java * * Function: Tank Game 2.0 * 1: Draw tank * 2: My tank can move up and down * 3: Draw Enemy Tanks * 4: My tank can be fired. * 5: Bullets can be fired (up to five bursts) * 6: When my tank hits enemy tanks, the enemy disappeared (exploded * "to determine whether the bullets hit the tank; when to call;" * Explosion: 1 prepare three charts first; 2 defines the bomb class; 3 put bombs on the enemy tanks. Vector 4 Draw * 7: Enemy tanks in my rule fan
Wai Mobile * 8: Enemy Tanks can also send bullets * 9: When enemy tanks hit my tank, my tank disappeared/package Mytank9;
Import java.awt.*;
Import Javax.imageio.ImageIO;
Import javax.swing.*;
Import java.awt.event.*;
Import Java.io.File;
Import java.util.*;
  public class MyTankGame4 extends jframe{Mypanel MP = null; public static void Main (string[] args) {//TODO auto-generated method stub MyTankGame4 mytankgame1 = new Mytankga
  Me4 (); 
    Public MyTankGame4 () {MP = new Mypanel ();
    Thread t = new Thread (MP);
    T.start ();
This.add (MP);
    Register to monitor This.addkeylistener (MP);
    This.setsize (400, 300);
    This.setdefaultcloseoperation (Jframe.exit_on_close);
  This.setvisible (TRUE); } class Mypanel extends JPanel implements keylistener,runnable{//define one of my tanks Hero Hero = NULL;

Define enemy tank vector<enemytank> ETS = new vector<enemytank> ();

Defines a set of bombs vector<bomb> bombs = new vector<bomb> ();
Enemy tank how many int ensize = 3;
  Define three pictures of the switch to form a bomb image image1 = null;
  Image image2 = null;

Image image3 = null; Construct public Mypanel () {hero = new Hero (100,100);//enemy tank initialization for (int i = 0; I <enSize; i++) {//Chuang
      Build enemy Tank object Enemytank et = new Enemytank ((i+1) *50, 0);
      Et.setcolor (0);
Et.setdirect (2);
      Start enemy tank thread t = new thread (ET);

T.start ();
      Give the enemy tanks a shot. Shot s = new Shot (et.x+10,et.y+30,2);
      Et.ss.add (s);
      Thread t2 = new Thread (s);

T2.start ();
    Adding Ets.add (ET);
      } try{Image1 = Imageio.read (New File ("Bomb_1.gif"));
      Image2 = Imageio.read (New File ("Bomb_2.gif"));

    Image3 = Imageio.read (New File ("Bomb_3.gif"));
    }catch (Exception e) {e.printstacktrace (); //Initialize three Pictures//Image1 = ToolkIt.getdefaulttoolkit (). GetImage (Panel.class.getResource ("/bomb_1.gif"));
Image2 = Toolkit.getdefaulttoolkit (). GetImage (Panel.class.getResource ("/bomb_2.gif"));


  Image3 = Toolkit.getdefaulttoolkit (). GetImage (Panel.class.getResource ("/bomb_3.gif"));
    }//re-paint public void paint (Graphics g) {super.paint (g);

G.fillrect (0, 0, 400, 300);
    Draw out your own tank if (hero.islive==true) {This.drawtank (HERO.GETX), Hero.gety (), G, this.hero.direct, 1); ///Remove every bullet from SS and draw for (int i = 0; I  

Five, Test effect

Yellow for player, hit player

The enemy fired bullets.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.