# Making tons of bullets flying in random directions

So I have a game here:

int x = 500;
int y = 300;
int f1 = 255;
int f2 = 255;
int f3 = 255;
int ap = 120;
int hp = 190;
boolean showrange = true;
PFont font;
float xSpeed = 0;
float ySpeed = 0;

BulletSystem b1;

boolean keyLeft, keyRight, keyUp, keyDown;

void setup() {
size(1000, 600);
frameRate(1000);
textFont(font);
b1 = new BulletSystem();
}

void draw() {
background(255);

noStroke();
strokeCap(ELLIPSE);
fill(89, 200, 222, 40);
if (showrange == true) ellipse(x, y, 420, 420);

//player
strokeWeight(2);
stroke(0);
strokeCap(ELLIPSE);
fill(255);
ellipse(x, y, 30, 30);
strokeWeight(1);
fill(f1, f2, f3);
ellipse(x, y, 10, 10);

//laser
strokeWeight(8);
stroke(255, 0, 0);
strokeCap(SQUARE);
if (mousePressed && overCircle(x, y, 420) == true) {
line(x, y, mouseX, mouseY);
strokeWeight(3);
stroke(255, 140, 0);
line(x, y, mouseX, mouseY);
f1 = 255;
f2 = 0;
f3 = 0;
} else {
f1 = 255;
f2 = 255;
f3 = 255;
}

strokeWeight(1);
stroke(0);
strokeCap(ELLIPSE);

//hotbar
fill(0, 140);
rect(-1, 530, 1000, 70);

//hotbar - hearth and armor points
strokeWeight(19);
stroke(60);
strokeCap(SQUARE);
line(33, 578, 192, 578);
line(33, 552, 122, 552);
strokeWeight(15);
stroke(74, 189, 189);
if (ap > 35) {
line(35, 552, ap, 552);
}
stroke(204, 78, 71);
if (hp > 35) {
line(35, 578, hp, 578);
}
fill(255);
textSize(15);
if (ap > 35) {
text(ap - 35, 210, 557);
} else text(0, 208, 557);
if (hp > 35) {
text(hp - 35, 208, 583);
} else text(0, 208, 583);

strokeWeight(1);
stroke(0);
strokeCap(ELLIPSE);

//movement
countSpeed();   //Change speed based on current keys pressed.
changePosition(); //Change position based on speed.

xSpeed *= 0.9; //Apply some drag so that the square wouldn't fly off the screen indefinitely
ySpeed *= 0.9;
}

void countSpeed() {
if (x > 40 && keyLeft) xSpeed-= 0.5;
if (x < 970 && keyRight) xSpeed+= 0.5;
if (y > 40 && keyUp) ySpeed-= 0.5;
if (y < 500 && keyDown) ySpeed+= 0.5;
}

void changePosition() {
x+=xSpeed;
y+=ySpeed;
}

void keyPressed() {
if (key == 'w') {
keyUp = true;
}
if (key == 's') {
keyDown = true;
}
if (key == 'a') {
keyLeft = true;
}
if (key == 'd') {
keyRight = true;
}
}

void keyReleased() {
if (key == 'w') {
keyUp = false;
}
if (key == 's') {
keyDown = false;
}
if (key == 'a') {
keyLeft = false;
}
if (key == 'd') {
keyRight = false;
}
}

boolean overCircle(float x, float y, int diameter) {
float disX = x - mouseX;
float disY = y - mouseY;
if (sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
return true;
} else {
return false;
}
}

Basically there is a circle moving around and it can shoot a laser in its range. But there are no enemies to shoot.
What I want to do is have a lot of bullets spawning randomly and shooting in random directions. When one of them hits the player, players health gets low and when the player hits one with the laser; it goes away.
In order to do this, I made a bullet class:

class BulletSystem {
ArrayList<Bullet> particles;

BulletSystem() {
particles = new ArrayList<Bullet>();
}

}

