# How to make object shoot in Processing

**URL:** <https://discourse.processing.org/t/how-to-make-object-shoot-in-processing/32087>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [September 4, 2021, 7:33am UTC](https://discourse.processing.org/t/how-to-make-object-shoot-in-processing/32087 "2021-09-04T07:33:55Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![NewCoder1](https://avatars.discourse-cdn.com/v4/letter/n/c6cbf5/32.png) [@NewCoder1](https://discourse.processing.org/u/NewCoder1)\
**Post date:** [September 4, 2021, 7:33am UTC](https://discourse.processing.org/t/how-to-make-object-shoot-in-processing/32087/1 "2021-09-04T07:33:55Z")

</div>

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:

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 4, 2021, 9:34am UTC](https://discourse.processing.org/t/how-to-make-object-shoot-in-processing/32087/2 "2021-09-04T09:34:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 4, 2021, 9:35am UTC](https://discourse.processing.org/t/how-to-make-object-shoot-in-processing/32087/3 "2021-09-04T09:35:41Z")

</div>

> [@NewCoder1](#):
>
> bullet ()

call this from draw() please

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 6, 2021, 8:05pm UTC](https://discourse.processing.org/t/how-to-make-object-shoot-in-processing/32087/4 "2021-09-06T20:05:05Z")

</div>

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](https://discourse.processing.org/t/someone-please-help-solving-a-problem-shooting-bullets/21210/4)

---

<div class="post-metadata">

**Author:** ![NewCoder1](https://avatars.discourse-cdn.com/v4/letter/n/c6cbf5/32.png) [@NewCoder1](https://discourse.processing.org/u/NewCoder1)\
**Post date:** [September 10, 2021, 8:11am UTC](https://discourse.processing.org/t/how-to-make-object-shoot-in-processing/32087/5 "2021-09-10T08:11:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 10, 2021, 8:52am UTC](https://discourse.processing.org/t/how-to-make-object-shoot-in-processing/32087/6 "2021-09-10T08:52:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![NewCoder1](https://avatars.discourse-cdn.com/v4/letter/n/c6cbf5/32.png) [@NewCoder1](https://discourse.processing.org/u/NewCoder1)\
**Post date:** [September 14, 2021, 5:42am UTC](https://discourse.processing.org/t/how-to-make-object-shoot-in-processing/32087/7 "2021-09-14T05:42:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 14, 2021, 10:36am UTC](https://discourse.processing.org/t/how-to-make-object-shoot-in-processing/32087/8 "2021-09-14T10:36:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![NewCoder1](https://avatars.discourse-cdn.com/v4/letter/n/c6cbf5/32.png) [@NewCoder1](https://discourse.processing.org/u/NewCoder1)\
**Post date:** [September 17, 2021, 4:06am UTC](https://discourse.processing.org/t/how-to-make-object-shoot-in-processing/32087/9 "2021-09-17T04:06:34Z")

</div>

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;  
}  
}

---

<div class="post-metadata">

**Author:** ![gitanerie](https://avatars.discourse-cdn.com/v4/letter/g/b5a626/32.png) [@gitanerie](https://discourse.processing.org/u/gitanerie)\
**Post date:** [September 19, 2021, 12:33pm UTC](https://discourse.processing.org/t/how-to-make-object-shoot-in-processing/32087/10 "2021-09-19T12:33:19Z")

</div>

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

```auto
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).

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 19, 2021, 7:33pm UTC](https://discourse.processing.org/t/how-to-make-object-shoot-in-processing/32087/11 "2021-09-19T19:33:20Z")

</div>

> [@How to make bullet shoot only when the previous bullet is off the screen](https://discourse.processing.org/t/how-to-make-bullet-shoot-only-when-the-previous-bullet-is-off-the-screen/32314/2):
>
> this would be enough: just check if the bulletFlies and if so, leave else if (key == ' ') { if (bulletFlies) return; // leave bulletFlies=true; bulletX = gunX+14; bulletY = gunY+4; }

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [September 20, 2021, 3:18am UTC](https://discourse.processing.org/t/how-to-make-object-shoot-in-processing/32087/12 "2021-09-20T03:18:01Z")

</div>

Hi

Nice game

> [@Game from Japan](https://discourse.processing.org/t/game-from-japan/28639):
>
> import fisica.\*; FWorld world; FBox paddle; FCircle ball; int score=0; void setup() { size(600, 600, P3D); noStroke(); textAlign(CENTER); textSize(50); Fisica.init(this); world=new FWorld(); world.setGravity(0, 0); world.setEdges(0, 0, width, height); world.remove(world.bottom); for (int i=0; i\<8; i++) { for (int j=0; j\<5; j++) { FBox box=new FBox( 60, 20); box.setFill(j\*50, j, 150); box.setPosition(i\*70+50, 30\*j+30); box.setStatic(true); …
