<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
      <title>Tagged with #bullet - Processing 2.x and 3.x Forum</title>
      <link>https://forum.processing.org/two/discussions/tagged/feed.rss?Tag=%23bullet</link>
      <pubDate>Sun, 08 Aug 2021 19:14:50 +0000</pubDate>
         <description>Tagged with #bullet - Processing 2.x and 3.x Forum</description>
   <language>en-CA</language>
   <atom:link href="/two/discussions/tagged%23bullet/feed.rss" rel="self" type="application/rss+xml" />
   <item>
      <title>Bullet runs into issue when collided with more then one enemy</title>
      <link>https://forum.processing.org/two/discussion/27928/bullet-runs-into-issue-when-collided-with-more-then-one-enemy</link>
      <pubDate>Mon, 07 May 2018 00:59:21 +0000</pubDate>
      <dc:creator>ctrembla</dc:creator>
      <guid isPermaLink="false">27928@/two/discussions</guid>
      <description><![CDATA[<p>Hey, I'm making a space invaders game &amp; I made a bunch of different kind of bullets the player can use, but when a bullet collides with 2 enemies it gives me a bullet remove error.
<code>for (int i = Bullet.size()-1; i &gt;= 0; i--)
{ bullet bu = Bullet.get(i); 
bu.show(); 
bu.move(); 
for (int j = Enemy.size()-1; j &gt;= 0; j--)
{ enemy en = Enemy.get(j); 
float d = dist(bu.x,bu.y,en.x,en.y); 
if (d &lt; bu.r + en.r){ 
Bullet.remove(i); 
en.life -= 1; } } }</code></p>

<p>anyone know a way to fix it so my bullet ca hit 2 enemies without an error. I think the trouble is that both enemies it collided with are trying to remove it at the same time.</p>
]]></description>
   </item>
   <item>
      <title>{Basic} How to draw and add more "bullets" as soon as there is space for them?</title>
      <link>https://forum.processing.org/two/discussion/26707/basic-how-to-draw-and-add-more-bullets-as-soon-as-there-is-space-for-them</link>
      <pubDate>Thu, 08 Mar 2018 05:44:15 +0000</pubDate>
      <dc:creator>wakandaforever69</dc:creator>
      <guid isPermaLink="false">26707@/two/discussions</guid>
      <description><![CDATA[<p>Hey guys, I'm very new to processing and I'm having a lot of trouble with coming up with the logic of what I need to do.</p>

<p>Basically I'm just making a program which</p>

<p>1) will shoot a bullet from near the bottom of the window when I press the mouse, then travel up, then disappears as it hits near the top of the window. Also</p>

<p>2) as soon as there is enough spacing, another bullet will appear under it and travel with it as well, along with more under the newly appearing bullets. New bullets will keep being added until the first bullet reaches near the top of the page.</p>

<p>Here is what I have so far. Thank you to anyone who can help, and if there is any way to improve my amateur code please let me know! much appreciated, thank you!</p>

<hr />

<p>// bullets
int bullets = 1;</p>

<p>float BY;</p>

<p>float BX = 250;</p>

<p>int bulletSize;</p>

<p>final float BULLET_SPEED = 45;</p>

<p>final float BULLET_SPACING = 50;</p>

<p>final float moving = 0.5;</p>

<p>boolean shoot = false;</p>

<p>int n = 0;</p>

<p>void setup() {</p>

<p>size(500,500);</p>

<p>bulletSize = width/12;
}</p>

<p>void draw(){</p>

<p>background(255);</p>

<p>moveShot();</p>

<p>drawShot();</p>

<p>}</p>

<p>void mousePressed() {</p>

<p>if (!shoot) {</p>

<pre><code>BX = 250; 

BY = 450;
</code></pre>

<p>}</p>

<p>shoot = true;</p>

<p>}</p>

<p>void drawShot() {</p>

<p>fill(0);</p>

<p>if (shoot) {</p>

<pre><code> int i = 0;

 while (i &lt; 10) {
  ellipse(BX, BY, BULLET_SIZE, BULLET_SIZE);
  ellipse(BX, BY + (n * BULLET_SPACING), BULLET_SIZE, BULLET_SIZE);
  i++;
 }
</code></pre>

<p>}</p>

<p>if (BY &lt; 50) {</p>

<pre><code>shoot = false;
</code></pre>

<p>}</p>

<p>}</p>

