how to shoot bullets ??

Hello everyone
am trying to make the player able to shoot bullets, and i thought using arrayList is the best way to do that (please correct me if am wrong)
so here is what i did
after initializing everything,
i check if the “shoot” boolean is true,
then variable called “numBullet” increment by 1
then a for loop go from 0 to the “numBullet” that draw and move all the bullets in the ArrayList
the problem is :
i only able to shoot one time (i press space and a number of bullets get created, i press again, nothing happens)
the speed is not right (every time a new bullet is created the speed increase)
wrong positions for the new bullets

i don’t think that there is any need to post the code cause am looking for a theory or a solution more than a fix for my code

thank you

Using the pastebin (Linked beneath the text box) please show us the code you’re using for this? Because it sounds like there are multiple problems going on here.

Codes :
(this is my first OOP program so don’t judge the mess you’re gonna see ::slight_smile: )

Control.java

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class Control implements KeyListener{
	public static boolean right,left,shoot;
		
	@Override
	public void keyPressed(KeyEvent e) {
		
		//moving the player 
		if(e.getKeyCode()==e.VK_RIGHT){
			
			right=true;
		}else if(e.getKeyCode()==e.VK_LEFT){
			
			left=true;
		}
		
		//shooting bullets
		if(e.getKeyCode()==e.VK_SPACE){
			shoot=true;
		}
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		if(e.getKeyCode()==e.VK_RIGHT){
			
			right=false;
		}else if(e.getKeyCode()==e.VK_LEFT){
		
			left=false;
		}
		//shooting bullets
		if(e.getKeyCode()==e.VK_SPACE){
			shoot=false;
		}	
				
	}

	@Override
	public void keyTyped(KeyEvent e) {
		
	}

}

Bullet.java

import java.awt.Graphics;
import java.awt.Rectangle;

public class Bullet extends Rectangle {
	public static int speed = 1, 
			x = Player.x + Player.width / 2, 
			y = Player.y;

	public Bullet(int x, int y, int width, int height) {

		x = this.x;
		y = this.y;
		this.setBounds(x, y, width, height);

	}

	public void draw(Graphics g) {
		g.drawRect(x, y, width, height);
	}

	public void move(int speed) {

		speed = this.speed;
		this.y -= speed;
	}
}

Screen.java

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;

import javax.swing.JPanel;

public class Screen extends JPanel implements Runnable {
	Thread thread = new Thread(this);

	public static Ball ball;
	public static Player player;
	public static Control control;
	public static Bullet bullet;
	public static ArrayList<Bullet> bulletArray;

	public static boolean isFirst = true;

	public static int screenWidth, screenHeight, screenX, screenY, numBullet;

	public Screen() {
		define();
		thread.start();
		this.addKeyListener(control);
		this.setFocusable(true);
	}

	public void define() {
		control = new Control();
		ball = new Ball(ball.x, ball.y, ball.height, ball.width);
		player = new Player(player.x, player.y, player.width, player.height,
				player.speed);

		screenWidth = this.getWidth();
		screenHeight = this.getHeight();

		bulletArray = new ArrayList<Bullet>();

		bullet = new Bullet(0, 0, 50, 50);

	}

	public void paintComponent(Graphics g) {

		if (isFirst) {
			define();
		}
		// drawing the background
		g.setColor(new Color(0, 0, 0));
		g.fillRect(screenX, 0, screenWidth, screenHeight);

		// drawing the ball
		g.setColor(new Color(250, 50, 0));
		ball.move(ball.x, ball.y);
		ball.draw(g);

		// player
		g.setColor(new Color(255, 255, 255));
		player.draw(g);
		player.move();

		// creating bullets
		if (control.shoot) {
			numBullet++;
		}

		for (int i = 0; i < numBullet; i++) {
			bullet = new Bullet(player.x, bullet.y, 5, 5);
			bulletArray.add(bullet);

			bulletArray.get(i).draw(g);
			bulletArray.get(i).move(bullet.speed);
		}

	}

	public void run() {

		while (true) {

			if (!isFirst) {

			}

			repaint();

			try {

				Thread.sleep(5);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}

i don’t think you need the other classes

You really need to fix up your game in general, before you try debugging anything else.

1. You need a game loop (read this) instead of just stuffing everything in paintComponent() and calling repaint().

2. Why are your bullet x/y ints static? It makes sense to have your speed static if you aren’t doing any acceleration, but different bullets will have different x/y coordinates. Also, is it really necessary to have that much stuff static in your Screen and Control classes?

3.

bullet = new Bullet(player.x, bullet.y, 5, 5);

should not be in your loop, because then you are creating/resetting every bullet in your ArrayList every time through the loop. Put it in the constructor, or something.

i knew it :-\
those codes are worthless :’(

No, just needs to be improved. A good base of code will make future development 100% easier.

yes but i don’t know how to reach that level …
i start learning Java 2 months ago and still can’t do anything 100% by myself :-\

Also don’t forget to remove the bullet after it finishes its job, going out off screen or explode.

@alaslipknot

I think you need a tutorial to get started. Here’s a list of tutorials from this site.

These should get you started well. I also recommend to see the Zetcode Java2D Games Tutorial

@SHC

i started with the zetCode tutorials but then i thought that video tutorials might be better, easier and faster to learn, but i seems i was wrong :stuck_out_tongue:
it’s the same mistake i made when i started to learn ActionScript ::slight_smile:
anyway …
thank you