[Java] 为了解决坦克停下也能打出炮弹的问题—画出炮筒 →→→→→进入此内容的聊天室

来自 , 2019-06-09, 写在 Java, 查看 108 次.
URL http://www.code666.cn/view/cfa53013
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. public class TankClient extends Frame {
  5.         public static final int GAME_WIDTH = 800;
  6.         public static final int GAME_HEIGHT = 600;
  7.        
  8.         Tank myTank = new Tank(50, 50, this);
  9.         Missile m = null;
  10.        
  11.         Image offScreenImage = null;
  12.        
  13.         public void paint(Graphics g) {
  14.                 if(m!=null) m.draw(g);
  15.                 myTank.draw(g);
  16.         }
  17.        
  18.         public void update(Graphics g) {
  19.                 if(offScreenImage == null) {
  20.                         offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
  21.                 }
  22.                 Graphics gOffScreen = offScreenImage.getGraphics();
  23.                 Color c = gOffScreen.getColor();
  24.                 gOffScreen.setColor(Color.GREEN);
  25.                 gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
  26.                 gOffScreen.setColor(c);
  27.                 paint(gOffScreen);
  28.                 g.drawImage(offScreenImage, 0, 0, null);
  29.         }
  30.  
  31.         public void lauchFrame() {
  32.                 //this.setLocation(400, 300);
  33.                 this.setSize(GAME_WIDTH, GAME_HEIGHT);
  34.                 this.setTitle("TankWar");
  35.                 this.addWindowListener(new WindowAdapter() {
  36.                         public void windowClosing(WindowEvent e) {
  37.                                 System.exit(0);
  38.                         }
  39.                 });
  40.                 this.setResizable(false);
  41.                 this.setBackground(Color.GREEN);
  42.                
  43.                 this.addKeyListener(new KeyMonitor());
  44.                
  45.                 setVisible(true);
  46.                
  47.                 new Thread(new PaintThread()).start();
  48.         }
  49.  
  50.         public static void main(String[] args) {
  51.                 TankClient tc = new TankClient();
  52.                 tc.lauchFrame();
  53.         }
  54.        
  55.         private class PaintThread implements Runnable {
  56.  
  57.                 public void run() {
  58.                         while(true) {
  59.                                 repaint();
  60.                                 try {
  61.                                         Thread.sleep(50);
  62.                                 } catch (InterruptedException e) {
  63.                                         e.printStackTrace();
  64.                                 }
  65.                         }
  66.                 }
  67.         }
  68.        
  69.         private class KeyMonitor extends KeyAdapter {
  70.  
  71.                 public void keyReleased(KeyEvent e) {
  72.                         myTank.keyReleased(e);
  73.                 }
  74.  
  75.                 public void keyPressed(KeyEvent e) {
  76.                         myTank.keyPressed(e);
  77.                 }
  78.                
  79.         }
  80. }
  81.  
  82.  
  83.  
  84. import java.awt.*;
  85. import java.awt.event.*;
  86.  
  87. public class Tank {
  88.         public static final int XSPEED = 5;
  89.         public static final int YSPEED = 5;
  90.        
  91.         public static final int WIDTH = 30;
  92.         public static final int HEIGHT = 30;
  93.        
  94.         TankClient tc;
  95.        
  96.         private int x, y;
  97.        
  98.         private boolean bL=false, bU=false, bR=false, bD = false;
  99.         enum Direction {L, LU, U, RU, R, RD, D, LD, STOP};
  100.        
  101.         private Direction dir = Direction.STOP;
  102.         private Direction ptDir = Direction.D;
  103.  
  104.         public Tank(int x, int y) {
  105.                 this.x = x;
  106.                 this.y = y;
  107.         }
  108.        
  109.         public Tank(int x, int y, TankClient tc) {
  110.                 this(x, y);
  111.                 this.tc = tc;
  112.         }
  113.        
  114.         public void draw(Graphics g) {
  115.                 Color c = g.getColor();
  116.                 g.setColor(Color.RED);
  117.                 g.fillOval(x, y, WIDTH, HEIGHT);
  118.                 g.setColor(c);
  119.                
  120.                 switch(ptDir) {
  121.                 case L:
  122.                         g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT/2);
  123.                         break;
  124.                 case LU:
  125.                         g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y);
  126.                         break;
  127.                 case U:
  128.                         g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y);
  129.                         break;
  130.                 case RU:
  131.                         g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y);
  132.                         break;
  133.                 case R:
  134.                         g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT/2);
  135.                         break;
  136.                 case RD:
  137.                         g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT);
  138.                         break;
  139.                 case D:
  140.                         g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y + Tank.HEIGHT);
  141.                         break;
  142.                 case LD:
  143.                         g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT);
  144.                         break;
  145.                 }
  146.                
  147.                 move();
  148.         }
  149.        
  150.         void move() {
  151.                 switch(dir) {
  152.                 case L:
  153.                         x -= XSPEED;
  154.                         break;
  155.                 case LU:
  156.                         x -= XSPEED;
  157.                         y -= YSPEED;
  158.                         break;
  159.                 case U:
  160.                         y -= YSPEED;
  161.                         break;
  162.                 case RU:
  163.                         x += XSPEED;
  164.                         y -= YSPEED;
  165.                         break;
  166.                 case R:
  167.                         x += XSPEED;
  168.                         break;
  169.                 case RD:
  170.                         x += XSPEED;
  171.                         y += YSPEED;
  172.                         break;
  173.                 case D:
  174.                         y += YSPEED;
  175.                         break;
  176.                 case LD:
  177.                         x -= XSPEED;
  178.                         y += YSPEED;
  179.                         break;
  180.                 case STOP:
  181.                         break;
  182.                 }
  183.                
  184.                 if(this.dir != Direction.STOP) {
  185.                         this.ptDir = this.dir;
  186.                 }
  187.         }
  188.        
  189.         public void keyPressed(KeyEvent e) {
  190.                 int key = e.getKeyCode();
  191.                 switch(key) {
  192.                 case KeyEvent.VK_CONTROL:
  193.                         tc.m = fire();
  194.                         break;
  195.                 case KeyEvent.VK_LEFT :
  196.                         bL = true;
  197.                         break;
  198.                 case KeyEvent.VK_UP :
  199.                         bU = true;
  200.                         break;
  201.                 case KeyEvent.VK_RIGHT :
  202.                         bR = true;
  203.                         break;
  204.                 case KeyEvent.VK_DOWN :
  205.                         bD = true;
  206.                         break;
  207.                 }
  208.                 locateDirection();
  209.         }
  210.        
  211.         void locateDirection() {
  212.                 if(bL && !bU && !bR && !bD) dir = Direction.L;
  213.                 else if(bL && bU && !bR && !bD) dir = Direction.LU;
  214.                 else if(!bL && bU && !bR && !bD) dir = Direction.U;
  215.                 else if(!bL && bU && bR && !bD) dir = Direction.RU;
  216.                 else if(!bL && !bU && bR && !bD) dir = Direction.R;
  217.                 else if(!bL && !bU && bR && bD) dir = Direction.RD;
  218.                 else if(!bL && !bU && !bR && bD) dir = Direction.D;
  219.                 else if(bL && !bU && !bR && bD) dir = Direction.LD;
  220.                 else if(!bL && !bU && !bR && !bD) dir = Direction.STOP;
  221.         }
  222.  
  223.         public void keyReleased(KeyEvent e) {
  224.                 int key = e.getKeyCode();
  225.                 switch(key) {
  226.                 case KeyEvent.VK_LEFT :
  227.                         bL = false;
  228.                         break;
  229.                 case KeyEvent.VK_UP :
  230.                         bU = false;
  231.                         break;
  232.                 case KeyEvent.VK_RIGHT :
  233.                         bR = false;
  234.                         break;
  235.                 case KeyEvent.VK_DOWN :
  236.                         bD = false;
  237.                         break;
  238.                 }
  239.                 locateDirection();             
  240.         }
  241.        
  242.         public Missile fire() {
  243.                 int x = this.x + Tank.WIDTH/2 - Missile.WIDTH/2;
  244.                 int y = this.y + Tank.HEIGHT/2 - Missile.HEIGHT/2;
  245.                 Missile m = new Missile(x, y, ptDir);
  246.                 return m;
  247.         }
  248. }
  249.  
  250.  
  251. import java.awt.*;
  252.  
  253. public class Missile {
  254.         public static final int XSPEED = 10;
  255.         public static final int YSPEED = 10;
  256.        
  257.         public static final int WIDTH = 10;
  258.         public static final int HEIGHT = 10;
  259.        
  260.         int x, y;
  261.         Tank.Direction dir;
  262.        
  263.         public Missile(int x, int y, Tank.Direction dir) {
  264.                 this.x = x;
  265.                 this.y = y;
  266.                 this.dir = dir;
  267.         }
  268.        
  269.         public void draw(Graphics g) {
  270.                 Color c = g.getColor();
  271.                 g.setColor(Color.BLACK);
  272.                 g.fillOval(x, y, WIDTH, HEIGHT);
  273.                 g.setColor(c);
  274.                
  275.                 move();
  276.         }
  277.  
  278.         private void move() {
  279.                 switch(dir) {
  280.                 case L:
  281.                         x -= XSPEED;
  282.                         break;
  283.                 case LU:
  284.                         x -= XSPEED;
  285.                         y -= YSPEED;
  286.                         break;
  287.                 case U:
  288.                         y -= YSPEED;
  289.                         break;
  290.                 case RU:
  291.                         x += XSPEED;
  292.                         y -= YSPEED;
  293.                         break;
  294.                 case R:
  295.                         x += XSPEED;
  296.                         break;
  297.                 case RD:
  298.                         x += XSPEED;
  299.                         y += YSPEED;
  300.                         break;
  301.                 case D:
  302.                         y += YSPEED;
  303.                         break;
  304.                 case LD:
  305.                         x -= XSPEED;
  306.                         y += YSPEED;
  307.                         break;
  308.                 case STOP:
  309.                         break;
  310.                 }
  311.         }
  312.        
  313.        
  314. }
  315.  

回复 "为了解决坦克停下也能打出炮弹的问题—画出炮筒"

这儿你可以回复上面这条便签

captcha