当前位置:网站首页>Let our tanks move happily

Let our tanks move happily

2022-06-11 18:36:00 C_ x_ three hundred and thirty

Create a parent tank class

  • 🪂🪂🪂 This class is used as a subclass to create your own tanks and enemy tanks in the future
  • It's easy to implement It mainly defines some attributes
    • The position of the tank
    • The speed of the tank
    • The direction of the tank
public class Tank {
     

    // object-oriented !!!!!!! Give full expression to 
    private int x;
    private int y;
    private int direct;
    private int speed =1;

    public int getSpeed() {
     
        return speed;
    }

    public void setSpeed(int speed) {
     
        this.speed = speed;
    }

    public void moveUp(){
     
        y-=speed;
    }

    public void moveDown(){
     
        y+=speed;
    }

    public void moveLeft(){
     
        x-=speed;
    }

    public void moveRight(){
     
        x+=speed;
    }
    public int getDirect() {
     
        return direct;
    }

    public void setDirect(int direct) {
     
        this.direct = direct;
    }

    public Tank(int direct) {
     
        this.direct = direct;
    }

    public Tank(int x, int y) {
     
        this.x = x;
        this.y = y;
    }

    public int getX() {
     
        return x;
    }

    public void setX(int x) {
     
        this.x = x;
    }

    public int getY() {
     
        return y;
    }

    public void setY(int y) {
     
        this.y = y;
    }
}

Create your own tank

public class Hero extends Tank {
    

    public Hero(int x, int y) {
    
        super(x, y);
    }
}

‍*️‍*️‍*️ move

To get the tank moving , We must implement an interface on the sketchpad class we define

  • Event monitoring mechanism

  • public class Mypanel extends JPanel implements KeyListener{
           }
    
  • And then you have to implement all of his methods , But we only use one of these methods for this requirement

 @Override
    public void keyTyped(KeyEvent e) {
     

    }

    @Override
    public void keyPressed(KeyEvent e) {
     
        if(e.getKeyCode()==KeyEvent.VK_W){
     
           if(hero.getY()>0){
     
               hero.moveUp();
           }
            hero.setDirect(0);
        } else if (e.getKeyCode()==KeyEvent.VK_D) {
     
            if(hero.getX()+60<1000){
     
                hero.moveRight();
            }
            hero.setDirect(1);
        }
        else if (e.getKeyCode()==KeyEvent.VK_S) {
     
            if (hero.getY()+60<750){
     
                hero.moveDown();
            }
            hero.setDirect(2);
        }else if (e.getKeyCode()==KeyEvent.VK_A) {
     
            if (hero.getX()>0){
     
                hero.moveLeft();
            }
            hero.setDirect(3);
        }
        
        this.repaint();
         Be careful !!!
        
         This line of code is the point ! Don't forget to write !!!
         I said in my last blog ,paint Method is called by the system , Redrawing will be called automatically , So when we press the keyboard keys every time, we need to call the sketchpad redrawing operation again , Only in this way can we see the effect of the dynamic movement of the tank , Without this redrawn method call , There is no way to see the dynamic effect , You can only see that the tank keeps rotating in place 
    }

    @Override
    public void keyReleased(KeyEvent e) {
     

    }

  • ️‍*️️‍*️️‍*️ One last thing to remember , To put This event listening mechanism is added to our framework , Because everything we do is based on JFrame Framework implementation , So in Tank The event listening mechanism should be added to the construction method of To take effect
    public TankGame03() {
    
        mp=new Mypanel();
        this.add(mp);
        	
        this.addKeyListener(mp);// Add an event listening mechanism to the framework 
       		 
        this.setSize(1030,810);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
原网站

版权声明
本文为[C_ x_ three hundred and thirty]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111819132853.html