Search code examples
javaswinganimationgraphicsgame-physics

How to shoot "bullets" in java swing animation game


I have created a simple animation game in which the user can move a circle with the arrow keys. I have drawn a square that 'follows' the center of the circle around. My goal is for there to be an infinite supply of squares (bullets) in the center of the circle (that follow it around), that can shoot left right up down using asdf while moving around. I am trying to figure out how to type code that will detach the bullet from following the circle around once shot. Thanks in advance. I am a beginner so i apologize in advance for the messy code:)

public class Game extends JPanel implements ActionListener, KeyListener
{

    Timer t = new Timer(5, this);

    int x, y, velx, vely, movx, movy = 0; 

    int shootx, shooty;

    int xaim = 0;

    int yaim = 0;

    int xpts[], ypts[];

    int aim;

    Polygon bullet;

    public Game(){

        t.start();

        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }
    public void paintComponent(Graphics g){

        Graphics2D g2d = (Graphics2D) g;
        //Background
        g2d.setColor(Color.BLUE);
        g2d.fillRect(0, 0, 600, 600);
        g2d.fillRect(x, y, 30, 30);
        //Borders
        g2d.setColor(Color.GREEN);
        g2d.fillRect(0, 0, 20, 600);
        g2d.fillRect(0, 0, 600, 20);
        g2d.fillRect(580, 0, 20, 600);
        g2d.fillRect(0,560,600,20);
        //Player
        g2d.setColor(Color.RED);
        g2d.fillOval(x, y, 30, 30);
        //Bullet
        g2d.setColor(Color.BLACK);
        g2d.fillRect(xaim+28,yaim+28,15,15);    
    }
    public void actionPerformed(ActionEvent e) {
          checkbounds();
          x += movx;
          y += movy;
          xaim += velx;
          yaim += vely;

        repaint();
    }
    public void keyPressed(KeyEvent e) {
          int code = e.getKeyCode();
          if (code == KeyEvent.VK_UP)
          {
              up();
          }
          if (code == KeyEvent.VK_DOWN)
          {
              down();
          }
          if (code == KeyEvent.VK_LEFT)
          {
              left();
          }
          if (code == KeyEvent.VK_RIGHT)
          {
              right();
          }
          if (code == KeyEvent.VK_A)
          {
             //code to shoot left
          }
          if (code == KeyEvent.VK_W)
          {
              //code to shoot up
          }
          if (code == KeyEvent.VK_D)
          {
              //code to shoot right
          }
          if (code == KeyEvent.VK_S)
          {
              //code to shoot down
          }

    }
    public void up(){
        movy = -1;
        movx = 0;
        velx = 0;
        vely = -1;
    }
    public void down(){
        movy = 1;
        movx = 0;
        velx = 0;
        vely = 1;

    }
    public void right(){
        movx = 1;
        movy = 0;
        velx = 1;
        vely= 0;

    }
    public void left(){
        movx = -1;
        movy = 0;
        velx = -1;
        vely = 0;
    }
    public void checkbounds(){
        if(x < 20){
            x = 20;
        }
        if(x > 550){
            x = 550;
        }
        if(y < 20){
            y=20;
        }
        if(y > 530){
            y = 530;
        }
    }
    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}

}

Solution

  • Basically, what you are going to want to do is create a new class for Bullet. This class will hold the data values for each individual bullet (x position, y position, x velocity, y velocity). Then, create a list of bullets in your main class. Whenever you want to add a bullet, add it to the list. Whenever you update your game, update the list as well. This is a good game tutorial to get you started.