[Java] 如何让坦克向8个方向行走 →→→→→进入此内容的聊天室

来自 , 2019-06-02, 写在 Java, 查看 104 次.
URL http://www.code666.cn/view/1415db70
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. public class TankClient extends Frame {
  5.        
  6.         public static final int GAME_WIDTH=800;
  7.         public static final int GAME_HEIGHT=600;
  8.        
  9.     Tank myTank =new Tank(50,50);
  10.        
  11.         Image offScreenImage = null;
  12.        
  13.         public void paint(Graphics g) {
  14.  
  15.                 myTank.draw(g);//理解一下面向对象
  16.         }
  17.        
  18.         public void update(Graphics g) {
  19.                 if(offScreenImage == null) {
  20.                         offScreenImage = this.createImage(GAME_WIDTH, GAME_WIDTH);
  21.                 }
  22.                 Graphics gOffScreen = offScreenImage.getGraphics();
  23.                 Color c = gOffScreen.getColor();
  24.                 gOffScreen.setColor(Color.GREEN);
  25.                 gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_WIDTH);
  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_WIDTH);
  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.                 new Thread(new PaintThread()).start();
  47.         }
  48.  
  49.         public static void main(String[] args) {
  50.                 TankClient tc = new TankClient();
  51.                 tc.lauchFrame();
  52.         }
  53.        
  54.         private class PaintThread implements Runnable {
  55.  
  56.                 public void run() {
  57.                         while(true) {
  58.                                 repaint();
  59.                                 try {
  60.                                         Thread.sleep(50);
  61.                                 } catch (InterruptedException e) {
  62.                                         e.printStackTrace();
  63.                                 }
  64.                         }
  65.                 }
  66.         }
  67.  
  68.         private class KeyMonitor  extends KeyAdapter
  69.         {
  70.  
  71.                 public void keyPressed(KeyEvent e) {
  72.  
  73.                         System.out.println("ok");
  74.                        
  75.             myTank.keyPressed(e);
  76.                 }
  77.                
  78.         }
  79.  
  80. }
  81.  
  82.  
  83.  
  84.  
  85.  
  86. import java.awt.*;
  87. import java.awt.event.*;
  88. public class Tank {
  89.  
  90. public static final int XSPEED =5;
  91. public static final int YSPEED =5;
  92.  
  93. private int x,y;
  94. private boolean bL=false,bU=false,bR=false,bD=false;
  95.  
  96. enum Direction{L,LU,U,RU,R,RD,D,LD,STOP};
  97. private Direction dir=Direction.STOP;  
  98.        
  99.        
  100.         public Tank(int x, int y) {
  101.                 this.x = x;
  102.                 this.y = y;
  103.         }
  104.  
  105.         public void draw(Graphics g)
  106.         {
  107.                 Color c = g.getColor();
  108.                 g.setColor(Color.RED);
  109.                 g.fillOval(x, y, 30, 30);
  110.                 g.setColor(c);
  111.                        
  112.                 move();
  113.         }
  114.        
  115.         void move()
  116.         {
  117.                 switch(dir) {
  118.                 case L:
  119.                         x -= XSPEED;
  120.                         break;
  121.                 case LU:
  122.                         x -= XSPEED;
  123.                         y -= YSPEED;
  124.                         break;
  125.                 case U:
  126.                         y -= YSPEED;
  127.                         break;
  128.                 case RU:
  129.                         x += XSPEED;
  130.                         y -= YSPEED;
  131.                         break;
  132.                 case R:
  133.                         x += XSPEED;
  134.                         break;
  135.                 case RD:
  136.                         x += XSPEED;
  137.                         y += YSPEED;
  138.                         break;
  139.                 case D:
  140.                         y += YSPEED;
  141.                         break;
  142.                 case LD:
  143.                         x -= XSPEED;
  144.                         y += YSPEED;
  145.                         break;
  146.                 case STOP:
  147.                         break;
  148.                 }
  149.         }
  150.        
  151.        
  152.         public void keyPressed(KeyEvent e)
  153.         {
  154.  
  155.                 int key=e.getKeyCode();
  156. //              if(key==KeyEvent.VK_RIGHT)
  157. //              {
  158. //                      x +=5;
  159. //              }
  160.                
  161.                 switch(key)
  162.                 {
  163.                 case KeyEvent.VK_LEFT:
  164.                         bL=true;
  165.                         break;
  166.                 case KeyEvent.VK_UP:
  167.                         bU=true;
  168.                         break;
  169.                 case KeyEvent.VK_RIGHT:
  170.                         bR=true;
  171.                         break;
  172.                 case KeyEvent.VK_DOWN:
  173.                     bD=true;
  174.                     break;
  175.                 }
  176.                 locateDirection() ;
  177.         }
  178.  
  179.  
  180.         void locateDirection()
  181.         {
  182.                 if(bL && !bU && !bR && !bD) dir = Direction.L;
  183.                 else if(bL && bU && !bR && !bD) dir = Direction.LU;
  184.                 else if(!bL && bU && !bR && !bD) dir = Direction.U;
  185.                 else if(!bL && bU && bR && !bD) dir = Direction.RU;
  186.                 else if(!bL && !bU && bR && !bD) dir = Direction.R;
  187.                 else if(!bL && !bU && bR && bD) dir = Direction.RD;
  188.                 else if(!bL && !bU && !bR && bD) dir = Direction.D;
  189.                 else if(bL && !bU && !bR && bD) dir = Direction.LD;
  190.                 else if(!bL && !bU && !bR && !bD) dir = Direction.STOP;
  191.         }
  192.        
  193.  
  194. }
  195.  

回复 "如何让坦克向8个方向行走"

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

captcha