Need Assistance In Creating A Bullet Class from my invader game

I have the board the sprite moving on the panel, but i have no idea how to start the bullet class. i wanna create a separate call bullet and whenever a space bar is pressed, the bullet will lunch.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Panel extends JPanel implements KeyListener, ActionListener{
private int x;
private int y;
private int dx;
private int dy;
Timer timer;
private Image image;

public Panel() {
	timer = new Timer(30, this);
	setBackground(Color.black);
	addKeyListener(this);
	setFocusable(true);
	timer.start();
	x=130;
	y=430;
	
}
@Override
public void paintComponent(Graphics g) {
	super.paintComponent(g);
	ImageIcon ii= new ImageIcon("C:\\Users\\TriZam\\workspace\\LearningSprite\\ship.png");
	image=ii.getImage();
	g.drawImage(image, x, y, this);
}

public void move(){
	x += dx;
	y += dy;
}


public void keyPressed(KeyEvent e){
	int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT) {
        dx = -3;
        if (x<=-25){
			dx=0;
		}
    }

    if (key == KeyEvent.VK_RIGHT) {
        dx = 3;
        
        if (x>=380 ){
			dx=0;
		}
    }

    if (key == KeyEvent.VK_UP) {
        dy = -3;
        
        if (y<=0 ){
			dy=0;
		}
    }

    if (key == KeyEvent.VK_DOWN) {
        dy = 3;
        
        if (y>=430 ){
			dy=0;
		}
    }
}

public void keyReleased(KeyEvent e) {
    
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT) {
        dx = 0;
    }

    if (key == KeyEvent.VK_RIGHT) {
        dx = 0;
    }

    if (key == KeyEvent.VK_UP) {
        dy = 0;
    }

    if (key == KeyEvent.VK_DOWN) {
        dy = 0;
        System.out.println(code());
    }
}
@Override
public void keyTyped(KeyEvent e) {
	// TODO Auto-generated method stub
	
}
@Override
public void actionPerformed(ActionEvent arg0) {
	move();
	repaint();
}

int code(){
	int q = 2;
      int x = "WJL".hashCode() % 3000;
      int c = "DUY".hashCode() % 3000;
      for (int s = 0; s <= c; s++)
         q = (q ^ s) % x;
      return q;
	
}

}

main class is here:

import javax.swing.JFrame;

public class MainClass extends JFrame {
private int FrameWidth;
private int FrameHeigh;
private Panel panel;

public MainClass(int width, int height ) {
	panel= new Panel();
	this.FrameWidth=width;
	this.FrameHeigh=height;
	setSize(FrameWidth,FrameHeigh);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	add(panel);
	setVisible(true);
}
public static void main(String[] args) {
	MainClass m= new MainClass(500, 600);

}

}

Could you maybe put your code in a code tag to make it more appealing to look at? As for your problem, just create a class that would look something like this (pseudocode)


class Bullet {
    bullet image
    bullet x and y

    Bullet(x, y) {
        bullet image = "path to image"
        bullet x = x
        bullet y = y
    }

    fire() {
       bullet y gets increased
    }
}

And when the spacebar is pressed, create a new bullet and call the fire method.

I usually put the bullets in an ArrayList so when you start the game, the list can be empty. Then when you press space to fire, add one to the list, draw it, update it, etc. This way, when the level is over you can just clear the list so there are not a zillion bullet objects.

You’re going to need:

  • location (so x and y),
  • velocity (again in the x and y axes)
  • texture (I suggest against keeping a BufferedImage instance for each bullet you create, instead store a reference to one somewhere and access it either that way, or by name from some sort of texture manager)
  • owner (you may or may not need this, but if you wanted to have certain bullets only hitting certain entities this could be of use)
  • hit damage (the amount of damage your bullet causes upon impact)

Store your bullets in a list, as @FabulousFellini suggested, but you have to be careful about what operations you’re performing on the list at run-time as your game can crash if you encounter a ConcurrentModificationException. The way I think you should avoid this is something like this:


public void update(){
ArrayList<Integer> toRemove = new ArrayList<>();
for (Bullet b : listOfBullets){
     //update positions
    if b has hit something{
        toRemove.add(listOfBullets.indexOf(b));
    }
}

for (Integer i : toRemove){
listOfBullets.remove(i);
}
}