How to make object shoot in Processing

Hi everyone, I’m doing my Processing homework and I’ve got my object but I can’t get it to shoot. Can someone tell me the correct code please? I have the following so far:

int x = 30;
int y = 30;


void setup(){
size(500,500);

}
 void draw(){
   background(0);
   fill(255,56,99); 
ellipse(x,y,60,60);

}
void keyPressed(){
  if (key == 's') y = y + 8;
  if (key == 'w') y = y - 8;
}

void bullet (){
float gunX = 30;
float bulletX = 30;
float bulletY = 30;
ellipse(gunX, y,60,60);
bulletX = gunX +200; 
ellipse(bulletX,bulletY,60,60);
if (key == 'd') x = x+200;
}

Think about how shooting works.

When a key (use Space Bar which is ’ ')
set a marker boolean bulletFlies to true.

Before setup () : boolean bulletFlies=false;

When it’s true increase the bulletX position and display the bullet

call this from draw() please

of course in a real game you can shoot multiple bullets from different positions that fly on the same time. So a few bullets are visible and animated at the same time

see Someone please help solving a problem : shooting bullets - #4 by Chrisir

Hey do you know how to do it without using classes?

Yes, I know.

Please read my advice and change your code accordingly. Play with it and find the right solution.

It’s forum policy not to provide full code solutions

Okay thank you! I’m starting to understand it now :slight_smile:

1 Like

Make the changes and post your entire code please when you have questions

For example the gun is visible throughout and attached to the player (at a small distance)

Hence gun is shown in draw() throughout

Bullet is different, they appear upon firing

This is the code that I have. I’m able to shoot my bullet but unable to shoot one bullet at a time. Would I have to use another Boolean variable so I can fire a bullet only when it leaves the screen?

int x = 30;
int y = 60;
float gunX = x+20;
float gunY = y+20;
boolean bulletFlies=false;
float bulletX = 20;
float bulletY = 20;

void setup() {
size(500, 500);
}

void draw() {
background(0);
fill(255, 56, 99);
ellipse(x, y, 60, 60);
gunX = x+12;
gunY = y;
bullet();
}

void keyPressed() {
if (key == ‘s’) y = y + 8;
else if (key == ‘w’) y = y - 8;
else if (key == ’ ') {
bulletFlies=true;
bulletX = gunX+14;
bulletY = gunY+4;
}
}

void bullet() {
if (bulletFlies) {
ellipse(bulletX, bulletY, 6, 6);
bulletX = bulletX+5;
}
}

Yes that would totally work!
You could also directly call a function

boolean can_shoot(){...}

that would tell you if the bullet left the screen (using the built-in variables width and height) and call that function when the right key is pressed to ensure that besides pressing the space bar, no other bullet is left on screen.

Be careful though as the first bullet will always be considered as ‘on screen’ with that because it initial position is (20, 20) which is on screen, so one easy way to work around that (though it is not proper at all) is to init the bullet position off screen, or you could just add a condition in the can_shoot() function to check whether the bullet is flying (which is cleaner).

Hi

Nice game