Galaga projectile motion

//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);

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

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

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

Hidden
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++) {
}
}
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);
}
}

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

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

Would you mind sharing the spaceship.png, enemy.png, rocket.png so I can â€śplaytestâ€ť it? It might solve a few of your problems.

the enemy image hasnâ€™t been used yet.

here is my code for shooting rockets:

Rockets
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);
}
}
rocketW = rocket.width;
rocketH = rocket.height;
}
void mousePressed() {
shootRocket();
}
void shootRocket() {
}
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

spaceship shooting 3 rockets

Code
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);
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() {
}
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
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);
rocketW = rocket.width;
rocketH = rocket.height;
for(int i = 0; i < 100; i++) {
}
}
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() {
}
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));
}
}

1 Like

Thank you for the help

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
//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);

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

1 Like