# Need help in creating lasers for this spaceship! (laser beams, laser rays)

**URL:** https://discourse.processing.org/t/need-help-in-creating-lasers-for-this-spaceship-laser-beams-laser-rays/40236
**Category:** Coding Questions
**Created:** [December 20, 2022, 9:18pm UTC](https://discourse.processing.org/t/need-help-in-creating-lasers-for-this-spaceship-laser-beams-laser-rays/40236 "2022-12-20T21:18:12Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Envyy2020](https://avatars.discourse-cdn.com/v4/letter/e/46a35a/32.png) [@Envyy2020](https://discourse.processing.org/u/Envyy2020)
#### Post date: [December 20, 2022, 9:18pm UTC](https://discourse.processing.org/t/need-help-in-creating-lasers-for-this-spaceship-laser-beams-laser-rays/40236/1 "2022-12-20T21:18:12Z")

</div>

I need to create a spaceship that shoots lasers from the tip of the spaceship and goes up when the mouse is clicked. The lasers just need to go vertically upwards, that’s it.

I HAVE SPENT HOURS trying to figure out how to make the lasers move up and how you can have multiple on the screen if you hit the mouse multiple times.

Everything is done expect for the lasers.

I was hoping someone can help me.

Here is the code:

```auto
int rSphere = 0;
int gSphere = 0;
int bSphere = 0;

int speed = 10;
  
void setup()
{
  size(640, 480);
  noCursor();
}

void draw()
{
  background(0);
  
  glowingSphere();
  
  fill(255);
  spaceship();
  
  thrust();
  
  stroke(255);
  
  int laserX = mouseX;
  int laserY = mouseY;

}

void glowingSphere()
{
  
  int rInc = (int)(random(0, 3));
  int gInc = (int)(random(0, 4));
  int bInc = (int)(random(0, 2));

  int glowingSphereX = mouseX;
  int glowingSphereY = mouseY;
  
  glowingSphereX = constrain(glowingSphereX, 0 + 17, width - 17);
  glowingSphereY = constrain(glowingSphereY, 0 + 20, height - 30);
  
  if (rSphere >= 230)
  {
    rSphere += -215;
  }
  
  if (gSphere >= 230)
  {
    gSphere += -215;
  }
  
  if (bSphere >= 230)
  {
    bSphere += -215;
  }
  
  fill(rSphere += rInc, gSphere += gInc, bSphere += bInc);
  noStroke();
  ellipse(glowingSphereX, glowingSphereY + 10, 27, 36);
}

void spaceship()
{
  int spaceshipX = mouseX;
  int spaceshipY = mouseY;
  
  spaceshipX = constrain(spaceshipX, 17, width - 17);
  spaceshipY = constrain(mouseY, 20, height - 30);
  
  triangle(spaceshipX, spaceshipY - 20, spaceshipX + 17, spaceshipY + 30, spaceshipX - 17, spaceshipY + 30);
  
  fill(0);
  triangle(spaceshipX, spaceshipY - 5, spaceshipX + 7, spaceshipY + 22, spaceshipX - 7, spaceshipY + 22);
}

void thrust()
{
  int thrustX = mouseX;
  int thrustY = mouseY;
  
  thrustX = constrain(thrustX, 0 + 17, width - 17);
  thrustY = constrain(thrustY, 0 + 23, height - 23);
  
  fill(200, 0, 0);
  int radius = (int)(random(5, 20));
  int xPos = (int)(random(-7, 7));
  int yPos = (int)(random(-4, 4));
  
  ellipse(thrustX + xPos, thrustY + 42 + yPos, radius, radius);
}

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [December 21, 2022, 1:13am UTC](https://discourse.processing.org/t/need-help-in-creating-lasers-for-this-spaceship-laser-beams-laser-rays/40236/2 "2022-12-21T01:13:29Z")

</div>

Hello @Envyy2020,

Consider:

> **[mousePressed() / Reference](https://processing.org/reference/mousePressed_.html)**
>
> The mousePressed() function is called once after every time a mouse button is pressed. The mouseButton variable (see the related reference entry) can be used to determine which button ha…

And draw a line from the current spaceship location (mouseX, mouseY) along the y-axis.

I tried it and it was a nice effect… you can then build on that.

Reference:  
[https://processing.org/reference/line\_.html](https://processing.org/reference/line_.html)

`:)`

---

<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: [December 21, 2022, 8:06am UTC](https://discourse.processing.org/t/need-help-in-creating-lasers-for-this-spaceship-laser-beams-laser-rays/40236/3 "2022-12-21T08:06:05Z")

</div>

> [@Envyy2020](#):
>
> how to make the lasers move up and how you can have multiple on the screen if you hit the mouse multiple times.

The idea here is that you have an ArrayList of lasers (of `PVector`).  
When you hit a mouse button you add a new laser to the ArrayList. At spaceship / mouse position.  
The laser beams are now independent of the spaceship/ mouse.

In `draw()` you display all lasers from the list (not using the position of spaceship or mouse since both might have moved!).

Also, in the ArrayList move each laser up to make them move / fly.

When a laser has collided or left the screen, remove it from the list.

See the reference for details

(This might LATER be easier when you have a class LaserBeam.)

* * *

**Quick example**

```auto
ArrayList<PVector> lasers = new ArrayList();

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

void draw() {
  background(0); 
  stroke(255, 0, 0); 

  for (PVector pv : lasers) {
    line(pv.x, pv.y, 
      pv.x, pv.y-6);
    pv.y-=2;
  }
}

void mousePressed() {
  lasers.add(new PVector(mouseX, mouseY));
}

```

---

<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: [December 23, 2022, 4:25pm UTC](https://discourse.processing.org/t/need-help-in-creating-lasers-for-this-spaceship-laser-beams-laser-rays/40236/4 "2022-12-23T16:25:29Z")

</div>

here is a new version with removing old lasers

```auto

ArrayList<PVector> lasers = new ArrayList();

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

void draw() {
  background(0); 
  stroke(255, 0, 0); 

  showAndMoveLasers();
  removeOldLasers();

  // debug 
  text(lasers.size(), 19, 19);
}

// ----------------------------------------------------------------------------

void mousePressed() {
  lasers.add(new PVector(mouseX, mouseY));
}

void showAndMoveLasers() {
  for (PVector pv : lasers) {
    line(pv.x, pv.y, 
      pv.x, pv.y-6); // show a line |
    pv.y-=2; // move
  }//for
}

void removeOldLasers() {
  for (int i=lasers.size()-1; i>=0; i--) {
    if (lasers.get(i).y<-10) {
      lasers.remove(i);
    }//if
  }//for
}
//

```
