Java makes a simple tank war _java

Source: Internet
Author: User
Tags explode getcolor

For details please refer to the note, here is not much nonsense, to achieve a childhood classic.

Blood.java

Package Com.hkm.TankWar;
Import java.awt.*; /** * Blood clots, our tanks eat can gyrus; * @author hekangmin * */public class Blood {private int x,y,w,h;//the position and width of the blood clot; private T
   
  Ankwarclient TC;
  private int step=0;//records the number of steps the blood clot moves; private Boolean live=true;
  public Boolean islive () {return live;
  public void Setlive (Boolean live) {this.live = live; /** * Record the position of the blood clot; * * Private int[][] pos={{400,300},{400,320},{420,320},{440,300},{440,330},{480,400},{520,
        
  400},{540,400}};
    Public Blood () {x=pos[0][0];
    Y=POS[0][1];
  w=h=18;
     
    public void Draw (Graphics g) {if (!live) return;
    Color C=g.getcolor ();
    G.setcolor (Color.cyan);
    G.filloval (x, Y, W, h);
     
    G.setcolor (c);
  Move ();
    /** * Moving blood clots/public void Move () {step++;
    if (step>=pos.length) step=0;
    else{X=pos[step][0];
    Y=POS[STEP][1]; } public Rectangle GetRect () {return new Rectangle x,Y,W,H); }
   
   
}

Explode.java

Package Com.hkm.TankWar;
Import java.awt.*;
/**
 * Explosion class * @author hekangmin * * */public
class Explode {
  private int x,y;//where
   
  the explosion occurred Private Boolean live=true;
   
  The int dia[]={4,8,12,16,32,40,20,14,4};//is modeled with a garden, representing the diameter of the circle, and the
   
  int step=0;//The difference to the first few diameters
   
  private tankwarclient tc;// Hold reference to public
   
  Explode (int x,int y,tankwarclient tc)
  {
    this.x=x;
    this.y=y;
    THIS.TC=TC;
  }
  public void Draw (Graphics g)
  {
    if (! Live)
    {
      tc.explodes.remove (this);
      return;
    }
    if (step==dia.length)//if to the last diameter explosion dies;
    {
      live=false;
      Step=0;
      return;
    }
    Color C=g.getcolor ();
    G.setcolor (color.yellow);
    G.filloval (x, Y, Dia[step], dia[step]);
    G.setcolor (c);
    step++
  }
   
}

Missile.java

Package Com.hkm.TankWar;
Import java.awt.*;
Import java.awt.event.*;
Import java.awt.event.KeyEvent;
Import java.util.List; /** * @author hekangmin * */public class missile {private int x,y;//bullets location private tank.direction dir;
     
    Tank direction private static final int xspeed=10;//tank x direction moving speed, private static final int yspeed=10;//tank y-direction moving speed,
    public static final int width=10;
     
    public static final int height=10;
     
  Private Boolean live=true;//judge whether the Bullets alive private Boolean good;//distinguish enemy bullets and our bullets private tankwarclient TC;
    Public missile (int x, int y, tank.direction dir) {this.x = x;
    This.y = y;
  This.dir = dir;
    Missile (int x,int y,boolean good,tank.direction dir,tankwarclient tc) {this (x,y,dir);
  this.good=good;//the property of the tank is the same as the bad property of the bullet; THIS.TC=TC; /** * Draw Bullets * @param g is the brush/public void Draw (Graphics g) {if (!
      Live) {tc.missiles.remove (this); ReTurn
    Color C=g.getcolor ();
    if (good) {G.setcolor (Color.Blue);
    else G.setcolor (Color.orange);
    G.filloval (x, Y, WIDTH, HEIGHT);
     
    G.setcolor (c);
  Move ();
      /** * To allow bullets to move in accordance with the direction of the tank * * private void moves () {switch (dir) {case l:x-=xspeed;
    Break
      Case Lu:x-=xspeed;
      Y-=yspeed;
    Break
      Case U:y-=yspeed;
    Break
      Case Ru:x+=xspeed;
      Y-=yspeed;
    Break
      Case R:x+=xspeed;
    Break
      Case Rd:x+=xspeed;
      Y+=yspeed;
    Break
      Case D:y+=yspeed;
    Break
      Case Ld:x-=xspeed;
      Y+=yspeed;
    Break } if (x<0| | y<0| | x>tankwarclient.game_width| |
    Y>tankwarclient.game_height)//The bullet crosses the line to let it die; {live=false;
  } public boolean islive () {return Live;
  Public Rectangle GetRect ()//Get the rectangular area of the bullet; {return new Rectangle (This.x,this.y,this.) Width,this.
  HEIGHT);
    /** * Judge bullets collided with tanks; * @param t for tank * @return return True to indicate a collision, otherwise no collision; * Public boolean Hittank (Tank t) { if (this. Live&&this.getrect (). intersects (T.getrect ()) &&t.islive () &&this.good!=t.isgood ()) {if (t).
        Isgood ()) {T.setlife (T.getlife ()-10); 
      if (T.getlife () <=0) t.setlive (false);
      }else{t.setlive (FALSE); } this.
      live=false;///the bullet to death;
      Explode e=new Explode (X,Y,TC);//explosion; Tc.explodes.add (e);
       
    return true;
  return false; /** * Judge bullets collided with enemy tanks; * @param tanks enemy Tank * @returntrue said the collision, false did not collide; * Public boolean hittanks (List<ta Nk> tanks) {for (int i=0;i<tanks.size (), i++) {if (Hittank (Tc.tanks.get (i))) {return T
      Rue
  return false; /** * To determine whether the bullet hit the wall * @param w Walls * @returntrue, crashed, false, not hit; * Public boolean Hitswall (Wall W) {if (This. live&&this.getrect (). intersects (W.getrect ())) {live=false;
    return true;
  return false; }
   
   
}

Tank.java

Package Com.hkm.TankWar;
Import java.awt.*;
Import java.awt.event.KeyEvent;
Import java.util.*; /** * Tank class * @author hekangmin * * * * * */public class Tank {public static final int xspeed=5;//tank x direction speed public static fi
   
  nal int yspeed=5;
  public static final int width=30;
   
  public static final int height=30;
   
  Private Bloodbar bb=new Bloodbar ();//blood bar private int life=100;
  public int Getlife () {return life;
  public void Setlife (int life) {this.life = life;
   
  private static Random r=new Random ();
   
  private static int Step=r.nextint (12) +3;//defines a number indicating the number of steps of an enemy tank randomly eastward; private Boolean bl=false,bu=false,br=false,bd=false;
   
  The enum direction{l,lu,u,ru,r,rd,d,ld,stop};//defines the direction of the tank using the enumeration type; private int x,y; private int oldx,oldy;//Record the position of the tank on the previous step; private Boolean live=true;//to determine whether or not to live public boolean islive () {return Li
  ve
  public void Setlive (Boolean live) {this.live = live; Private Boolean good;//tank is good or bad publIC Boolean Isgood () {return good;
   
  Private Direction ptdir=direction.d;//added the direction of the barrel;
   
  Tankwarclient tc;//in order to hold each other's references to facilitate access to its member variables; Direction dir=direction.stop;//first set the tank direction to a STOP; public Tank (int x,int Y,boolean good,direction
    ENT tc) {this.x=x;
    This.y=y;
    This.oldx=x;
    This.oldy=y;
    This.good=good;
    This.dir=dir;
  this.tc=tc;//hold each other's references; public void Draw (Graphics g) {if (!live)//If death is no longer draw; {if (!good) {tc.tanks.
        Remove (this); if (Tc.tanks.size () <5)//Less than 5 tanks added tank; {for (int i=0;i<10;i++) {int Posx=r.nex
            TINT (800);
            int Posy=r.nextint (600);
    Tc.tanks.add (New Tank (POSX,POSY,FALSE,DIRECTION.D,TC));//make the position of tank appear random}} return;
    Color C=g.getcolor ();
      if (good) {G.setcolor (color.red);
    Bb.draw (g); else G.setcolor (Color.blacK);
    G.filloval (x, Y, WIDTH, HEIGHT);
     
    G.setcolor (c);
    Switch (PTDIR)//Draw the direction of the barrel;
    {Case L:g.drawline (X+TANK.WIDTH/2, Y+TANK.HEIGHT/2, x-10, Y+TANK.HEIGHT/2);/draw a barrel, draw a straight line instead;
      Case Lu:g.drawline (X+TANK.WIDTH/2, Y+TANK.HEIGHT/2, x-7, y-7);
    Break
      Case U:g.drawline (X+TANK.WIDTH/2, Y+TANK.HEIGHT/2, X+TANK.WIDTH/2, y-10);
    Break
      Case Ru:g.drawline (X+TANK.WIDTH/2, Y+TANK.HEIGHT/2, x+tank.width+7, y-7);
    Break
      Case R:g.drawline (X+TANK.WIDTH/2, Y+TANK.HEIGHT/2, x+tank.width+10, Y+TANK.HEIGHT/2);
    Break
      Case Rd:g.drawline (X+TANK.WIDTH/2, Y+TANK.HEIGHT/2, x+tank.width+7, y+tank.height+7);
    Break
      Case D:g.drawline (X+TANK.WIDTH/2, Y+TANK.HEIGHT/2, X+TANK.WIDTH/2, y+tank.height+10);
    Break
      Case Ld:g.drawline (X+TANK.WIDTH/2, Y+TANK.HEIGHT/2, x-7, y+height+7);
    Break
  } move (); public void Move () {oldx=x;//record TitanOldy=y the position of the previous step;
      Switch (dir) {case l:x-=xspeed;
    Break
      Case Lu:x-=xspeed;
      Y-=yspeed;
    Break
      Case U:y-=yspeed;
    Break
      Case Ru:x+=xspeed;
      Y-=yspeed;
    Break
      Case R:x+=xspeed;
    Break
      Case Rd:x+=xspeed;
      Y+=yspeed;
    Break
      Case D:y+=yspeed;
    Break
      Case Ld:x-=xspeed;
      Y+=yspeed;
    Break
    Case Stop:break;
     
    } if (this.dir!=direction.stop) This.ptdir=this.dir;
    /** * Prevent tank from crossing the line; */if (x<0) x=0;
    if (y<25) y=25;
    if (x+tank.width>tankwarclient.game_width) x=tankwarclient.game_width-30;
     
    if (y+tank.height>tankwarclient.game_height) y=tankwarclient.game_height-30; if (!good) {direction[] dirs=direction.values ();//Converts an enumerated type to an array; if (step==0) {STEP=R.N
        Extint (12) +3;
     int Rn=r.nextint (dirs.length);//To produce a random integer within length;   DIR=DIRS[RN];
      } step--;
    D keypressed (KeyEvent e) {int key=e.getkeycode ();
      Switch (key) {case keyevent.vk_left:bl=true;
    Break
      Case Keyevent.vk_right:br=true;
    Break
       Case Keyevent.vk_up:bu=true;
    Break
      Case Keyevent.vk_down:bd=true;
    Break
  } locationdir ();
    public void keyreleased (KeyEvent e) {int key=e.getkeycode ();
      Switch (key) {case KeyEvent.VK_CONTROL:fire ();
    Break
      Case Keyevent.vk_left:bl=false;
    Break
      Case Keyevent.vk_right:br=false;
    Break
       Case Keyevent.vk_up:bu=false;
    Break
      Case Keyevent.vk_down:bd=false;
    Break
      Case KeyEvent.VK_A:superFire ();
    Break
      Case KeyEvent.VK_F2:reBorn ();
    Break
    }Locationdir ();
    /** * Fired bullets * @return return Bullet type * * Public missile Fire () {if (!live) returns null;
    int mx=this.x+tank.width/2-missile.width/2;//calculates the position of the projectile firing; int MY=THIS.Y+TANK.HEIGHT/2-MISSILE.HEIGHT/2;
    Missile m=new Missile (MX,MY,GOOD,PTDIR,THIS.TC);////fired bullets according to barrel direction tc.missiles.add (m);
  return m;
    Public missile Fire (Direction dir) {if (!live) return null;
    int MX=THIS.X+TANK.WIDTH/2-MISSILE.WIDTH/2;
    int MY=THIS.Y+TANK.HEIGHT/2-MISSILE.HEIGHT/2;
    Missile m=new Missile (MX,MY,GOOD,DIR,THIS.TC);//Fired bullets according to the direction of the tank; Tc.missiles.add (m);
  return m;
    public void Superfire () {direction[] dirs=direction.values ();
    for (int i=0;i<8;i++) {fire (dirs[i]);
    } public void Locationdir () {if (BL&AMP;&AMP;!BU&AMP;&AMP;!BR&AMP;&AMP;!BD) DIR=DIRECTION.L;
    else if (BL&AMP;&AMP;BU&AMP;&AMP;!BR&AMP;&AMP;!BD) dir=direction.lu; else if (!bl&&bu&&!br&AMP;&AMP;!BD) dir=direction.u;
    else if (!BL&AMP;&AMP;BU&AMP;&AMP;BR&AMP;&AMP;!BD) dir=direction.ru;
    else if (!BL&AMP;&AMP;!BU&AMP;&AMP;BR&AMP;&AMP;!BD) DIR=DIRECTION.R;
    else if (!BL&AMP;&AMP;!BU&AMP;&AMP;BR&AMP;&AMP;BD) dir=direction.rd;
    else if (!BL&AMP;&AMP;!BU&AMP;&AMP;!BR&AMP;&AMP;BD) dir=direction.d;
    else if (BL&AMP;&AMP;!BU&AMP;&AMP;!BR&AMP;&AMP;BD) dir=direction.ld;
  else if (!BL&AMP;&AMP;!BU&AMP;&AMP;!BR&AMP;&AMP;!BD) dir=direction.stop; The public Rectangle getrect ()//Gets the rectangular region of tank {return new Rectangle this.x,this.y,this. Width,this.
  HEIGHT);
    /** * Tank Wall * @param w Wall * @returntrue hit, false not hit; * Public boolean Colliedswithwall (Wall W) {
      if (This.live&&this.getrect (). intersects (W.getrect ())) {This.stay ();
    return true;
  return false; /** * Handling tanks collided with tanks to prevent them from crossing each other; * @param tanks enemy Tanks; * @return true hit, false not hit;/public BooleaN Colliedswithtanks (java.util.list<tank> tanks) {for (int i=0;i<tanks.size (); i++) {Tank t=tanks.
      Get (i); 
          if (this!=t) {if (This.live&&this.islive () &&this.getrect (). intersects (T.getrect ())) {
        This.stay ();//Returns the position of the previous step; T.stay ();////returns the position of the previous step return true;
  }} return false;
    private void Stay () {x=oldx;
  Y=oldy; /** * is the inner class of the tank; the blood strips are shown above the heads of our tanks; * @author hekangmin * */Private class Bloodbar {public void
      Draw (Graphics g) {Color c=g.getcolor ();
      G.setcolor (color.red);
      G.drawrect (x,y-10,width,10);
      int w=width*life/100;
    G.fillrect (x,y-10,w,10); }/** * Eat blood clots; * @param b blood clots * @returntrue eat, false not to eat;/public boolean Eat (blood b) {if T
      His.live&&b.islive () &&this.getrect (). intersects (B.getrect ())) {this.life=100; B.sEtlive (FALSE);
    return true;
  return false; /** * The resurrection of our tanks after death; * * public void Reborn () {if (This.isgood () &&!this.islive ()) {This
      . Setlive (True);
    This.setlife (100); }
  }
}

Tankwarclient.java

Package Com.hkm.TankWar;
Import java.awt.*;
Import java.awt.event.*;
Import java.util.List;
 
Import java.util.ArrayList; /** * This is the game window; * @author hekangmin * */public class Tankwarclient extends frame{/** * The width of the game window;/Public stat
   
  IC final int game_width=800;
   
  /** * The height of the game window; * * public static final int game_height=600;
  Tank mytank=new Tank (700,400,true,tank.direction.stop,this);
  List<tank> tanks=new arraylist<tank> ();
  List<explode> explodes=new arraylist<explode> ();
  List<missile> missiles=new arraylist<missile> ();
  Wall w1=new Wall (300,200,20,200,this);
  Wall w2=new Wall (600,300,30,150,this);
   
  Blood B=new blood ();
   
  /** * Draw a virtual picture; * * Image offscreenimage=null;
  The public tankwarclient (String name)//sets the text {super (name); /** * Run window; */public void Launchframe () {for (int i=0;i<10;i++)//Add 10 enemy tanks {tanks. Add (New Tank 50+40* (i+1), 50,FALSE,TANK.DIRECTION.D,This));
    } this.setbounds (200,100,game_width,game_height);
    This.setbackground (Color.green); This.addwindowlistener (New Windowadapter ()//anonymous class {public void windowclosing (WindowEvent e) {syste
      M.exit (0);
    }
    }); This.addkeylistener (New Keymonitor ())//join keyboard listener; this.setresizable (false);//cannot change the size of the window; This.setvisible (True)
     
    ; New Thread (new Paintthread ()). Start ();//create a new thread;} public void Paint (Graphics g) {g.drawstring ("Missile coun
    T: "+missiles.size (), 10, 50);//display string; g.DrawString (" Explodes Count: "+explodes.size (), 10,70);
    g.DrawString ("Tanks Count:" +tanks.size (), 10,90);
    g.DrawString ("Mytank Life:" +mytank.getlife (), 10,110);
    /** * Draw the wall; * * W1.DRAW (g);
     
    W2.draw (g);
      /** * Detect bullets and all kinds of things; * for (int i=0;i<missiles.size (); i++) {missile m=missiles.get (i);
      M.hitswall (W1);
      M.hitswall (W2);
      M.hittanks (tanks); M.hittank(Mytank);
       
      M.draw (g);
      if (!m.islive ())//missiles.remove (m);
    else M.draw (g);
      /** * Draw an explosion; * for (int i=0;i<explodes.size (); i++) {Explode e=explodes.get (i);
    E.draw (g);
      for (int i=0;i<tanks.size (); i++) {Tank t=tanks.get (i);
      T.colliedswithwall (W1);
      T.colliedswithwall (W2);
      T.colliedswithtanks (tanks);
    T.draw (g);
    } B.draw (g);
    Mytank.eat (b);
  Mytank.draw (g);
  /** * Use double buffering technology to eliminate the phenomenon of tank flicker;/public void update (Graphics g)//g as a brush painted on the screen;
    {if (offscreenimage==null) offscreenimage=this.createimage (Game_width, game_height);
    Graphics goffscreen=offscreenimage.getgraphics ();//goffscreen is the brush of offscreenimage;
    Color C=goffscreen.getcolor ();
    Goffscreen.setcolor (Color.green);
    Goffscreen.fillrect (0, 0, game_width, game_height);
    Goffscreen.setcolor (c);
    Paint (Goffscreen);//painting on the virtual picture; G.drawimage (Offscreenimage,0,0,null///////////////////////////////////////////G
        {while (true) {repaint ();//The Repaint method here is the try{thread.sleep (100) of the frame class;
        }catch (interruptedexception e) {e.printstacktrace ();
      "}}} private class Keymonitor extends Keyadapter {public void keyreleased (KeyEvent e) {
    Mytank.keyreleased (e);
      public void keypressed (KeyEvent e) {mytank.keypressed (e); } public static void Main (string[] args) {New tankwarclient (' My Tank World '). Launchfram
  E (); }
   
   
   
   
}

Wall.java

Package Com.hkm.TankWar;
Import java.awt.*;
/**
 * Generate block Wall this class;
 * @author hekangmin * * */Public
 
class Wall {
  /**
   * x,y for the wall position, w,h for width height;
   *
  /int x,y,w,h;
  /**
   * Holding reference * *
  tankwarclient TC;
   
  public Wall (int x, int y, int w, int h, tankwarclient tc) {
    this.x = x;
    This.y = y;
    THIS.W = W;
    This.h = h;
    THIS.TC = TC;
  }
   
  public void Draw (Graphics g)
  {
    Color c=g.getcolor ();
    G.setcolor (Color.gray);
    G.fillrect (x,y,w,h);
    G.setcolor (c);
  }
   
  /**
   * Get the rectangular area of the wall;
   * @return/public
  Rectangle getrect ()
  {return
    new Rectangle (x,y,w,h );
  }
   
}

The above mentioned is the entire content of this article, I hope you can enjoy.

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.