void run() {
int i = particles.size()-1;
for (i = particles.size()-1; i >= 0; i = i - 1) {
Bullet p = particles.get(i);
p.display();
particles.remove(i);
}
}
}
}

class Bullet {

Bullet() {
}

float x1 = random(0,1000);
float y1 = random(0,600);
int w = 15;

//so here I thought would work but...
float r = random(0,3);
void display() {

if (r == 0) {
x1++;
y1++;
} else
if (r == 1) {
x1--;
y1++;
} else
if (r == 2){
x1++;
y1--;
} else
if (r == 3) {
x1--;
y1--;
}
//it did not.

//draw the bullet
fill(0);
ellipse(x1,y1,w,15);

//death to the bullet by laser
if (mousePressed && overCircle1(x,y,420) == true && overCircle(x1,y1,20) == true) {
w = 0;
x1 = -10;
y1 = -10;
}

//damage to the player by bullet
if (overCircle1(x,y,40) == true) {
if (ap > 0) ap = ap - 5;
else hp = hp - 5;
w = 0;
x1 = -10;
y1 = -10;
}
}

if (x1 == -10 && y1 == -10) {
return true;
} else {
return false;
}
}

boolean overCircle1(float x, float y, int diameter) {
float disX = x - x1;
float disY = y - y1;
if (sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
return true;
} else {
return false;
}
}

}

The damage, gone when shot with laser and random spawn parts are done. What I want now is to spawn a lot of them randomly and make them move randomly in random directions. But I canâ€™t quite figure out how to?

1 Like
2 Likes

Thanks a lot! That helped making a lot of them spawn randomly. Still need to figure out a way to do the movement though.

1 Like

The code above is my entire code (and there is no images, there is font tho I need to comment that out), I just added and array to spawn a lot of bullets. And in the bullet class look at the part where I commented.

???

Or did you update your code above?

I will update in a second just added an arraylist.
edit: @Chrisir updated!

int x = 500;
int y = 300;
int f1 = 255;
int f2 = 255;
int f3 = 255;
int ap = 120;
int hp = 190;
boolean showrange = true;
PFont font;
//float xSpeed = 0;
//float ySpeed = 0;

BulletSystem b1;

boolean keyLeft, keyRight, keyUp, keyDown;

void setup() {
size(1000, 600);
frameRate(1000);
cursor(CROSS);
font = createFont("CenturyGothic-48.vlw", 16);
textFont(font);
b1 = new BulletSystem();
}

void draw() {
background(255);

noStroke();
strokeCap(ELLIPSE);
fill(89, 200, 222, 40);
if (showrange)
ellipse(x, y, 420, 420);

//player
strokeWeight(2);
stroke(0);
strokeCap(ELLIPSE);
fill(255);
ellipse(x, y, 30, 30);
strokeWeight(1);
fill(f1, f2, f3);
ellipse(x, y, 10, 10);

b1.run();

//laser
strokeWeight(8);
stroke(255, 0, 0);
strokeCap(SQUARE);
if (mousePressed && overCircle(x, y, 420) ) {
//line(x, y, mouseX, mouseY);
strokeWeight(3);
stroke(255, 140, 0);
// line(x, y, mouseX, mouseY);
f1 = 255;
f2 = 0;
f3 = 0;
} else {
f1 = 255;
f2 = 255;
f3 = 255;
}

strokeWeight(1);
stroke(0);
strokeCap(ELLIPSE);

//hotbar
fill(0, 140);
rect(-1, 530, 1000, 70);

//hotbar - hearth and armor points
strokeWeight(19);
stroke(60);
strokeCap(SQUARE);
line(33, 578, 192, 578);
line(33, 552, 122, 552);
strokeWeight(15);
stroke(74, 189, 189);
if (ap > 35) {
line(35, 552, ap, 552);
}
stroke(204, 78, 71);
if (hp > 35) {
line(35, 578, hp, 578);
}
fill(255);
textSize(15);
if (ap > 35) {
text(ap - 35, 210, 557);
} else text(0, 208, 557);
if (hp > 35) {
text(hp - 35, 208, 583);
} else text(0, 208, 583);

strokeWeight(1);
stroke(0);
strokeCap(ELLIPSE);

//movement
// countSpeed();   //Change speed based on current keys pressed.
// changePosition(); //Change position based on speed.

//xSpeed *= 0.9; //Apply some drag so that the square wouldn't fly off the screen indefinitely
//ySpeed *= 0.9;
}

