Tank war is a classic game in the era of red and white computers when we were young , See a lot of small partners are using a variety of languages to achieve a bit , Hand itch , Also used java Do a relatively simple tank war , Mainly for those who have learned Java The crowd , With someone who's been learning for a while , It is conducive to the improvement of object-oriented thinking , I recommend it to you .

Please refer to the note for details , There's no more nonsense here , It's just a childhood classic .

Blood.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.hkm.TankWar;
import java.awt.*;
/**
  * Blood clots , Our tanks can recover blood after eating ;
  * @author Hekangmin
  *
  */
public class Blood {
   private int x,y,w,h; // The location, width and height of the clot ;
    
   private TankWarClient tc;
    
   private int step= 0 ; // Record the number of steps the clot moves ;
    
   private boolean live= true ;
   public boolean isLive() {
     return live;
   }
  
   public void setLive( boolean live) {
     this .live = live;
   }
     /**
      * Record the location of the 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.hkm.TankWar;
import java.awt.*;
/**
  * Explosion
  * @author Hekangmin
  *
  */
public class Explode {
   private int x,y; // The location of the explosion
    
   private boolean Live= true ;
    
   int dia[]={ 4 , 8 , 12 , 16 , 32 , 40 , 20 , 14 , 4 }; // Use garden simulation , Represents the diameter of the circle ;
    
   int step= 0 ; // The difference moves to the diameter
    
   private TankWarClient tc; // Hold quotes
    
   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 goes to the last diameter, it explodes and 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.hkm.TankWar;
import java.awt.*;
import java.awt.Event.*;
import java.awt.event.KeyEvent;
import java.util.List;
/**
  * Bullets
  * @author Hekangmin
  *
  */
  public class Missile {
     private int x,y; // The location of the bullet
     private Tank.Direction dir; // Tank direction
      
     private static final int XSPEED= 10 ; // tanks x The speed of movement in the direction ,
     private static final int YSPEED= 10 ; // tanks y The speed of movement in the direction ,
      
     public static final int WIDTH= 10 ;
     public static final int HEIGHT= 10 ;
      
     private boolean Live= true ; // Judge whether the bullet is alive
      
     private boolean good; // Distinguish between 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;
   }
    
   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 or bad attribute and bullet's bad attribute to the same ;
     this .tc=tc;
   }
    
   /**
    * Draw the bullet
    * @param g For 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 bullets 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 line, it will kill him ;
     {
       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 whether the bullet collides with the tank ;
    * @param t For tanks
    * @return return true It means that there is 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); // Explosion ;
       tc.explodes.add(e);
       return true ;
        
     }
     return false ;
   }
   /**
    * Judge whether the bullet collided with the enemy tank ;
    * @param tanks Enemy tanks
    * @returntrue It means a collision ,false There was no collision ;
    */
   public boolean hitTanks(List<Tank> tanks)
   {
     for ( int i= 0 ;i<tanks.size();i++)
     {
       if (hitTank(tc.tanks.get(i)))
       {
         return true ;
       }
     }
      
     return false ;
   }
    
   /**
    * Judge whether the bullet hit the wall
    * @param w wall
    * @returntrue, Hit ,false, Didn't hit ;
    */
   public boolean hitsWall(Wall w)
   {
     if ( this .Live&& this .getRect().intersects(w.getRect()))
     {
       Live= false ;
       return true ;
     }
     return false ;
   }
    
    
}

Tank.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package com.hkm.TankWar;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.*;
/**
  * Tanks
  * @author Hekangmin
  *
  */
public class Tank {
   public static final int XSPEED= 5 ; // tanks 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 strip
    
   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 random steps of enemy tanks ;
    
   private boolean bL= false ,bU= false ,bR= false ,bD= false ;
    
   enum Direction{L,LU,U,RU,R,RD,D,LD,STOP}; // Use enumeration types to define the direction of the tank ;
   private int x,y;
    
   private int oldX,oldY; // Record the position of the last tank ;
    
   private boolean live= true ; // Judge if you're alive
    
   public boolean isLive() {
     return live;
   }
  
   public void setLive( boolean live) {
     this .live = live;
   }
  
  
   private boolean good; // Tanks are good or bad
    
   public boolean isGood() {
     return good;
   }
  
  
   private Direction ptDir=Direction.D; // The direction of the new barrel ;
    
   TankWarClient tc; // In order to hold the reference of the other party so that its member variables can be easily accessed ;
    
