I need help with Bullets and targets
in
Programming Questions
•
4 months ago
I am new at Processing and have to make a gun and a moving target. When the gun is clicked, a projectile must fire from the gun towards the right side of the screen, and if it's in the target, it must expand. Please help!!
// - - - -
float gravity_x;
float gravity_y;
// - - - -
float tyPos;
float txPos;
float targetVelocity;
float diam;
// - - - -
float gunVelocity;
float gxPos;
float gyPos;
boolean shot;
ArrayList<Particle> particles = new ArrayList<Particle>();
void setup(){
size(500, 500);
targetVelocity= -3;
gunVelocity = 2;
gravity_x = 0.023;
gravity_y = 3;
tyPos = 400;
txPos = 300;
gxPos = 10;
gyPos = 200;
diam = 100;
for (int i = 0; i < 2; i++) {
Particle newParticle = new Particle(gxPos, gyPos, 10, 5);
particles.add(newParticle);
}
}
class Particle {
float xPos, yPos, xVel, yVel;
Particle(float xPos, float yPos, float xVel, float yVel) {
this.xPos = xPos;
this.yPos = yPos;
this.xVel = xVel;
this.yVel = yVel;
}
void shootBullet(){
xPos += xVel;
xPos += -gravity_x;
yPos += gravity_y;
if (abs(xPos - width/2 - 30) > width/2 - 16) {
xPos = -30;
}
if (abs(yPos - height/2 - 30) > height/2 - 16) {
yPos = -30;
}
}
void drawBullet(){
pushMatrix();
fill(0);
translate(xPos, yPos);
ellipse(0, 0, 32, 32);
popMatrix();
}
}
void draw() {
background(255);
drawTarget();
drawGun();
}
void drawGun() {
fill(0);
rect(gxPos, gyPos, 60, 20);
rect(gxPos, gyPos, 20, 50);
gyPos += gunVelocity;
if (gyPos <= 0) {
gunVelocity = -gunVelocity;
}
if (gyPos + 50 >= 499) {
gunVelocity = -gunVelocity;
}
}
void drawTarget(){
noStroke();
fill(255, 0, 0);
ellipse(txPos, tyPos, diam, diam);
fill(255);
ellipse(txPos,tyPos, 80, 80);
fill(255, 0, 0);
ellipse(txPos, tyPos, 60, 60);
fill(255);
ellipse(txPos, tyPos, 40, 40);
tyPos += targetVelocity;
if (tyPos - 50 <= 0) {
targetVelocity = -targetVelocity;
}
if (tyPos + 50 >= 499) {
targetVelocity = -targetVelocity;
}
}
void shouldShoot(){
}
void mouseClicked() {
println("clicked");
for (int i = 0; i < particles.size(); i++) {
Particle curParticle = particles.get(i);
curParticle.shootBullet();
curParticle.drawBullet();
}
if(isInTarget(mouseX, mouseY, txPos, tyPos, diam/2)) {
//EXPLODE (expand and fade to invisibility at a constant rate, reappear after)
diam++;
shot = true;
}
}
boolean isInTarget(float x, float y, float a, float b, float r) {
if (dist(x, y, a, b) <=r) {
return true;
}
else {
return false;
}
}
1