void countSpeed() {
//if (x > 40 && keyLeft) xSpeed-= 0.5;
//if (x < 970 && keyRight) xSpeed+= 0.5;
//if (y > 40 && keyUp) ySpeed-= 0.5;
//if (y < 500 && keyDown) ySpeed+= 0.5;
}

void changePosition111() {
//x+=xSpeed;
//y+=ySpeed;
}

void keyPressed() {
if (key == 'w') {
keyUp = true;
}
if (key == 's') {
keyDown = true;
}
if (key == 'a') {
keyLeft = true;
}
if (key == 'd') {
keyRight = true;
}
}

void keyReleased() {
if (key == 'w') {
keyUp = false;
}
if (key == 's') {
keyDown = false;
}
if (key == 'a') {
keyLeft = false;
}
if (key == 'd') {
keyRight = false;
}
}

boolean overCircle(float x, float y,
int diameter) {
float disX = x - mouseX;
float disY = y - mouseY;
if (sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
return true;
} else {
return false;
}
}

// ==========================================================================================================

class BulletSystem {

ArrayList<Bullet> particles;

BulletSystem() {
particles = new ArrayList<Bullet>();
}

}

void run() {
for (int i = particles.size()-1; i >= 0; i--) {
Bullet p = particles.get(i);
p.display();
//  particles.remove(i);
}
}
}
}

// ==========================================================================================================

class Bullet {

float x1 = 500; //  random(0, 1000);
float y1 = 300; //  random(0, 600);
int w = 4;

float xspeed=1;
float yspeed=1;

//so here I thought would work but...
float r = random(0, 3);

// origin
PVector position = new PVector(x, y);

Bullet() {
// constr
float angle = update(mouseX, mouseY);
xspeed = cos(angle);
yspeed = sin(angle);
}

float update(int mx, int my) {
//determines angle to mouse
float angle = atan2(float(my)-(position.y), float(mx)-(position.x));
return angle;
}

void display() {

/*
if (r == 0) {
x1++;
y1++;
} else
if (r == 1) {
x1--;
y1++;
} else
if (r == 2) {
x1++;
y1--;
} else
if (r == 3) {
x1--;
y1--;
}
*/
//it did not.

//draw the bullet
fill(255, 0, 0);
noStroke();
ellipse(x1, y1,
w, w);

x1+=xspeed;
y1+=yspeed;

//death to the bullet by laser
if (mousePressed && overCircle1(x, y, 420) == true && overCircle(x1, y1, 20) == true) {
//    w = 0;
// x1 = -10;
y1 = -10;
}

//damage to the player by bullet
if (overCircle1(x, y, 40) == true) {
if (ap > 0) ap = ap - 5;
else hp = hp - 5;
//w = 0;
//x1 = -10;
//y1 = -10;
}
}

if (x1 == -10 && y1 == -10) {
return true;
} else {
return false;
}
}

boolean overCircle1(float x, float y, int diameter) {
float disX = x - x1;
float disY = y - y1;
if (sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
return true;
} else {
return false;
}
}
}
//
2 Likes

Hey,
Thanks so much!
That was not what I wanted exactly(I can probably do it too based on this) on this post but it was another thing I was trying to implement in this game (I couldnâ€™t so I added the laser lol). And also I didinâ€™t know cursor() function existed also thanks for adding that.

1 Like

Great!

You are welcome!

1 Like