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);
font = loadFont("CenturyGothic-48.vlw");
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);
b1.addParticle();
//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 addParticle() {
particles.add(new 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();
if (p.isDead()) {
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;
}
}
boolean isDead() {
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?