you can ignore the particles i just added them cause i was bored
int x = 30;
int y = 60;
float gunX = x+20;
float gunY = y+20;
float circleY;
float circleX;
float circleRadius;
float circleDiameter;
boolean bulletFlies=false;
float bulletX;
float bulletY;
ArrayList<Particle> particles;
void setup() {
size(500, 500);
noStroke();
circleY = random(25, 480);
circleX = 450;
circleRadius = 80;
circleDiameter = circleRadius * 2;
particles = new ArrayList<Particle>();
}
void draw() {
background(0);
fill(255, 56, 99);
ellipse(x, y, 60, 60);
if(bulletFlies)
ellipse(bulletX, bulletY, 6, 6);
gunY = y;
fill(255);
circle(circleX, circleY, circleDiameter);
circleX=circleX-0.5;
bullet();
for (int i = particles.size() - 1; i >= 0; i--) {
Particle p = particles.get(i);
p.update();
p.present();
if (p.ttl <= 0) {
particles.remove(i);
}
}
}
void keyPressed() {
if (key == 's') y = y + 8;
else if (key == 'w') y = y - 8;
else if (key == ' ') {
if (!bulletFlies) {
bulletFlies=true;
bulletX = gunX+14;
bulletY = gunY+4;
}
}
}
void bullet() {
if (bulletFlies) {
//check the bullet hasn't left the screen
if (bulletX > width) {
bulletFlies = false;
return;
}
bulletX = bulletX+5;
//calculate the distance between the bullet and the circle
float dx = bulletX - circleX;
float dy = bulletY - circleY;
float dist = sqrt(dx * dx + dy * dy);
if (dist < circleRadius) {
//the target has been hit so lets explode it and reset its position
explode();
//reset the bullet as well
bulletX = gunX+14;
bulletY = gunY+4;
bulletFlies = false;
}
}
}
void explode() {
int amt = int(random(10, 50));
for (int i = 0; i < amt; i++) {
Particle p = new Particle(circleX, circleY, 100);
p.addForce(random(-5, 5), random(-5, 5));
particles.add(p);
}
//reset the circle so it appears there is a new one
circleY = random(25, 480);
circleX = width + circleRadius;
}
class Particle {
PVector pos, vel, acc;
int ttl;
Particle(float x, float y, int ttl) {
this.pos = new PVector(x, y);
this.vel = new PVector();
this.acc = new PVector();
this.ttl = ttl;
}
void addForce(float x, float y) {
this.acc.x += x;
this.acc.y += y;
}
void update() {
this.vel.add(this.acc);
this.acc.mult(0);
this.pos.add(this.vel);
this.ttl--;
}
void present() {
fill(color(255, 255, 255, (ttl / 100.0) * 255));
// fill(255);
ellipse(this.pos.x, this.pos.y, 10, 10);
}
}
this is the pertinent piece of code for determining when to reset the target
float dx = bulletX - circleX;
float dy = bulletY - circleY;
float dist = sqrt(dx * dx + dy * dy);
if (dist < circleRadius) {
//the target has been hit so lets explode it and reset its position
explode();
//reset the bullet as well
bulletX = gunX+14;
bulletY = gunY+4;
bulletFlies = false;
}
edit: you really want to start packaging things into classes at this point. it will make things so much easier for yourself. also if you are looking for some good beginner tutorials you can’t go wrong with these by codeincomplete