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

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:

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 xPos = (int)(random(-7, 7));
int yPos = (int)(random(-4, 4));

}
1 Like

Hello @Envyy2020,

Consider:

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.

:)

1 Like

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

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() {
}

2 Likes

here is a new version with removing old lasers

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() {
}

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

1 Like