   Direction dir=Direction.STOP; // Set the direction of the tank to stop;
    
    
    
   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 other party's reference ;
   }
    
    
   public void draw(Graphics g)
   {
     if (!live) // If you die, no more draw;
    
       if (!good)
       {
         tc.tanks.remove( this );
         if (tc.tanks.size()< 5 ) // Less than 5 Add a tank when adding a tank ;
         {
           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 tanks appear in random positions
           }
         }
       }
        
       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 , Draw a straight line instead of ;
       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 position of the last step 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 border ;
      */
     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 enumeration types to arrays ;
        
       if (step== 0 )
       {
         step=r.nextInt( 12 )+ 3 ;
         int rn=r.nextInt(dirs.length); // produce length Random integers within ;
         dir=dirs[rn];
       }
       step--;
       if (r.nextInt( 40 )> 20 ) this .fire(); // Make enemy tanks fire bullets ;
     }
      
   }
    
   /**
    * Processing keys
    * @param e Keyboard events ;
    */
   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();
   }
    
   /**
    * bullets
    * @return Returns the bullet type
    */
   public Missile fire() {
     if (!live)
       return null ;
     int mx= this .x+Tank.WIDTH/ 2 -Missile.WIDTH/ 2 ; // Calculate the location of the bullet ;
     int my= this .y+Tank.HEIGHT/ 2 -Missile.HEIGHT/ 2 ;
     Missile m= new Missile(mx,my,good,ptDir, this .tc); //// Fire bullets in 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); // Fire bullets in 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() // obtain tank The rectangular area of
   {
     return new Rectangle( this .x, this .y, this .WIDTH, this .HEIGHT);
   }
    
   /**
    * The tank hit the wall
    * @param w wall
    * @returntrue Hit ,false Didn't hit ;
    */
   public boolean colliedsWithWall(Wall w)
   {
     if ( this .live&& this .getRect().intersects(w.getRect()))
     {
       this .stay();
       return true ;
     }
     return false ;
   }
    
   /**
    * Dealing with tank to tank collisions , Prevent them from crossing each other ;
    * @param tanks Enemy tanks ;
    * @return true Hit ,false Didn't 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(); // Return to the previous position ;
           t.stay(); //// Return to the previous position
           return true ;
         }
          
      
     }
     return false ;
   }
    
  
   private void stay()
   {
     x=oldX;
     y=oldY;
   }
    
   /**
    * by Tank The inner class of ; Blood strip , On top 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 );
     }
   }
    
   /**
    * Add blood to the blood clot ;
    * @param b clot
    * @returntrue Eat to ,false Not eating ;
    */
   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 tanks come back to life after death ;
    */
   public void reBorn()
   {
     if ( this .isGood()&&! this .isLive())
     {
       this .setLive( true );
       this .setLife( 100 );
     }
   }
}

