当前位置:网站首页>Friendly tanks fire bullets

Friendly tanks fire bullets

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

Create a bullet class

  • To fire a bullet , There must be a bullet class first

    • It contains the location of the bullet
    • The speed of the bullet
    • The direction of the bullet
    • The living condition of the bullet
  • The main point is that we should treat the bullets as a Threads treat , And when the shot goes out of bounds ( Find out the scope of our framework ) Stop the work of the thread when

public class Shot implements Runnable{
    
     int x;
     int y;
     int direct=0;
     int speed=5;
     boolean isLive=true;

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

    @Override
    public void run() {
    

        while (isLive){
    
            try {
    
                Thread.sleep(50);
            } catch (InterruptedException e) {
    
                throw new RuntimeException(e);
            }

            switch (direct){
    
                case 0:
                    y-=speed;
                    break;
                case 1:
                    x+=speed;
                    break;
                case 2:
                    y+=speed;
                    break;
                case 3:
                    x-=speed;
                    break;
            }
            
             When it hits a tank or crosses the boundary, it will disappear automatically   
             Thread termination 
            if(!(x>=0&&x<=1000&&y>=0&&y<=750&&isLive)){
    
                isLive=false;
                break;
            }
            
        }
    }
}

Add bullets to friendly tanks

  • To add another event listening event when we press J You can fire bullets when you need them , It is equivalent to starting the bullet thread
  • By being in your own tank class , Get the position and direction of your tank, and then you can get the position and direction of the bullets , Then create a new bullet class from your tank class , And start the thread of the bullet to be fired
public class Hero extends Tank {
     
    
     Define a bullet 
    Shot shot=null;

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


       public void shotEnemyTank(){
     
           
          Get the direction and position of the bullets to be fired according to your tanks 
		 This self can first determine the direction of the bullets to be fired according to the direction of the tank on the paper 
          Then it will be easy to implement it by code 
             
        switch (getDirect()){
     
            case 0:
                shot=new Shot(getX()+20,getY(),0);
                break;
            case 1:
                shot=new Shot(getX()+60,getY()+20,1);
                break;
            case 2:
                shot=new Shot(getX()+20,getY()+60,2);
                break;
            case 3:
                shot=new Shot(getX(),getY()+20,3);
                break;
        }
           
          After creating the bullet, don't forget to start the bullet thread finally  
         new Thread(shot).start();
    }
}

Add event monitoring mechanism for firing bullets

  • Still MyPanel Inside keyPressed() Method when you press J Start the bullet thread when
  • Here we are , The complete code of the event listening mechanism is as follows
    @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);
        }
		
        🧨🧨🧨 Monitor the firing of bullets 
        if (e.getKeyCode()==KeyEvent.VK_J){
     
            hero.shotEnemyTank();
        }
		
        🥽🥽🥽 Do not forget to redraw the panel  
         Otherwise, it is a static effect 
        this.repaint();
    }
原网站

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