Java-based tank war instances and java tank war instances

Source: Internet
Author: User
Tags getcolor

Java-based tank war instances and java tank war instances
Originality statement

The source of this blog post is workshop. Author of this article original, mailbox zhujunxxxxx@163.com, if you have any questions, please contact the author

Preface I haven't touched java for a long time. Today I suddenly found out the source code of a tank war game written during the sophomore period. Then I ran it and it turned out to be so friendly, I suddenly thought of a little bit of time in my sophomore year. Okay. Let's give a picture first.
This is the interface of the entire game. The interface is all painted in java, not very nice. If you want to make a good-looking image version, the basic principle of the game is to create N enemy instances at the beginning, multithreading moves in the interface (the movement is implemented by rewriting the paint function), and then moves and emits bullets randomly. In addition, a simple collision detection method is used to achieve the dual buffering technology of bullet hitting other people and encountering obstacles, eliminating frequent flashes.
Tanks and tanks are the main objects of this game. They can be divided into three types of tanks: their own tanks and general tanks, the other is the BOSS tank (which has a lot of damage), but it is implemented by a class.
Import java. awt. *; import java. awt. event. *; import java. util. *; public class Tank {public static final int XSPEED = 5; public static final int YSPEED = 5; public static final int WIDTH = 30; public static final int HEIGHT = 30; tankClient tc; private int x, y; // tank coordinate, that is, location private int oldX, oldY; // used to save the coordinates of the previous step, private boolean bL = false, bU = false, bR = false, bD = false when hitting the wall; // The Direction Flag variable enum Direction {L, LU, U, RU, R, RD, D, LD, STOP}; // enumeration in the Direction private Direction dir = Direction. STOP; // Our tank. The initial Direction is private Direction ptDir = Direction. d; // The initial direction of the enemy tank, down private int step = r. nextInt (12) + 3; private boolean isSuper = false; public boolean isSuper () {return isSuper;} public void setSuper (boolean isSuper) {this. isSuper = isSuper;} private static Random r = new Random (); // Random number generator private boolean good; // distinguished variable private boolean boss; Public boolean isBoss () {return boss;} public boolean isGood () {return good;} public void setGood (boolean good) {this. good = good;} private BloodBar bb = new BloodBar (); // blood bar // variable of tank life value private int life = 100; public int getLife () {return life;} public void setLife (int life) {this. life = life;} // The tank life and Death variable private boolean live = true; public boolean isLive () {return live;} public void setLive (boolean live) {this. live = liv E;} // Tank constructor public Tank (int x, int y, boolean good) {this. x = x; this. y = y; this. oldX = x; this. oldY = y; this. good = good;} public Tank (int x, int y, boolean good, Direction dir, TankClient tc) {this (x, y, good); this. dir = dir; this. tc = tc;} public Tank (int x, int y, boolean good, boolean boss, Direction dir, TankClient tc) {this (x, y, good); this. dir = dir; this. boss = boss; this. tc = tc;} // Method for drawing tanks public void draw (Grap Hics g) {if (! Live) return; // If Tank is dead, Color c = g will not be re-painted. getColor (); if (good) // set the color {g. setColor (Color. CYAN);} else if (boss) {g. setColor (Color. RED);} else {g. setColor (Color. BLUE);} g. fillOval (x, y, WIDTH, HEIGHT); g. setColor (c); if (good) bb. draw (g); // Our tank has blood strip if (boss) bb. draw (g); // The boss has a blood bar switch (ptDir) {// determine the position through the orientation, draw the barrel position case L: g. drawLine (x + Tank. WIDTH/2, y + Tank. HEIGHT/2, x, y + Tank. HEIGHT/2); break; case LU: g. drawLine (x + Ta Nk. WIDTH/2, y + Tank. HEIGHT/2, x, y); break; case U: g. drawLine (x + Tank. WIDTH/2, y + Tank. HEIGHT/2, x + Tank. WIDTH/2, y); break; case RU: g. drawLine (x + Tank. WIDTH/2, y + Tank. HEIGHT/2, x + Tank. WIDTH, y); break; case R: g. drawLine (x + Tank. WIDTH/2, y + Tank. HEIGHT/2, x + Tank. WIDTH, y + Tank. HEIGHT/2); break; case RD: g. drawLine (x + Tank. WIDTH/2, y + Tank. HEIGHT/2, x + Tank. WIDTH, y + Tank. HEIGHT); break; cas E D: g. drawLine (x + Tank. WIDTH/2, y + Tank. HEIGHT/2, x + Tank. WIDTH/2, y + Tank. HEIGHT); break; case LD: g. drawLine (x + Tank. WIDTH/2, y + Tank. HEIGHT/2, x, y + Tank. HEIGHT); break;} move (); // when re-painting, move} // The method of moving the tank. When re-painting, move void move () {this. oldX = x; this. oldY = y; 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; brea K; 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) {// position of the barrel, which is consistent with the action direction. this. ptDir = this. dir ;}// determine whether the Tank is out of bounds. if (x <0) x = 0 when the boundary is reached, stop if (x <0) x = 0; if (y <30) y = 30; if (x + Tank. WIDTH> TankClient. GAME_WIDTH) x = TankClient. GAME_WIDTH-Tank. WIDTH; if (y + Tank. HEIGHT> TankClient. GAME_HEIGHT) y = TankClient. GAME_HEIGHT-Tank. HEIGHT; // random movement of bad guy tanks, and the time when the bullet was issued if (! Good &&! Boss) {Direction [] dirs = Direction. values (); if (step = 0) {step = r. nextInt (12) + 3; int rn = r. nextInt (dirs. length); dir = dirs [rn];} step --; if (r. nextInt (40)> 38) this. fire () ;}// attack method of the boss tank if (! Good & boss) {Direction [] dirs = Direction. values (); if (step = 0) {step = r. nextInt (12) + 3; int rn = r. nextInt (dirs. length); dir = dirs [rn];} step --; if (r. nextInt (40)> 20) this. fire () ;}// when the key is pressed, set the key flag to determine the direction of positioning public void keyPressed (KeyEvent e) {int key = e. getKeyCode (); switch (key) {case KeyEvent. VK_CAPS_LOCK: this. setSuper (! This. isSuper (); case KeyEvent. VK_F2: if (! This. live) {this. live = true; this. life = 100;} case KeyEvent. VK_CONTROL: fire (); break; case KeyEvent. VK_LEFT: bL = true; break; case KeyEvent. VK_UP: bU = true; break; case KeyEvent. VK_RIGHT: bR = true; break; case KeyEvent. VK_DOWN: bD = true; break;} locateDirection (); // locate the direction after each button.} // locate the direction of the Tank, determine void locateDirection () {if (bL &&! BU &&! BR &&! BD) dir = Direction. L; else if (bL & bU &&! BR &&! BD) dir = Direction. LU; else if (! BL & bU &&! BR &&! BD) dir = Direction. U; else if (! BL & bU & bR &&! BD) dir = Direction. RU; else if (! BL &&! BU & bR &&! BD) dir = Direction. R; else if (! BL &&! BU & bR & bD) dir = Direction. RD; else if (! BL &&! BU &&! BR & bD) dir = Direction. D; else if (bL &&! BU &&! BR & bD) dir = Direction. LD; else if (! BL &&! BU &&! BR &&! BD) dir = Direction. STOP;} // when the direction key is raised, the key position flag is restored, and the public void keyReleased (KeyEvent e) {int key = e. getKeyCode (); switch (key) {case KeyEvent. VK_LEFT: bL = false; break; case KeyEvent. VK_UP: bU = false; break; case KeyEvent. VK_RIGHT: bR = false; break; case KeyEvent. VK_DOWN: bD = false; break; case KeyEvent. VK_A: superFire (); break;} locateDirection ();} // Method for issuing a bullet, add the public Missile fire () {if (! Live) return null; int x = this. x + Tank. WIDTH/2-Missile. WIDTH/2; int y = this. y + Tank. HEIGHT/2-Missile. HEIGHT/2; Missile m; if (good & this. isSuper () // determine if it is a super shell {m = new Missile (x, y, good, ptDir, this. tc, true);} else {m = new Missile (x, y, good, ptDir, this. tc);} tc. missiles. add (m); return m ;}// SuperFire Method for issuing a bullet to each Direction public Missile fire (Direction Dir) {if (! Live) return null; int x = this. x + Tank. WIDTH/2-Missile. WIDTH/2; int y = this. y + Tank. HEIGHT/2-Missile. HEIGHT/2; Missile m = new Missile (x, y, good, Dir, this. tc); tc. missiles. add (m); return m;} private void superFire () {Direction [] dirs = Direction. values (); for (int I = 0; I <8; I ++) {// tc. missiles. add (fire (dirs [I]); fire (dirs [I]) ;}// public Rectangle getRect () required to determine the collision method () {return new Rectangle (x, y, WIDTH, H EIGHT);} // private void stay () {x = oldX; y = oldY ;} // public boolean collidesWithWall (Wall w) {if (this. live & this. getRect (). intersects (w. getRect () {this. stay (); return true;} return false;} // public boolean collidesWithTanks (java. util. list <Tank> tanks) {for (int I = 0; I <tanks. size (); I ++) {Tank t = tanks. get (I); if (this! = T) {if (this. live & t. isLive () & this. getRect (). intersects (t. getRect () {this. stay (); t. stay (); return true ;}}return false;} // private class BloodBar {public void draw (Graphics g) {Color c = g. getColor (); g. setColor (Color. orange); g. drawRect (x, y-10, WIDTH, 10); int w = WIDTH * life/100; g. fillRect (x, y-10, w, 10); g. setColor (c) ;}}// public boolean eat (Blood B) {if (this. live & B. isLive () & this. getRect (). intersects (B. getRect () {this. life = 100; B. setLive (false); return true;} return false ;}}

There are many operations inside the tank class, and there is a blood bar.
Bullets are classified into enemy bullets and our bullets. They have the attributes of moving speed and collision detection functions.
Import java. awt. *; import java. util. list; public class Missile {public static final int XSPEED = 10; // variable of bullet speed public static final int YSPEED = 10; public static int WIDTH = 10; // The bullet size is public static int HEIGHT = 10; int x, y; private TankClient tc; Tank. direction dir; // The Direction variable private boolean isSuper; private boolean live = true; // The Life and Death variable public boolean isLive () {return live;} private boolean good; // public variable for good or bad tanks Boolean isGood () {return good;} // two construction methods of Missile public Missile (int x, int y, Tank. direction dir) {this. x = x; this. y = y; this. dir = dir;} public Missile (int x, int y, boolean good, Tank. direction dir, TankClient tc) {this (x, y, dir); this. tc = tc; this. good = good;} public Missile (int x, int y, boolean good, Tank. direction dir, TankClient tc, boolean isSuper) {this (x, y, dir); this. tc = tc; this. isSuper = isSupe R; this. good = good;} // Method for drawing bullets public void draw (Graphics g) {if (! Live) {tc. missiles. remove (this); return;} Color c = g. getColor (); if (good & isSuper) // The shell becomes larger {g. setColor (Color. YELLOW); g. fillOval (x-5, y-5, 20, 20);} else if (good &&! IsSuper) {g. setColor (Color. YELLOW); g. fillOval (x, y, WIDTH, HEIGHT);} else {g. setColor (Color. RED); g. fillOval (x, y, WIDTH, HEIGHT);} g. setColor (c); move (); // The moving method of the bullet when drawing the bullet} // The moving method of the bullet, which is related to the location of the tank private void move () {switch (dir) {// determine the speed of 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 + = Y SPEED; break; case D: y + = YSPEED; break; case LD: x-= XSPEED; y + = YSPEED; break; case STOP: break ;} // if (x <0 | y <0 | x> TankClient when the bullet crosses the border. GAME_WIDTH | y> TankClient. GAME_HEIGHT) {live = false; tc. missiles. remove (this) ;}} public Rectangle getRect () {return new Rectangle (x, y, WIDTH, HEIGHT);} // method of collision between a bullet and a bullet, and the bullets are classified into good or bad public boolean hitMissile (Missile m) {if (this. live & this. getRect (). intersects (m. getRect () & M. isLive () & this. good! = M. isGood () {this. live = false; m. live = false; return true;} return false;} public boolean hitMissiles (List <Missile> missiles) {for (int I = 0; I <missiles. size (); I ++) {if (hitMissile (missiles. get (I) {// call the hitMissile method missiles. remove (missiles. get (I); return true ;}} return false;} public boolean hitTank (Tank t) {if (this. live & this. getRect (). intersects (t. getRect () & t. isLive () & this. good! = T. isGood () {if (t. isGood () // you are hit to reduce blood {t. setLife (t. getLife ()-20); if (t. getLife () <= 0) t. setLive (false);} else if (t. isBoss () // The BOSS is hit {if (this. isSuper) // hit {t. setLife (t. getLife ()-33); if (t. getLife () <= 0) t. setLive (false);} else {t. setLife (t. getLife ()-5); if (t. getLife () <= 0) t. setLive (false) ;}} else // the bad guys directly die t. setLive (false); // t. setLive (false); this. live = false; Explode e = new Explode (x, y, tc); tc. explodes. add (e); return true;} return false;} // The Bullet collided with the tank, and the bullet score is good or bad (the method of hitting the enemy tank) public boolean hitTanks (List <Tank> tanks) {for (int I = 0; I <tanks. size (); I ++) {if (hitTank (tanks. get (I) {// call the hitTank method tanks. remove (tanks. get (I); return true ;}} return false ;}// METHOD OF COLLISION BETWEEN bullets and the Wall public boolean hitWall (Wall w) {if (this. live & this. getRect (). intersects (w. getRect () {this. live = false; return true;} return false ;}}

Main Program class main class is mainly to achieve the display interface function, draw out the objects in the memory, to achieve the animation effect.
Import java. awt. *; import java. awt. event. *; import java. util. list; import java. util. arrayList; import org. omg. CORBA. PUBLIC_MEMBER; public class TankClient extends Frame {public static int GAME_WIDTH = 800; public static int GAME_HEIGHT = 600; Tank myTank = new Tank (200,300, true, Tank. direction. STOP, this); Tank BossTank = new Tank (60, 50, false, true, Tank. direction. d, this); List <Missile> missiles = new ArrayList <Missile> (); // List of containers loaded with bullets <Explode> explodes = new ArrayList <Explode> (); // List of containers for loading explosion <Tank> tanks = new ArrayList <Tank> (); // The container for loading enemy tanks Blood B = new Blood (); // instantiate a blood clot // instantiate two walls Wall w1 = new Wall (300,200, 20,150, this); Wall w2 = new Wall (200,100, 20,150, this ); // Double Buffer technology, eliminating frequent Image offScreenImage = null; public void update (Graphics g) {if (offScreenImage = null) {offScreenImage = this. createImage (GAME_WIDTH, GAME_HEIGHT);} Graphics gOffScreen = offScreenImage. getGraphics (); Color c = gOffScreen. getColor (); gOffScreen. setColor (Color. gray); gOffScreen. fillRect (0, 0, GAME_WIDTH, GAME_HEIGHT); gOffScreen. setColor (c); paint (gOffScreen); g. drawImage (offScreenImage, 0, 0, null);} public void paint (Graphics g) // This method is automatically called {g. drawString ("tanks count:" + tanks. size (), 10, 35); g. drawString ("explodes count:" + explodes. size (), 10, 50); g. drawString ("missiles count:" + missiles. size (), 10, 65); g. drawString ("Tank life:" + myTank. getLife (), 10, 80); if (tanks. size () <= 0) {for (int I = 0; I <5; I ++) {tanks. add (new Tank (50 + 40 * (I + 1), 50, false, Tank. direction. d, this) ;}/// draw all the bullets in the container for (int I = 0; I <missiles. size (); I ++) {Missile m = missiles. get (I); m. hitTanks (tanks); // Method for adding a bullet to the tank. hitTank (myTank); m. hitTank (BossTank); m. hitWall (w1); m. hitWall (w2); m. hitMissiles (missiles); // call the bullet hitting method m. draw (g) ;}// draw all the explosions in the container for (int I = 0; I <explodes. size (); I ++) {Explode e = explodes. get (I); e. draw (g); // e. explodehitTanks (tanks); // e. explodehitTank (BossTank);} // draw all the tanks in the container for (int I = 0; I <tanks. size (); I ++) {Tank t = tanks. get (I); t. collidesWithWall (w1); t. collidesWithWall (w2); t. collidesWithTanks (tanks); t. draw (g);} B. draw (g); // draw BossTank from the blood clot. draw (g); BossTank. collidesWithWall (w1); BossTank. collidesWithWall (w2); BossTank. collidesWithTanks (tanks); // The method myTank is drawn and added. draw (g); myTank. eat (B); myTank. collidesWithWall (w1); myTank. collidesWithWall (w2); // draw the wall to w1.draw (g); w2.draw (g);} // initialize the game window public void lauchFrame () {// initialize the tank, add to container for (int I = 0; I <10; I ++) {tanks. add (new Tank (50 + 40 * (I + 1), 50, false, Tank. direction. d, this);} this. setLocation (300,100); this. setSize (GAME_WIDTH, GAME_HEIGHT); this. setTitle ("zz TankWar"); this. addWindowListener (new WindowAdapter () {public void windowClosing (invalid wevent e) {System. exit (0) ;}}); this. setBackground (Color. GREEN); this. setResizable (false); this. addKeyListener (new KeyMonitor (); this. setVisible (true); new Thread (new PaintThread ()). start ();} private class PaintThread implements Runnable {public void run () {while (true) {repaint (); try {Thread. sleep (50);} catch (InterruptedException e) {e. printStackTrace () ;}}} private class KeyMonitor extends KeyAdapter {// keyboard listener public void keyReleased (KeyEvent e) {myTank. keyReleased (e);} public void keyPressed (KeyEvent e) {myTank. keyPressed (e) ;}} public static void main (String [] args) {TankClient t = new TankClient (); t. lauchFrame ();}}


To sum up, the game of tank wars is very simple. As long as you understand some basic data structures and collision detection, and redraw methods, you can create your own tank wars and give your childhood a magnificent transformation.

I need a java version of the tank war instance, but this tank war must be a little more complex and cannot be me

373557411 (xulong_4180@qq.com) qq mail has been sent! Check.

I found the source code on the Internet, but there is no document.

It's a horse soldier.

 

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.