TankWarClient.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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{
/**
  * The 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() // An anonymous class
     {
       public void windowClosing(WindowEvent e)
       {
         System.exit( 0 );
       }
     });
     this .addKeyListener( new KeyMonitor()); // Add a keyboard monitor ;
     this .setResizable( false ); // You can't 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 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);
      
     /**
      * Detecting 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 the 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 buffer technology to eliminate tank flicker ;
    */
   public void update(Graphics g) //g For the brush on the screen ;
   {
     if (OffScreenImage== null )
       OffScreenImage= this .createImage(GAME_WIDTH, GAME_HEIGHT);
     Graphics gOffScreen=OffScreenImage.getGraphics(); //gOffScreen yes OffScreenImage My brush ;
     Color c=gOffScreen.getColor();
     gOffScreen.setColor(Color.GREEN);
     gOffScreen.fillRect( 0 , 0 , GAME_WIDTH, GAME_HEIGHT);
     gOffScreen.setColor(c);
     paint(gOffScreen); // On a virtual picture ;
     g.drawImage(OffScreenImage, 0 , 0 , null ); // use g The brush draws the virtual picture on the screen
      
   }
    
    
   private class PaintThread implements Runnable{
  
     public void run() {
       while ( true )
       {
         repaint(); // there repaint The method is Frame Class
         try {
         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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.hkm.TankWar;
import java.awt.*;
/**
  * Generate the obstruction wall class ;
  * @author Hekangmin
  *
  */
  
public class Wall {
   /**
    * x,y For the location of the wall ,w,h Is the width height ;
    */
   int x,y,w,h;
   /**
    * Hold quotes
    */
   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 whole content of this article , I hope you like it .

java Make simple tank war more related articles

  1. JAVA curriculum design ( Tanks war )

    2019-01-16 Tank war game background : 1. Demand analysis 1.1 Environmental requirements operating system :Windows 7(SP1) above JAVA virtual machine :JDK1.8 above development environment :Eclipse(4.5 above ) 1.2 horn ...

  2. Han Shunping html5 Course sharing :6 Hours to make a classic tank war !

    Remember an interview I attended last year , I've done Java Many years of interviewers don't play well in front of their faces , But as soon as he heard that I would html5, Eyes brightened immediately . No matter what you want to sign with me .. . therefore . Today's friends who are worried about their work , Learn from good examples html5, I'll definitely find it for you ...

  3. java Basics Graphic technology . Tanks war And java Drawing coordinate system ( Two )

    function : Draw the tank on the coordinate system /* * function : Tank game's 1.0 * 1. Draw the tank * */ package com.tank; import javax.swing.*; import java.awt ...

  4. java Basics Graphic technology . Tanks war And java Drawing coordinate system ( One )

    Introduction to coordinate system The following figure illustrates java Coordinate system . The origin of the coordinates is in the upper left corner , In pixels , A pixel is the smallest unit of display on a computer screen . stay java In the coordinate system of , The first is x coordinate , Indicates that the current position is horizontal , From the origin of the coordinates x Pixel : the second ...

  5. utilize JAVA Make a simple login window

    import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; ...

  6. HTML5 Tanks war ( Han Shunping version )

    HTML5 Tanks war ( Han Shunping version ) 2017-3-22 22:46:22 by SemiconductorKING I studied last summer vacation HTML5 Achieve a simple tank war , Feel right JavaScript For beginners , ...

  7. I'll teach you how to use Python Realization “ Tanks war ”, With detailed code !

    When I was a kid “ Tanks war ”, Do you remember ? ​ Full of memories ! today , We use Python And powerful third-party library to achieve a simple tank war game . ​ Overall effect Environment depends on python3.7 pygame1.9.6 ...

  8. Java Small projects -- Tanks war (version1.0)

    Java Small projects -- Tanks war <TankWar1.0> This little project is mainly about practice j2se The basic content and object-oriented thinking of . The project implements basic simple functions , One of our tanks , Use up, down, left and right keys to control the direction of movement , Press F The key is gun ...

  9. nyoj 284 Tanks war Simple search

    Topic link :http://acm.nyist.net/JudgeOnline/problem.php?pid=284 The question : In a given graph , Iron wall , The river cannot go , If the brick wall goes away , Spend more time 1, Ask from the beginning to the end at least ...

Random recommendation

  1. C# in “ Looks like ” Jump out of while(true) Dead cycle

    When the program first runs to Read() Function time , The program will be blocked , Then enter the characters ,Enter Then the program is activated ,windows The platform will automatically add carriage return and line feed after the input characters , There are three characters in the input stream , However read Read only one at a time ...

  2. poj 1651 http://poj.org/problem?id=1651

      http://poj.org/problem?id=1651Multiplication Puzzle   Time Limit: 1000MS   Memory Limit: 65536K To ...

  3. Angularjs summary ( 5、 ... and ) Instruction use and common control assignment operation

    1. Commonly used instructions <div ng-controller="jsyd-controller"> <div style="float:left;width:10 ...

  4. CSS Authoritative guide study notes —— HTML Element classification

    HTML The document consists of various elements . such as ,p.table.span wait . Each element has an impact on the performance of the document .CSS in , Each element generates a box ( The legendary box ), It contains element content . Elements can be divided into two types according to how they are created ...

  5. It occurred to me that :QProcess Of Read It's too powerful , If there's something you can't decide , You can call external programs good

    In this way, we can use other languages to expand its functions indefinitely , such as golang, such as Delphi

  6. [ Weave a message framework ][rpc] Use article

    rpc There are two parts , One is the caller , The other side is the service provider The caller only cares about that service , Transfer the corresponding parameters , Just return the content The provider calls the corresponding service according to the same parameters , After the work is processed, you can respond to the content According to their relationship, we can use JAVA The interface is the same as the implementation class ...

  7. Mobile tab Swipe and pull up refresh loads

    Mobile tab Swipe and pull up refresh loads see demo( Please view in mobile mode ) Look at the code The original intention of developing this plug-in is , When doing a project, I found that we are now implementing the mobile terminal tab Sliding plug-ins are mostly based on swiper,swiper It's too powerful, and I ...

  8. POJ-2570 Fiber Network---Floyd+ Binary represents a set

    Topic link : https://vjudge.net/problem/POJ-2570 The main idea of the topic : Some companies decided to build a faster network , be called " Fiber optic network ". They've set up a lot of sites around the world , this ...

  9. python Graphics users

    1) Use GUI 1.GUI:Graphical user interface 2.tkinter:GUI libary for Python A library of its own 3.GUI:Example 2)Ubuntu18. ...

  10. Palindromic Numbers LightOJ - 1205

    The main idea of the topic : Find the number of palindromes in the interval Topic ideas : digit dp, Start with the first half of the numbers , Then fill in the second half of the number . #include<cstdio> #include<cstring> #i ...