<p>void moveShot() {</p>

<p>if (shoot &amp;&amp; BY &gt; 25) {</p>

<pre><code>BY -= moving;

if (BY % BULLET_SPACING == 0) 

 n++;
</code></pre>

<p>}</p>

<h2>}</h2>
]]></description>
   </item>
   <item>
      <title>How can I make my bullet class move with my ship and rotate when it rotates?</title>
      <link>https://forum.processing.org/two/discussion/26049/how-can-i-make-my-bullet-class-move-with-my-ship-and-rotate-when-it-rotates</link>
      <pubDate>Sat, 20 Jan 2018 15:59:27 +0000</pubDate>
      <dc:creator>Maysin</dc:creator>
      <guid isPermaLink="false">26049@/two/discussions</guid>
      <description><![CDATA[<p>Hello, I am trying to make an asteroids game for my video game programming class, and I want to make my bullet circle move with my ship so it shoots out of the ship instead of just shooting from the center of the screen. Here is my code, and a link to the the whole program. <a rel="nofollow" href="https://drive.google.com/file/d/1LIeWhYXvugX_rXlFhEq_e2arq0PlHB8a/view?usp=sharing"><strong>Google Drive Link</strong></a>
Thanks, Mason</p>

<pre><code>import processing.sound.*; //Import the processing sound library into the program
SoundFile shoot, thrust; //Set the shooting sound &amp; the thrust sound
float a1X, a1Y, a2X, a2Y, a3X, a3Y, aD1, aD2, aD3, hbX, hbY, SmvX, SmvY, sAng, sRot, shotMV;
float clickX, clickY;
float bDia = 200;
float sX = 900, sY = 500;
float dbW; //Damage Bar
float bX = sX; //Bullet X = Ship X
float bY = sY - 25; //Bullet Y = Ship Y-25
float speedVar = 0.1; //Speed Variable for the ship to accelerate at
float sDia = 35; //The ships collision circle 
int bW = 4, bH = 4; //Bullet width &amp; height
float bSpd = 10; //Bullet speed
color red = color(255, 0, 0); //Color variable to make the bullet red
PImage ship, a1, a2, a3, sViz; //Image variables
PFont pixel; //Font variable
boolean accel = false; //Boolean for the ship to accelerate or not
boolean shot = false; //Boolean for if the bullet is shot
boolean rules = true; //Boolean for if the rules need to be displayed or not
String ruletext = "Use W to thrust forward\n \nUse A and D keys to\nrotate the ship left and right\n\nPress space to shoot the asteroids\n\n\nClick anywhere to start";
Bullet bullet;

void setup() {
  size(1800, 1000); //Size of the window
  background(0); //Background color
  sX = 900; //Ship X location
  sY = 500; //Ship Y location
  a1X = 1000; //Asteroid1 X location
  a1Y = 100; //Asteroid1 Y location
  a2X = 400; //Asteroid1 X location
  a2Y = 700; //Asteroid1 Y location
  a3X = 1600; //Asteroid1 X location
  a3Y = 800; //Asteroid1 Y location
  aD1 = 100;
  aD2 = 100;
  aD3 = 80;
  hbX = 1750; //Health bar rectangle X location variable
  hbY = 50; //Health bar rectangle Y location variable
  dbW = 0; //Damage bar variable
  SmvX = 0; //The ships X movement variable
  SmvY = 0; //The ships Y movement variable
  shoot = new SoundFile(this, "shoot.mp3"); //Load the shoot soundfile
  thrust = new SoundFile(this, "thrust.mp3"); //Load the thrust soundfile
  ship = loadImage("data/ship.png"); //Load the ship image
  a1 = loadImage("a1.png"); //Load asteroid1 image
  a2 = loadImage("a2.png"); //Load asteroid2 image
  a3 = loadImage("a3.png"); //Load asteroid3 image
  sViz = ship; //The ships visible sprite
  pixel = createFont("pixel.TTF", 24); //Load the font
  textAlign(CENTER, CENTER); //Make the text align centered
}

void draw() {
  if (rules == true) { //If rules is true then display the rules
    fill(255, 0, 0);
    textFont(pixel);
    textSize(52);
    text(ruletext, 900, 500);
    if (mousePressed) {
      rules = false; //When the screen is clicked stop displaying the rules
    }
  }

  if (rules == false) { //When rules are gone, start the game
    fill(0); //Fill the rectangle below with black
    rect(-1, -1, 1801, 1001); //Make a ractangle the size of the screen
    fill(50, 255, 100); //Fill the rectangle below with green
    rect(hbX, hbY, -80, 900, 7); //Make the health bar rectangle
    fill(red); //Fill the rectangle below with red
    rect(1750, 50, -80, dbW, 7); //Make the damage bar rectangle
    stroke(255); //Set the stroke to white

    imageMode(CENTER); //Sets the image to be centered from the center
    pushMatrix();
    translate(sX, sY);
    rotate(radians(sAng));
    image(sViz, 0, 0, 80, 80); //Shows an image on the screen
    popMatrix();

    image(a1, a1X, a1Y, 100, 100);
    image(a2, a2X, a2Y, 100, 100);
    image(a3, a3X, a3Y, 80, 80);

    a1X = a1X-random(25);
    a1Y = a1Y+random(25);
    a2X = a2X-random(25);
    a2Y = a2Y-random(25);
    a3X = a3X-random(25);
    a3Y = a3Y+random(25);

    noStroke();
    noFill();
    ellipse(a1X, a1Y, 100, 100);
    ellipse(a2X, a2Y, 100, 100);
    ellipse(a3X, a3Y, 100, 100);

    if (dist(sX, sY, a1X, a1Y) &lt; sDia/2 + aD1/2) {
      dbW = dbW+10;
    }
    else if (dist(sX, sY, a2X, a2Y) &lt; sDia/2 + aD2/2) {
      dbW = dbW+10;
    }
    else if (dist(sX, sY, a3X, a3Y) &lt; sDia/2 + aD3/2) {
      dbW = dbW+10;
    }

    clickX = mouseX;
    clickY = mouseY;

    if (accel == true) {
      SmvX += cos(radians(sAng)); //If accel is true, then make the image move
      SmvY += sin(radians(sAng));
    }

    sX = sX + SmvX * speedVar;
    sY = sY + SmvY * speedVar;

    sAng = (sAng + sRot);

    if (shot == true) {
      bullet.move();
      bullet.display();
    }

    if (sX &lt; -100) { //Has an image gone off the left edge?
      sX = 1900; //Teleport image to right edge
    } else if (sX &gt; 1900) { //Has an image gone off the right edge?
      sX = 0; //Teleport image to left edge
    }
    if (sY &lt; -100) { //Has an image gone off the top edge?
      sY = 1100; //Teleport image to bottom edge
    } else if (sY &gt; 1100) { //Has an image gone off the bottom edge?
      sY = 0; //Teleport image to top edge
    }

    if (a1X &lt; -100) { //Has an image gone off the left edge?
      a1X = 1900; //Teleport image to right edge
    } else if (a1X &gt; 1900) { //Has an image gone off the right edge?
      a1X = 0; //Teleport image to left edge
    }
    if (a1Y &lt; -100) { //Has an image gone off the top edge?
      a1Y = 1100; //Teleport image to bottom edge
    } else if (a1Y &gt; 1100) { //Has an image gone off the bottom edge?
      a1Y = 0; //Teleport image to top edge
    }

    if (a2X &lt; -100) { //Has an image gone off the left edge?
      a2X = 1900; //Teleport image to right edge
    } else if (a2X &gt; 1900) { //Has an image gone off the right edge?
      a2X = 0; //Teleport image to left edge
    }
    if (a2Y &lt; -100) { //Has an image gone off the top edge?
      a2Y = 1100; //Teleport image to bottom edge
    } else if (a2Y &gt; 1100) { //Has an image gone off the bottom edge?
      a2Y = 0; //Teleport image to top edge
    }

    if (a3X &lt; -100) { //Has an image gone off the left edge?
      a3X = 1900; //Teleport image to right edge
    } else if (a3X &gt; 1900) { //Has an image gone off the right edge?
      a3X = 0; //Teleport image to left edge
    }
    if (a3Y &lt; -100) { //Has an image gone off the top edge?
      a3Y = 1100; //Teleport image to bottom edge
    } else if (a3Y &gt; 1100) { //Has an image gone off the bottom edge?
      a3Y = 0; //Teleport image to top edge
    }

    if (dbW &gt;= 900) { //If the player has taken enough damage, end the game
      fill(0);
      rect(-1, -1, 1801, 1001); //Make a ractangle the size of the screen
      fill(red);
      ellipse(400, 600, bDia, bDia); // Draw the left button.
      ellipse(1400, 600, bDia, bDia); // Draw the right button. 
      text("Click this to\n play again", 400, 350);
      text("Click this to\n exit", 1400, 350);
    }
  }
}

void keyPressed() {
  if (key == ' ') {
    if (shot == false) {
      bullet = new Bullet(bX, bY, bSpd, bW, bH, red, red);
      bullet.display();
      shoot.play();
      shot = true;
    }
  } else if (key == 'w') {
    accel = true;
    thrust.play();
  } else if (key == 'a') { //If a is pressed, rotate the image left
    sRot = -3.0;
  } else if (key == 'd') { //If d is pressed, rotate the image left
    sRot = 3.0;
  } else if (key == 'l') { //If d is pressed, rotate the image left
    dbW = dbW+50;
  }
}

void keyReleased() {

  if (key == 'w') { //If s is released, stop thrusting
    accel = false;
  } else if (key == 'a') {  //If a is released, stop rotating the image
    sRot = 0.0;
  } else if (key == 'd') { //If d is released, stop rotating the image
    sRot = 0.0;
  }
}

void mousePressed() {
  if (dist(clickX, clickY, 400, 600) &lt; bDia/2) {
    rules = true;
    dbW = 0;
  }

  //Check if the user clicked the right button.
  else if (dist(clickX, clickY, 1400, 600) &lt; bDia/2) {
    exit();
  }
}


class Bullet {
  color bFill, bStroke;
  int bW = 2, bH = 2;
  float bX, bY, bSpd;

  Bullet(float bX, float bY, float bSpd, int bW, int bH, color bFill, color bStroke) {
    this.bX = bX;
    this.bY = bY;
    this.bSpd = bSpd;
    this.bW = bW;
    this.bH = bH;
    this.bFill = bFill;
    this.bStroke = bStroke;
  }

  void display() {
    fill(this.bFill);
    stroke(this.bStroke);
    ellipse(this.bX, this.bY, this.bW, this.bH);
  }

  void move() {
    this.bY -= this.bSpd;
    if (this.bY &lt; 1) {
      this.bY = 500;
      shot = false;
    }
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Need help making a colission</title>
      <link>https://forum.processing.org/two/discussion/25902/need-help-making-a-colission</link>
      <pubDate>Wed, 10 Jan 2018 17:57:11 +0000</pubDate>
      <dc:creator>oogabooga</dc:creator>
      <guid isPermaLink="false">25902@/two/discussions</guid>
      <description><![CDATA[<p>``I'm having trouble detecting the bullet collide with the enemy ship in my shooter game. Here's my code so far. Any ideas on how I can make it work?</p>

<pre><code>PImage background;
ship player= new ship();

int score;
PImage enemypic;
boolean enemyAlive = true;
boolean instructions = true;
boolean level1=true;
boolean gameOver=false;
enemy enemyArr [] = new enemy [11];
color rectColor = color (200);
ArrayList &lt;Bullet&gt; bullets;
void setup() {
size(1000, 750);
bullets = new ArrayList();
background=loadImage("shooterbackground.png");
ship=loadImage("ship.png");
enemypic = loadImage("enemy.png");
frameRate(13);
smooth();
noStroke();
float enemyY = 0;
for (int i = 0; i &lt; 11; i++) {

 enemyArr [i] = new enemy(900, 5 + enemyY);
enemyY +=65;
}
}
void draw() {
  println(frameRate);
image (background, 0, 0, 1000, 750); 

moveAll();
displayAll();
if (instructions) {
 fill(rectColor);
//rect(0, 0, 1000, 750);
fill(0);
text("afdsf", 0, 0);
          }
      if (instructions) {
background(200, 200, 200);
fill(225);
rect(300, 275, 400, 200);
fill(0);
textSize(65);
text("Start", 420, 390);
         }

       if (enemyAlive &amp;&amp; !instructions &amp;&amp; !gameOver) {

   player.display();
for (int i = 0; i &lt; 11; i++) {
  enemyArr[i].update();
  enemyArr[i].display();
}
}

if (playerX &gt; 244) {//player move limits
playerX = 244;
}
if (playerX &lt; 0) {
playerX = 0;
}
if (playerY &lt; -68) {//wrap screen
playerY = 740;
}
if (playerY &gt; 740) {
playerY = -68;
}
}  


void keyPressed() {
 if (key == 'a') {
  playerX -= 8;
 }
  if (key == 'd') {
  playerX+= 8;
 }
  if (key == 'w') {
  playerY -= 8;
 }
  if (key == 's') {
    playerY += 8;
   }
  }




   void mousePressed() {

   if (mouseX &gt; 300 &amp;&amp; mouseX &lt; 700 &amp;&amp;  mouseY &gt; 275 &amp;&amp; mouseY  &lt; 475 &amp;&amp; instructions) {
    instructions = false;
   }
   Bullet temp = new Bullet(playerX+65, playerY+34);
   bullets.add(temp);
   }



public class enemy {
  float health, speed;
  float yPos, xPos;


   enemy(float XPOS, float YPOS) {

    health=5;
     speed=10;
    xPos=XPOS;
    yPos=YPOS;
  }

  public void display() {
    image(enemypic, xPos, yPos, 75, 75);
   }
   public void update() {
     if (enemyAlive) {
     xPos -=7;
        }
   if (xPos &lt; -65) {
    xPos=925;
      }
            }
         }

     class Bullet{//bullet class

       float x=playerX +20;
      float y=playerY;
     float speed;
    Bullet(float tx, float ty){

        x = tx;
       y = ty;
     }
       void display(){

     fill(255, 0,0);
        rect(x,y, 10, 5);
      }
       void move(){

        x += 25;
      }

     void moveAll(){

     for (Bullet temp : bullets){

       temp.move();
        }
       }
       void displayAll(){

       for (Bullet temp : bullets){

           temp.display();
      }
       }

 PImage ship;
    int playerX = 100;
    int playerY = 300;

     public class ship {

 public void display() {
image(ship, playerX, playerY, 75, 75);
        }
    }
</code></pre>
]]></description>
   </item>
   <item>
      <title>How to make a bullet fire from the cannon angle</title>
      <link>https://forum.processing.org/two/discussion/23541/how-to-make-a-bullet-fire-from-the-cannon-angle</link>
      <pubDate>Thu, 20 Jul 2017 16:08:18 +0000</pubDate>
      <dc:creator>mozy</dc:creator>
      <guid isPermaLink="false">23541@/two/discussions</guid>
      <description><![CDATA[<p>Hi!
I have made a code for rotating the tank by pressing 'l' and 'r' and a bullet class using vectors and I have no idea how to make the bullet fire from the same angle as the cannon. Here is the code I wrote:</p>

<pre><code>PImage cannon;
PImage bullet;
int x = 250;
int y = 100;
float rota=0;
void setup() {
  size(700, 700);
  background(0);
  cannon = loadImage("cannona.png");
  bullet = loadImage("fireball.png");
  image(cannon, width/2, height/2, 100, 200);
}

void draw() {

}

void keyPressed() {

  switch(key) {
  case 'l':
    background(0);
    pushMatrix();
    imageMode(CENTER);
    translate(x+cannon.width/2, y+cannon.height/2);
    rotate(rota);
    image(cannon, 0, 0, 100, 200);
    translate(x+cannon.width/2, y+cannon.height/2);
    popMatrix();
    rota=rota+0.1;
    break;
  case 'r':
    background(0);
    pushMatrix();
    imageMode(CENTER);
    translate(x+cannon.width/2, y+cannon.height/2);
    rotate(rota);
    image(cannon, 0, 0, 100, 200);
    translate(x+cannon.width/2, y+cannon.height/2);
    popMatrix();
    rota=rota-0.1;
    break;
  }
}

class Bullet {

  PVector pos;
  PVector vel;
  PVector acc;
  float friction = 0.99;
  float speed;

  float direction; 



  Bullet(PVector _pos, float _direction, float _speed) {
    pos = _pos.copy();
    vel = new PVector();
    acc = new PVector();
    direction = _direction;
    speed = _speed;
    this.initVel();
  }

  void update() {

    vel.add(acc);
    vel.mult(friction);
    pos.add(vel);
    acc.mult(0);
  }

  void display() {
    stroke(0, 255, 0);
    strokeWeight(2);
    pushMatrix();
    translate(pos.x, pos.y);
    line(vel.x*0.5, vel.y*0.5, 0, 0);
    popMatrix();
  }


  void initVel() {
    PVector dir = new PVector(cos(direction), sin(direction));
    dir.mult(speed);
    this.applyForce(dir);
  }

  void applyForce(PVector _force) {
    acc.add(_force);
  }

  boolean isDone() {
    return (pos.x &lt; 0 || pos.x &gt; width || pos.y &lt; 0 || pos.y &gt; height);
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Circle collisions #2</title>
      <link>https://forum.processing.org/two/discussion/22753/circle-collisions-2</link>
      <pubDate>Thu, 25 May 2017 05:04:36 +0000</pubDate>
      <dc:creator>JaeY</dc:creator>
      <guid isPermaLink="false">22753@/two/discussions</guid>
      <description><![CDATA[<p>I still need one more help. I made a code of bullets that are fired from the red circle. I want to make it so that if the bullet collides with a grey circle, both the grey circle and the bullet are removed from the screen</p>

<pre><code>float radius=10;
int n=100;
float velocityY=3;
int xship=250; //SHIP X POSITION
int yship=500; //SHIP Y POSITION
float bulletX[]; //BULLETS X POSITION
float bulletY[]; //BULLETS Y POSITION
float x[];  // OBSTACLES X POSITIONS
float y[]; //OBSTACLES Y POSITIONS
boolean space; //SPACESHIP BULLET FIRE
int ammo=1;  //AMMO FOR BULLETS

void setup()
{
  size(500, 600);
  x = new float [n];
  y = new float [n];

  bulletX = new float [2];
  bulletY = new float [2];

  for (int i=0; i&lt;x.length; i++)
  {
    x[i]=random(0, width);   //RANDOMIZE OBSTACLES LOCATIONS
    y[i]=random(-600, 0);    //RANDOMIZE OBSTACLES Y LOCATIONS
  }
  noStroke();
  ellipseMode(RADIUS);
}


void draw()
{
  background(0);

  for (int i=0; i&lt;x.length; i++)
  {
    y[i]=y[i]+velocityY;

    //////////////////////////////COLLISION START////////////////////////////////////////
    if (contact(x[i], y[i])==true)
    {
      x[i]=random(0, width);
      y[i]=random(0, 0);
      xship=250; //restart spaceship position
      yship=500;
    }
    /////////////////////////////COLLISION END/////////////////////////////////////
    fill(120);
    ellipse(x[i], y[i], radius, radius); 

    if (y[i]&gt;height) //IF OBSTACLES HIT GROUND
    {
      //y[i]=0; //OBSTACLES START FROM TOP
      x[i]=random(0, 600);  //OBSTACLES RANDOMIZE AGAIN
      y[i]=random(0, 0);    //OBSTACLES RANDOMIZE AGAIN
    }
  }

  fill(124, 255, 60);
  ellipse(xship, yship, radius, radius); //SPACESHIP



  //////////////////////////////////BULLET SPEED AND LOCATION//////START////////
  for (int i=0; i&lt;bulletX.length; i++)
  {
    fill(255, 0, 0);
    ellipse(bulletX[i], bulletY[i], radius, radius); 
    bulletY[i]=bulletY[i]-2; ///BULLETS SPEED
    if (bulletcontact(bulletX[i], bulletY[i], radius, radius)==true) //bullets contact with obstacles
    {
      x[i]=random(0, width); 
      y[i]=random(0, 0);
      bulletY[i]=0;
    }
  }



  ////////////////////BULLET COMES OUT OF SPACESHIP /////////////////START////////////
  if (space == true &amp;&amp; ammo&gt;0) 
  {
    float _bulletX[]=append(bulletX, xship);
    float _bulletY[]=append(bulletY, yship-20);
    bulletX = _bulletX;
    bulletY = _bulletY;
    space = false;   
    ammo=ammo-1;  //AMMO COUNT TO 3 MAXIMUM ON SCREEN
  } 

  for (int i = 0; i &lt; bulletY.length; i += 1) 
  {
    if (bulletY[i] &lt;= 0) 
    { 
      ammo += 1;
      arrayCopy(bulletX, i+1, bulletX, i, bulletX.length-i-1);
      bulletX = shorten(bulletX);
      arrayCopy(bulletY, i+1, bulletY, i, bulletY.length-i-1);
      bulletY = shorten(bulletY);
    }
  }
}
////////////////////BULLET COMES OUT OF SPACESHIP///////////////////END////////////////////////

boolean contact(float x2, float y2)
{
  return (dist(xship, yship, x2, y2))&lt;2*radius;
}
boolean bulletcontact(float x3, float y3, float x4, float y4)
{
  return (dist(x3, y3, x4, y4))&lt;=2*radius;
}

void keyPressed()
{
  if (key == ' ') 
  {
    space = true;
  }
}

void keyReleased()
{
  fill(255);
  if (key == ' ') 
  {
    space = false;
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Shooting a bullet after rotating the cannon</title>
      <link>https://forum.processing.org/two/discussion/22072/shooting-a-bullet-after-rotating-the-cannon</link>
      <pubDate>Tue, 18 Apr 2017 15:47:59 +0000</pubDate>
      <dc:creator>MPigeon</dc:creator>
      <guid isPermaLink="false">22072@/two/discussions</guid>
      <description><![CDATA[<p>Hello, I'm quite new to programming in Processing and I'm making a simple shooting game. Basically there's a tank and the user is able to rotate the gun/cannon and shoot, but I don't know how to make the missile appear in the right location and at the right angle after rotating the cannon. I will add another tank when I get the shooting mechanics right. This is the code that I've got so far:</p>

<pre><code>PVector tankLocation;
float theta = 0.01;

void setup() {
  size(1200, 800);
  tankLocation = new PVector(80, 575);
}

void draw() {
  noStroke();
  background(135, 206, 235);
  fill(34, 139, 34);
  ellipse(520, 800, 1600, 450);
  fill(85, 107, 47);

  rotate(-0.12);
  rect(tankLocation.x, tankLocation.y, 80, 40); // Tank
  translate(135, 584);
  rotate(theta);
  rect(0, 0, 70, 10); // Gun

  if (keyPressed == true) {
    if (keyCode == UP) {
      theta = theta - 0.05;
      if (theta&lt;-0.49) { // Prevents rotating the gun too much
        theta = -0.49;
      }
    }
    if (keyCode == DOWN) {
      theta = theta + 0.05;
      if (theta&gt;=0.01) {
        theta = 0.01;
      }
    }
    if (key == 's') {

    }
  }
}
</code></pre>

<p>How can I alter it so I could shoot missiles at an angle by pressing the key 's'? Help would be greatly appreciated. :)</p>
]]></description>
   </item>
   <item>
      <title>Help with object collision</title>
      <link>https://forum.processing.org/two/discussion/21154/help-with-object-collision</link>
      <pubDate>Sat, 04 Mar 2017 18:49:39 +0000</pubDate>
      <dc:creator>Kang01</dc:creator>
      <guid isPermaLink="false">21154@/two/discussions</guid>
      <description><![CDATA[<p>I am currently working on a game where you shoot stuff down in space
and I cant figure out how to check if the bullets are hitting the targets
heres the code</p>

<pre><code>ArrayList &lt;Bullet&gt; bullets;
float numOfStars = 500;
PImage spaceShip;


float [] x = new float[int(numOfStars)];
float [] y = new float[int(numOfStars)];
float [] speed = new float[int(numOfStars)];
float [] bullet;
//Bullet = new Bullet();
float bulletSpeed;
boolean Bullet;










void setup() {
   bullets = new ArrayList();
  fullScreen();
  //size(500, 400);
  background(0);
  stroke(255);
  spaceShip = loadImage("Spaceship2.png");

  noCursor();



  int i = 0;
  while (i &lt; numOfStars) {
    x[i]= random(0, width);
    y[i] = random(0, height);
    speed[i] = random(1, 5);
    i = i +1;
  }
}


void draw() {
  background(0);
  removeToLimit(100);
  moveAll();
  displayAll();
  //bulletSpeed = bulletSpeed +5;







  //draw triangle
  //triangle(mouseX,mouseY-6,mouseX +20, mouseY,mouseX, mouseY +6);

  //rotate(radians(180));{
  image(spaceShip, mouseX-10, mouseY);
  //}

  int i = 0;
  while (i &lt; numOfStars) {
    float co = map(speed[i], 1, 5, 100, 255);
    stroke(co);
    strokeWeight(speed[i]);
    point(x[i], y[i]);



    x[i] = x[i] -speed[i]/ 2;

    if (x[i] &lt; 0) {
      x[i] = width;
    }
    i = i+5;
  }






  //if (mousePressed == true) {
  // Bullet = true;
  //}





  //  if (Bullet == true) {
  //    rect(mouseX -10 +bulletSpeed, mouseY, 40,10);
  //    fill(#03FC3E);


  //  }







}



class Bullet//bullet class
{
  float x;
  float y;
  float speed;
  Bullet(float tx, float ty)
  {
    x = tx;
    y = ty;
  }
  void display()
  {
     rect(x,y+20, 40,10);
     rect(x,y+80,40,10);
  fill(#03FC3E);
    //stroke(255);
    //point(x,y);
  }
  void move()
  {
    x+= 20;
  }
}

void removeToLimit(int maxLength)
{
  while(bullets.size() &gt; maxLength)
  {
    bullets.remove(0);
  }
}
void moveAll()
{
  for(Bullet temp : bullets)
  {
    temp.move();
  }
}
void displayAll()
{
  for(Bullet temp : bullets)
  {
    temp.display();
  }
}
void mousePressed()//add a new bullet if mouse is clicked
{
  Bullet temp = new Bullet(mouseX,mouseY);
  bullets.add(temp);
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Fullscreen sketch</title>
      <link>https://forum.processing.org/two/discussion/20411/fullscreen-sketch</link>
      <pubDate>Sun, 22 Jan 2017 14:19:20 +0000</pubDate>
      <dc:creator>schotsl</dc:creator>
      <guid isPermaLink="false">20411@/two/discussions</guid>
      <description><![CDATA[<p>Hello, I wrote this code in which you have a traingle that points towards you mouse around a circle, if you press your left mouse it will shoot a bullet and al of that works fine but when I replace</p>

<p><code>size(400, 400);</code></p>

<p>with</p>

<p><code>fullScreen();</code></p>

<p>the bullets disapear straight after I shoot them and I cant figure out why. Here's the whole code with 400 x 400 resolution</p>

<pre><code>float angle, rotation, speed, canShootCounter;

int basesize = 250;
int speedfactor = 1;
int version = 3;

int x, y, i;

Player player = new Player();

PVector location;
ArrayList&lt;Bullet&gt; bullets;
boolean canShoot = true;


void setup() {
  size(400, 400);
  bullets = new ArrayList&lt;Bullet&gt;();
  player = new Player();
}

void draw() {
  player.update();
  background(255);
  ellipseMode(CENTER);
  ellipse(width/2, height/2, basesize, basesize);

   x = (int)(width/2+165*cos(atan2( mouseY - height/2, mouseX - width/2)));
   y = (int)(height/2+165*sin(atan2( mouseY - height/2, mouseX - width/2)));

  pushMatrix();
  translate(x, y);
  rotate(atan2( mouseY - height/2, mouseX - width/2)-PI/2);
  triangle(-30, -10, 0, +50, +30, -10);
  popMatrix();

  for (i = bullets.size()-1; i &gt;= 0; i--) {
    Bullet bullet = bullets.get(i);
    bullet.update();
  }
}

class Player {
  Player() {
    location = new PVector(x, y);
  } 
  void update() {
    if (mousePressed == true) {
      if (canShoot == true) {
        bullets.add( new Bullet());
        canShoot = false;
        canShootCounter = 0;
      }
    }
    if (canShoot == false) {
      canShootCounter ++;
      if (canShootCounter == 10){
        canShoot = true;
      }
    }
  }
}

class Bullet {
  Bullet() {
    location= new PVector(x, y);
    rotation = atan2( mouseY - height/2, mouseX - width/2 );
    speed = 5;
  } 
  void update() {
    location.x = location.x + cos(rotation)*speed;
    location.y = location.y + sin(rotation)*speed;
    ellipse(location.x, location.y, 10, 10);

    if (location.x &gt; 0 &amp;&amp; location.x &lt; width &amp;&amp; location.y &gt; 0 &amp;&amp; location.x &lt; height) {
    }
    else {
      bullets.remove(i);
    }
  }
}
</code></pre>

<p>I hope someone can help. :)</p>
]]></description>
   </item>
   <item>
      <title>Pvector bullets</title>
      <link>https://forum.processing.org/two/discussion/19353/pvector-bullets</link>
      <pubDate>Mon, 28 Nov 2016 13:11:54 +0000</pubDate>
      <dc:creator>schotsl</dc:creator>
      <guid isPermaLink="false">19353@/two/discussions</guid>
      <description><![CDATA[<p>I got this little code working to shoot bullet from an entity which can move around a circle and shoot a bullet and then the bullet maintains its angle</p>

<p>I know how to use the basic Pvector but I can't figure out how to use them in this case, so I had to code it in such a way that  you can only shoot a new one once the previous one is of screen so I was hoping someone could show me how to use the Pvector in this case</p>

<pre><code>int basesize = 250;
int growth;
int direction = 0;
int speedfactor = 1;
int space;
int line1; 
int line2;
float bullet1;
int bulle1 = 220;
int open1;
int millis1;
int bullet;
float t = PI+PI/2;
float r = 165;

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

void keyPressed() {
  if (key == 'a' || key == 'A' || keyCode == DOWN) direction = -1;
  if (key == 'd' || key == 'D' || keyCode == UP) direction = 1;
  if (key == ' ') space = 1;
}

void keyReleased() {
  if (key == ' ') space = 0;
  if (key == 'a' || key == 'A' || keyCode == DOWN) direction = 0;
  if (key == 'd' || key == 'D' || keyCode == UP) direction = 0;
}

void draw() {

  background(255);

  stroke(0);
  fill(255);
  ellipseMode(CENTER);

  if (growth == 1) basesize += 2 * speedfactor;
  if (growth == 0) basesize -= 2 * speedfactor;

  if (basesize &gt; 275) growth = 0;
  if (basesize &lt; 225) growth = 1;

  ellipse(width/2, height/2, basesize, basesize);

  t += direction * speedfactor * 0.03;

  int x = (int)(width/2+r*cos(t));
  int y = (int)(height/2+r*sin(t));

  pushMatrix();
  translate(x, y);
  rotate(t-PI/2);
  stroke(0);
  fill(255);
  triangle(-30, -10, 0, +50, +30, -10);
  popMatrix();



  //bullet 1
  if (space == 1 &amp;&amp; open1 == 0) {
    bullet1 = t;
    millis1 = millis();
    open1 = 1;
    space = 0;
  }
  if (open1 == 1) {
    bulle1 += 20 * speedfactor;
    line(width/2+bulle1*cos(bullet1), height/2+bulle1*sin(bullet1), width/2+(bulle1+10)*cos(bullet1), height/2+(bulle1+10)*sin(bullet1));
  }
  if (width/2+(bulle1+10)*cos(bullet1) &lt; -20 || width/2+(bulle1+10)*cos(bullet1) &gt; width + 20 || height/2+(bulle1+10)*sin(bullet1) &lt; 20 || height/2+(bulle1+10)*sin(bullet1) &gt; height) {
    bulle1 = 220;
    millis1 = 0;
    bullet = 1;
    open1 = 0;
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Program draw in order</title>
      <link>https://forum.processing.org/two/discussion/18869/program-draw-in-order</link>
      <pubDate>Thu, 03 Nov 2016 19:27:55 +0000</pubDate>
      <dc:creator>schotsl</dc:creator>
      <guid isPermaLink="false">18869@/two/discussions</guid>
      <description><![CDATA[<p>So recently I made this little game</p>

<pre><code>int enemy1x = (int)random(25, 575);
float speedfactor = 1;
int bulletx = 100000;
int bullety = 500;
int bullet1x = 0;
int bullet1 = 0;
int random1 = 0;
int enemy1y = 0;
int posx = 300;
int posy = 500;
int health = 5;
int enemy1 = 1;
int timer1 = 0;
int timer2 = 0;
int timer3 = 0;
int w = 0;
int d = 0;
int a = 0;



void setup() {
  size(600, 600);
  strokeWeight(3);
}

void keyPressed() {
  if (key == 'w' || key == 'W') w = 1;
  if (key == 'd' || key == 'D') d = 1;
  if (key == 'a' || key == 'A') a = 1;
}

void keyReleased() {
  if (key == 'w' || key == 'W') w = 0;
  if (key == 'd' || key == 'D') d = 0;
  if (key == 'a' || key == 'A') a = 0;
} 

void draw() {

  background(255);

  if (w == 1 &amp;&amp; bullet1 == 0) {
    bullet1 = 1;
    bulletx = posx;
  }
  if (d == 1 &amp;&amp; posx &lt; 600) posx += (5*speedfactor); 
  if (a == 1 &amp;&amp; posx &gt; 0) posx -= (5*speedfactor);

  if (bullet1 == 1) {
    bullety -= (10*speedfactor);
    line(bulletx, bullety, bulletx, bullety +10);
    if (bullety &lt; 0) {
      bullet1 = 0;
      bullety = 500;
    }
  }

  textSize(16);
  fill(0, 102, 153, 51);
  text("health:", 10, 30); 
  text(health, 65, 30);

  textSize(16);
  fill(0, 102, 153, 51);
  text("speedfactor:", 10, 45); 
  text(speedfactor, 110, 45);


  if (enemy1 == 0) {
    random1 = (int)random(0, (1000/speedfactor)); 
    enemy1 = 1;
    timer1 = millis();
  }
  timer2 = millis();
  timer3 = timer2-timer1;
  if (timer3 &gt; random1) { 
    enemy1y += (2*speedfactor);
    if (enemy1y &gt; 500) {
      health -= 1;
      timer1 = 0;
      timer2 = 0;
      timer3 = 0;
      bullet1 = 0;
      enemy1y = -50;
      bullety = 500;
      enemy1x = (int)random(25, 575);
      if (speedfactor &gt; 1.5) speedfactor -= 0.5;
    }
    if ((bulletx &gt; (enemy1x - 25)) &amp;&amp; bulletx &lt; (enemy1x + 25) &amp;&amp; (bullety &lt; enemy1y)) {
      speedfactor += 0.1;
      enemy1y = -50;
      timer1 = 0;
      timer2 = 0;
      timer3 = 0;
      enemy1 = 0;
      bullety = 500;
      bullet1 = 0;
      enemy1x = (int)random(25, 575);
    } 
    rectMode(CENTER);
    fill(255);
    rect(enemy1x, enemy1y, 50, 50);
  }

  fill(255);
  triangle(posx-15, posy, posx+15, posy, posx, posy-60);
}
</code></pre>

<p>but when the game runes and you shoot a "bullet" it renders the bullet over the enemy block even though I coded it to draw the bullet first and then the enemy?</p>

<p>Also when the speedfactor reaches 5 everything starts to glitch out for instance there some code in there that says something like this when player within the boundaries move to the right or left and when it's almost out of the boundaries it can't move in that direction but when the speedfactor reaches 5 it just stops working?</p>

<p>I hope someone knows the answer :)</p>
]]></description>
   </item>
   <item>
      <title>Shoot bullet at angle</title>
      <link>https://forum.processing.org/two/discussion/19330/shoot-bullet-at-angle</link>
      <pubDate>Sun, 27 Nov 2016 13:57:40 +0000</pubDate>
      <dc:creator>schotsl</dc:creator>
      <guid isPermaLink="false">19330@/two/discussions</guid>
      <description><![CDATA[<p>Hello,</p>

<p>As the title says I want to shoot bullets from an object (the triangle) and I know how to shoot and accelerate the bullet straight forward by just increasing Y every time but now I wanted to make a game where you're supposed to shoot enemies that are approaching the center, but I was wondering how I could shoot the bullet in at the angle that the player is and when the bullet is shot</p>

<pre><code>int debug;
int basesize = 250;
int speedfactor = 1;
int direction = 0;
int growth;
int r = 200;
float t = PI+PI/2;

void setup() {
  frameRate(90);
  fullScreen();
  noStroke();
  smooth();
}

void draw() {
  background(255);

  stroke(0);
  fill(255);
  ellipseMode(CENTER);

  if (growth == 1) basesize += 2 * speedfactor;
  if (growth == 0) basesize -= 2 * speedfactor;

  if (basesize &gt; 275) growth = 0;
  if (basesize &lt; 225) growth = 1;

  ellipse(width/2, height/2, basesize, basesize);

  t += direction * speedfactor * 0.03;

  int x = (int)(width/2+r*cos(t));
  int y = (int)(height/2+r*sin(t));

  pushMatrix();
  translate(x, y);
  rotate(t-PI/2);
  stroke(0);
  fill(255);
  triangle(-20, -10, 0, +50, +20, -10);
  popMatrix();

  textAlign(LEFT);
  if (debug &gt; 1) debug = 0;
  if (debug == 1) {
    fill(0);
    textSize(12);
    text("health: " + "unknown", 10, 15); 
    text("speedfactor: " + speedfactor, 10, 30); 
    text("Framerate: " + round(frameRate), 10, 45);
    text("Framecount: " + frameCount, 10, 60);
    text("Runtime: " + millis()/1000 + " sec", 10, 75);
    text("Base size: " + basesize, 10, 90);
    text("Mouse cordinates:", 10, 115); 
    text("X:  ", 30, 130);
    text(mouseX, 45, 130);
    text("Y:  ", 30, 145);
    text(mouseY, 45, 145);
    text("Screensize: ", 10, 175);
    text("X:  ", 30, 190);
    text(width, 45, 190);
    text("Y:  ", 30, 205);
    text(height, 45, 205);
    text("Player cordinates:", 10, 235); 
    text("X:  ", 30, 250);
    text(x, 45, 250);
    text("Y:  ", 30, 265);
    text(y, 45, 265);
  }
}

void keyPressed() {
  if (key == 'c' || key == 'C' ) debug += 1;
  if (key == 'a' || key == 'A' || keyCode == DOWN) direction = -1;
  if (key == 'd' || key == 'D' || keyCode == UP) direction = 1;
}

void keyReleased() {
  if (key == 'a' || key == 'A' || keyCode == DOWN) direction = 0;
  if (key == 'd' || key == 'D' || keyCode == UP) direction = 0;
}
</code></pre>

<p>This is what I've got so far</p>

<p>I hope someone can help me</p>
]]></description>
   </item>
   <item>
      <title>How to limit number of bullets in shooting game?</title>
      <link>https://forum.processing.org/two/discussion/11750/how-to-limit-number-of-bullets-in-shooting-game</link>
      <pubDate>Sat, 18 Jul 2015 13:51:52 +0000</pubDate>
      <dc:creator>kkesih</dc:creator>
      <guid isPermaLink="false">11750@/two/discussions</guid>
      <description><![CDATA[<p>Hello,</p>

<p>I'm just learning processing and trying to create a very simple shooting game. I need to know how to fire bullets from the location of a mouse click to random positions, and limit the number of bullets to 10. What is the best way to approach this?</p>
]]></description>
   </item>
   <item>
      <title>Objects</title>
      <link>https://forum.processing.org/two/discussion/11735/objects</link>
      <pubDate>Fri, 17 Jul 2015 04:52:08 +0000</pubDate>
      <dc:creator>juanClot</dc:creator>
      <guid isPermaLink="false">11735@/two/discussions</guid>
      <description><![CDATA[<p>If i have an array of object from this</p>

<pre><code>class Shot {
  PVector loc;
  PVector vel;  
  int r,g,b; 
  Shot (PVector loc, PVector direction, int r, int g, int b) {
    this.loc=new PVector ();
    vel=new PVector();
    this.loc = loc.get();
    vel= direction.get();
    vel.normalize();
    vel.mult(9);
    this.r=r;
    this.g=g;
    this.b=b;
  }
  void displayShot() {    
    loc.add(vel);
    noStroke();
    fill(r,g,b);
    ellipse(loc.x, loc.y, 4, 4);    
  }  
}
</code></pre>

<p>and i want to shoot them by ckicking. How could i make it work without shooting too many of those(by to many i mean one each draw). because my game works perfect but it shoots a lot, its nice but THERE ARE TO MANY GOING OUT.</p>
]]></description>
   </item>
   <item>
      <title>Help with Collision detecting of 2 Arrays</title>
      <link>https://forum.processing.org/two/discussion/2435/help-with-collision-detecting-of-2-arrays</link>
      <pubDate>Tue, 14 Jan 2014 13:48:38 +0000</pubDate>
      <dc:creator>bonastella541</dc:creator>
      <guid isPermaLink="false">2435@/two/discussions</guid>
      <description><![CDATA[<p>I am trying to create a game within processing in which the player must shoot approaching zombies. I have created (with help from others) both classes for the bullets and the zombies but I am unable to figure out how to have the bullet affect the zombie. I'm looking for a way to maybe compare the x,y coordinates of each bullet with the x,y coordinates of each zombie (If that is the best way to approach this problem). I am a beginner with processing so please excuse any basic mistakes. Thanks.</p>

<p>class Bullet {
    float x, y;</p>

<pre><code>Bullet(float ax, float ay) {
  x=ax;
  y=ay;
}

void display() {
  image(img2,x,y,30,10);
}

void move() {
   x=x+10;
}

void  remove() {
  if(x&gt;displayWidth+200) {
    bullets.remove(0);
 }  


}

void run() {
  move();
  display(); 
  remove();  
}
</code></pre>

<p>}</p>

<p>class Zombie {
  float x;
  final float y, vx;
  final PImage img;</p>

<p>Zombie(float xx, float yy, float vv, PImage pic) {
    x = xx;
    y = yy;
    vx = vv;
    img = pic;
  }</p>

<p>boolean run() {
    display();
    return move();
  }</p>

<p>boolean move() {
    return (x += vx) &lt; -img.width-50;
  }</p>

<p>void display() {
    image(img, x, y, 150, 150);
  }</p>

<p>}</p>
]]></description>
   </item>
   </channel>
</rss>