# Galaga projectile motion

**URL:** https://discourse.processing.org/t/galaga-projectile-motion/26757
**Category:** Coding Questions
**Created:** [January 4, 2021, 1:19am UTC](https://discourse.processing.org/t/galaga-projectile-motion/26757 "2021-01-04T01:19:20Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![lionR](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@lionR](https://discourse.processing.org/u/lionR)
#### Post date: [January 4, 2021, 1:19am UTC](https://discourse.processing.org/t/galaga-projectile-motion/26757/1 "2021-01-04T01:19:20Z")

</div>

```auto
//Global Variables
PImage player, enemy, rocket;

int playerX;
int rocketX; 
int rocketY; 

//Following variables from funprogramming.org
float[] x = new float[250];// creates x values for 250 stars
float[] y = new float[250];// creates y values for 250 stars
float[] speed = new float[250];// creates speed values for 250 stars

void setup() {
  size(625, 1000);
  background(0);
  stroke(255);

  player = loadImage("spaceship.png");
  enemy = loadImage("enemy.png");
  rocket = loadImage("rocket.png");

  //from funprogramming.org
  int i = 0;
  while (i < 250) {  
    x[i] = random(0, width);//assigns a random value to all xs
    y[i] = random(0, height);//assigns a random value to all ys
    speed[i] = random(1, 7);//assigns a random value to all speeds
    i = i + 1;
  }
}//end setup

void draw() {
  background(0);
  
  imageMode(CENTER);
  image(player, playerX, 900); 

  playerX = mouseX;
  rocketX = int(mouseX);
  rocketY = 760;
  
  if (mouseButton == LEFT){
    rocket();
    rocketY += 4;
  }
  //from funprogramming
  int i = 0;
  while (i < 250) {//draws the stars in point function and adds motion 
    float co = map(speed[i], 1, 5, 100, 255);
    stroke(co);
    strokeWeight(speed[i]);
    point(x[i], y[i]); 
    y[i] = y[i] + speed[i] / 2;
    if (y[i] > 1000) {
      y[i] = 0;
    }
    i = i + 1;//adds 1 to i to stop more iterations of the star than needed
  }
}//end draw

void rocket(){
  image(rocket, rocketX, rocketY);
}

```

I have been able to create a character but when I try shooting the rocket the Y value won’t go up and when the mouse is moved the rocket disappears. How should I go about this problem?

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 4, 2021, 7:16am UTC](https://discourse.processing.org/t/galaga-projectile-motion/26757/2 "2021-01-04T07:16:20Z")

</div>

> [@lionR](#):
>
> ```auto
> rocketY = 760;
>   
> if (mouseButton == LEFT){
> rocket();
> rocketY += 4;
> }
> 
> ```

within this code you set rocketY to 760 and then changed it to 764 (+4). You effectively have to values for the rocketY; 760, when !mousePressed and 764 when it is

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 4, 2021, 7:30am UTC](https://discourse.processing.org/t/galaga-projectile-motion/26757/3 "2021-01-04T07:30:51Z")

</div>

Here is my star program! (stars moving in the background)

> **Hidden**
>
> ```auto
> ArrayList<PVector> stars = new ArrayList<PVector>();
> int starCount = 100, minSpeed = 10, maxSpeed = 20;
> void setup() {
> size(600,600);
> for(int i = 0; i < starCount; i++) {
> stars.add(new PVector(random(2*width),random(height),random(minSpeed,maxSpeed)));
> }
> }
> void draw() {
> background(0);
> stroke(255);
> strokeWeight(5);
> for(int i = 0; i < starCount; i++) {
> stars.set(i,new PVector(stars.get(i).x - stars.get(i).z, stars.get(i).y, stars.get(i).z));
> if(stars.get(i).x < 0) {
> stars.set(i, new PVector( random(width,2*width), random(height), random(minSpeed,maxSpeed)));
> }
> point(stars.get(i).x, stars.get(i).y);
> }
> }
> 
> ```

---

<div class="post-metadata">

### Author: ![lionR](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@lionR](https://discourse.processing.org/u/lionR)
#### Post date: [January 5, 2021, 2:10am UTC](https://discourse.processing.org/t/galaga-projectile-motion/26757/4 "2021-01-05T02:10:06Z")

</div>

How would I try to solve the problem I tried a couple of ways but they don’t seem to work.

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 5, 2021, 7:56am UTC](https://discourse.processing.org/t/galaga-projectile-motion/26757/5 "2021-01-05T07:56:55Z")

</div>

you should add a change Y varriable. Add +4 to it every frame the mouse is pressed and change Y by it. Or you can get rid of ` y = 760` and add a ySpeed variable to make the movement more natural

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 5, 2021, 7:58am UTC](https://discourse.processing.org/t/galaga-projectile-motion/26757/6 "2021-01-05T07:58:41Z")

</div>

Would you mind sharing the `spaceship.png, enemy.png, rocket.png` so I can “playtest” it? It might solve a few of your problems.

---

<div class="post-metadata">

### Author: ![lionR](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@lionR](https://discourse.processing.org/u/lionR)
#### Post date: [January 5, 2021, 7:12pm UTC](https://discourse.processing.org/t/galaga-projectile-motion/26757/7 "2021-01-05T19:12:25Z")

</div>

the enemy image hasn’t been used yet.

---

<div class="post-metadata">

### Author: ![lionR](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@lionR](https://discourse.processing.org/u/lionR)
#### Post date: [January 5, 2021, 7:12pm UTC](https://discourse.processing.org/t/galaga-projectile-motion/26757/8 "2021-01-05T19:12:56Z")

</div>

![rocket](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/02465d461626e8b6042eae18d7fa8905a5edc365.png)

---

<div class="post-metadata">

### Author: ![lionR](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@lionR](https://discourse.processing.org/u/lionR)
#### Post date: [January 5, 2021, 7:13pm UTC](https://discourse.processing.org/t/galaga-projectile-motion/26757/9 "2021-01-05T19:13:48Z")

</div>

![spaceship](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5d1a456c0418e36f361898135749d9f965540785.png)

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 5, 2021, 9:17pm UTC](https://discourse.processing.org/t/galaga-projectile-motion/26757/10 "2021-01-05T21:17:22Z")

</div>

here is my code for shooting rockets:

> **Rockets**
>
> ```auto
> ArrayList<PVector> rockets = new ArrayList<PVector>(); //.x = x, .y = y, .z = ySpeed
> float rocketW = 40, rocketH = 100, rocketSpeed = 10;
> PImage rocket;
> void setup() {
> size(600, 600);
> }
> void draw() {
> background(0);
> moveRockets();
> for (int i = 0; i < rockets.size(); i++) {
> PVector cRocket = rockets.get(i);
> image(rocket, cRocket.x,cRocket.y); //rect(cRocket.x, cRocket.y, rocketW, rocketH);
> if(cRocket.x > width || cRocket.x < 0 - rocketW || cRocket.y > height - rocketH) {
> rockets.remove(i);
> }
> }
> rocket = loadImage("data/rocket.png");
> rocketW = rocket.width;
> rocketH = rocket.height;
> }
> void mousePressed() {
> shootRocket();
> }
> void shootRocket() {
> rockets.add(new PVector(mouseX, mouseY, rocketSpeed));
> }
> void moveRockets() {
> for (int i = 0; i < rockets.size(); i++) {
> rockets.set(i, new PVector(rockets.get(i).x, rockets.get(i).y-rockets.get(i).z, rockets.get(i).z));
> }
> }
> 
> ```

I am still troubleshooting the rest

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 5, 2021, 9:26pm UTC](https://discourse.processing.org/t/galaga-projectile-motion/26757/11 "2021-01-05T21:26:49Z")

</div>

spaceship shooting 3 rockets

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b5b3a4880500d2e0ff8a5cd746e398c5da6ecfe5.png)

> **Code**
>
> ```auto
> ArrayList<PVector> rockets = new ArrayList<PVector>(); //.x = x, .y = y, .z = ySpeed
> float rocketW = 40, rocketH = 100, rocketSpeed = 10, spaceShipY = 400;
> PImage rocket, spaceship;
> void setup() {
> size(600, 600);
> rocket = loadImage("data/rocket.png");
> spaceship = loadImage("data/spaceship.png");
> rocketW = rocket.width;
> rocketH = rocket.height;
> }
> void draw() {
> background(0);
> moveRockets();
> for (int i = 0; i < rockets.size(); i++) {
> PVector cRocket = rockets.get(i);
> image(rocket, cRocket.x,cRocket.y); //rect(cRocket.x, cRocket.y, rocketW, rocketH);
> if(cRocket.x > width || cRocket.x < 0 - rocketW || cRocket.y > height - rocketH) {
> rockets.remove(i);
> }
> }
> image(spaceship,mouseX-spaceship.width/2,spaceShipY+spaceship.height/2); //adding the width/2 and height/2 of the image to make it centered
> }
> void mousePressed() {
> shootRocket();
> }
> void shootRocket() {
> rockets.add(new PVector(mouseX,spaceShipY, rocketSpeed));
> rockets.add(new PVector(mouseX-spaceship.width/3,spaceShipY+30, rocketSpeed));
> rockets.add(new PVector(mouseX+spaceship.width/3,spaceShipY+30, rocketSpeed));
> }
> void moveRockets() {
> for (int i = 0; i < rockets.size(); i++) {
> rockets.set(i, new PVector(rockets.get(i).x, rockets.get(i).y-rockets.get(i).z, rockets.get(i).z));
> }
> }
> 
> ```

I am posting this code for reference. Feel free to change it however you like. I am just showing you how I would solve the problem.

Here is a program with added snowflakes (if you want to see ofc)

> **Code SPOILER**
>
> ```auto
> ArrayList<PVector> rockets = new ArrayList<PVector>(), snowFlakes = new ArrayList<PVector>(); //.x = x, .y = y, .z = ySpeed
> float rocketW = 40, rocketH = 100, rocketSpeed = 10, spaceShipY = 400;
> PImage rocket, spaceship;
> void setup() {
> size(600, 600);
> rocket = loadImage("data/rocket.png");
> spaceship = loadImage("data/spaceship.png");
> rocketW = rocket.width;
> rocketH = rocket.height;
> for(int i = 0; i < 100; i++) {
> snowFlakes.add(new PVector(random(width),random(height),random(1,5)));
> }
> }
> void draw() {
> background(0);
>   
> for(int i = 0; i < snowFlakes.size(); i++) {
> PVector cSnow = new PVector( snowFlakes.get(i).x ,snowFlakes.get(i).y + snowFlakes.get(i).z,snowFlakes.get(i).z );    
> ellipse(cSnow.x,cSnow.y,5,5);
> if(cSnow.y > height) {
> cSnow = new PVector(random(width),-5,random(1,5));
> }
> snowFlakes.set(i,cSnow);
> }
>   
> moveRockets();
> for (int i = 0; i < rockets.size(); i++) {
> PVector cRocket = rockets.get(i);
> image(rocket, cRocket.x,cRocket.y); //rect(cRocket.x, cRocket.y, rocketW, rocketH);
> if(cRocket.x > width || cRocket.x < 0 - rocketW || cRocket.y > height - rocketH) {
> rockets.remove(i);
> }
> }
> image(spaceship,mouseX-spaceship.width/2,spaceShipY+spaceship.height/2); //adding the width/2 and height/2 of the image to make it centered
> }
> void mousePressed() {
> shootRocket();
> }
> void shootRocket() {
> rockets.add(new PVector(mouseX,spaceShipY, rocketSpeed));
> rockets.add(new PVector(mouseX-spaceship.width/3,spaceShipY+30, rocketSpeed));
> rockets.add(new PVector(mouseX+spaceship.width/3,spaceShipY+30, rocketSpeed));
> }
> void moveRockets() {
> for (int i = 0; i < rockets.size(); i++) {
> rockets.set(i, new PVector(rockets.get(i).x, rockets.get(i).y-rockets.get(i).z, rockets.get(i).z));
> }
> }
> 
> ```

---

<div class="post-metadata">

### Author: ![lionR](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@lionR](https://discourse.processing.org/u/lionR)
#### Post date: [January 5, 2021, 9:36pm UTC](https://discourse.processing.org/t/galaga-projectile-motion/26757/12 "2021-01-05T21:36:15Z")

</div>

Thank you for the help

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 5, 2021, 9:40pm UTC](https://discourse.processing.org/t/galaga-projectile-motion/26757/13 "2021-01-05T21:40:27Z")

</div>

I adapted your code a bit. It now works and shoots a single rocket. Whenever the mouse is pressed it is launced, if it is already launced, it moves back to the spaceship and is shot again.

> **Code**
>
> ```auto
> //Global Variables
> PImage player, enemy, rocket;
> 
> int playerX=300, playerY = 700;
> int rocketX; 
> int rocketY;
> int rocketSpeed = 5;
> 
> //Following variables from funprogramming.org
> float[] x = new float[250];// creates x values for 250 stars
> float[] y = new float[250];// creates y values for 250 stars
> float[] speed = new float[250];// creates speed values for 250 stars
> 
> void setup() {
> size(625, 800);
> background(0);
> stroke(255);
> 
> player = loadImage("spaceship.png");
> enemy = loadImage("enemy.png");
> rocket = loadImage("rocket.png");
> 
> //from funprogramming.org
> int i = 0;
> while (i < 250) {  
> x[i] = random(0, width);//assigns a random value to all xs
> y[i] = random(0, height);//assigns a random value to all ys
> speed[i] = random(1, 7);//assigns a random value to all speeds
> i = i + 1;
> }
> }//end setup
> 
> void draw() {
> background(0);
> 
> //from funprogramming
> int i = 0;
> while (i < 250) {//draws the stars in point function and adds motion 
> float co = map(speed[i], 1, 5, 100, 255);
> stroke(co);
> strokeWeight(speed[i]);
> point(x[i], y[i]); 
> y[i] = y[i] + speed[i] / 2;
> if (y[i] > 1000) {
> y[i] = 0;
> }
> i = i + 1;//adds 1 to i to stop more iterations of the star than needed
> }
>   
> rocketY -= rocketSpeed;
> rocket();
>   
> imageMode(CENTER);
> fill(255);
> playerX = mouseX;
> //rect(playerX,900,50,50);
> image(player, playerX, playerY); 
> 
> }//end draw
> 
> void rocket() {
> image(rocket, rocketX, rocketY);
> }
> void mousePressed() {
> rocketY = playerY;
> rocketX = playerX;
> }
> 
> ```

I edited the previous post that got the [solution] tag to add snowflakes (look under \>Code)
