Please refer to the notes for details. I won’t go into too much nonsense here, just realize a childhood classic.
Blood.java
package com.hkm.TankWar;import java.awt.*;/** * Blood clot class, our tank can recover blood by eating it; * @author Hekangmin * */public class Blood { private int x,y,w,h; //The position, width and height of the blood clot; private TankWarClient tc; private int step=0;//Record 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 location 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(); } /** * Move the blood clot*/ 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.*;/** * Explode class* @author Hekangmin * */public class Explode { private int x,y;//The location where the explosion occurred private boolean Live=true; int dia[]={4,8,12,16,32,40,20,14,4}; // Use circle simulation to represent the diameter of the circle; int step=0;//The difference is moved to the diameter private TankWarClient tc;//Holding the reference 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 it reaches the last diameter, it will explode and die; { 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;/** * Bullet class* @author Hekangmin * */ public class Missile { private int x,y;//The position of the bullet private Tank.Direction dir;//The direction of the tank private static final int XSPEED=10;//The moving speed of the tank in the x direction, private static final int YSPEED=10;//The moving speed of the tank in the y direction, public static final int WIDTH=10; public static final int HEIGHT=10; private boolean Live= true; // Determine whether the bullet is alive private boolean good; // Distinguish between enemy bullets and our own bullets private TankWarClient tc; 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,TankWarClient tc) { this(x,y,dir ); this.good=good;//Set the tank's good and bad attributes and the bullet's damage-returning attribute to the same; 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(); } /** * Move the bullet according to the direction of the tank */ private void move() { 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)//If the bullet crosses the boundary, 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); } /** * Determine whether the bullet collides with the tank; * @param t is the tank* @return returns true to indicate a collision, otherwise there is 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;///Set the bullet to death; Explode e=new Explode(x,y,tc);//An explosion occurs; tc.explodes.add(e); return true; } return false; } /** * Determine whether the bullet collided with the enemy tank; * @param tanks enemy tank* @returntrue means collision, false does not collide; */ public boolean hitTanks(List<Tank> tanks) { for(int i=0;i<tanks.size();i++) { if(hitTank(tc.tanks.get(i))) { return true; } } return false; } /** * Determine whether the bullet hits the wall * @param w wall* @returntrue, hit, 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 final 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;//Define a number to represent the number of steps the enemy tank takes to move randomly; private boolean bL=false,bU=false,bR=false,bD=false; enum Direction{L,LU,U,RU,R,RD,D,LD,STOP};//Use enumeration type to define the tank direction; private int x,y; private int oldX,oldY;//record the position of the tank in the previous step; private boolean live=true;//determine whether it is alive public boolean isLive() { return live; } public void setLive(boolean live) { this.live = live; } private boolean good;//Is the tank good or bad? public boolean isGood() { return good; } private Direction ptDir=Direction.D;//Add the direction of the barrel; TankWarClient tc ;//In order to hold the reference of the other party to facilitate access to its member variables; Direction dir=Direction.STOP;//Set the direction of the tank to stop at the beginning; public Tank(int x, int y, boolean good, Direction dir,TankWarClient tc) { this.x=x; this.y=y; this.oldX=x; this.oldY=y; this.good=good; this.dir=dir; this.tc=tc;// Hold the reference of the other party; } public void draw(Graphics g) { if(!live)//If it dies, it will no longer draw; { if(!good) { tc.tanks.remove(this); if(tc.tanks.size()<5)//Add tanks when there are less than 5 tanks; { for(int i=0;i<10;i++) { int posX=r.nextInt(800); int posY =r.nextInt(600); tc.tanks.add(new Tank(posX,posY,false,Direction.D,tc));//Make the location of the 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 the gun barrel and draw a straight line instead; break; 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 the previous position of the tank 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; 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 tanks from crossing the boundary; */ 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();//Convert the enumeration type into an array; if(step==0) { step=r.nextInt(12)+3; int rn=r.nextInt(dirs.length);//Generate a random integer within length; dir=dirs[rn]; } step- -; if(r.nextInt(40)>20) this.fire(); // Make the enemy tank fire bullets; } } /** * Handle key presses* @param e keyboard event; */ public void 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(); } /** * Fire a bullet* @return Returns the bullet type*/ public Missile fire() { if(!live) return null; int mx=this.x+Tank.WIDTH/2-Missile.WIDTH/2;//Calculate the position of the bullet launch; int my=this.y+Tank.HEIGHT/2-Missile.HEIGHT/2; Missile m=new Missile (mx,my,good,ptDir,this.tc);////Launch bullets according to the direction of the barrel 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);//Launch 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&&!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; } public Rectangle getRect()//Get the rectangular area of the tank{ return new Rectangle(this.x,this.y,this.WIDTH,this.HEIGHT); } /** * The tank hit the wall* @param w wall* @returntrue hit, false did not hit; */ public boolean colliedsWithWall(Wall w ) { if(this.live&&this.getRect().intersects(w.getRect())) { this.stay(); return true; } return false; } /** * Handle tank-tank collisions to prevent them from crossing each other; * @param tanks enemy tanks; * @return true if they collide, false if they do not; */ 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();//Return to the position of the previous step; t.stay();////Return The position of the previous step return true; } } } return false; } private void stay() { x=oldX; y=oldY; } /** * It is the internal class of Tank; the blood bar is displayed on the top of our tank's head; * @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 and add blood; * @param b blood clots* @returntrue if eaten, false if not eaten; */ 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; } /** * Our tank will be resurrected 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 running window of the game; * @ author Hekangmin * */public class TankWarClient extends Frame{/** * Width of the game window; */ public static 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; public TankWarClient( String name)//Set text { super(name); } /** * Run window; */ public void launchFrame() { for(int i=0;i<10;i++)//Add ten 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) { System.exit(0); } }); this.addKeyListener(new KeyMonitor());//Add keyboard monitor; this.setResizable(false) ;//The size of the window cannot be changed; this.setVisible(true); new Thread(new PaintThread()).start();//Create a new thread; } public void paint(Graphics g) { g.drawString("Missile count: "+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 various events; */ 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 explosions; */ 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 flickering; */ public void update(Graphics g) //g is the brush drawn 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);//Draw on the virtual picture; g.drawImage(OffScreenImage,0,0,null);//Use the g brush to draw things on the virtual picture on the screen} private class PaintThread implements Runnable{ public void run() { while(true) { repaint();//The repaint method here is the try{ of the Frame class Thread.sleep(100); }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").launchFrame(); } }
Wall.java
package com.hkm.TankWar;import java.awt.*;/** * Generate the obstacle wall class; * @author Hekangmin * */ public class Wall { /** * x, y is the position of the wall, w, h is the width and height; */ int x,y,w,h; /** * holds the 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 is the entire content of this article, I hope you all like it.