I am working on a space ship Processing project where when the key is pressed, the bullet moves on the x axis and keeps going right from where it left off up until it reaches the width of the screen. Once it reaches the width of the screen the bullet goes back to its original position in front of the space ship again. In other words, gives a feel of those old arcade space shooting games.
How I tried to do this was with a while and if statement in KeyPressed loop. When the position = width the bullet is created again (back in front of ship); however the bullet never moves.
I am really new to processing so I don't think I understand how their while and if statements work.
//bullet
int bulletP1X = mouseX+248;
int bulletP1Y =mouseY+100;
int bulletP2X = mouseX+207;
int bulletP2Y = mouseY+100;
int bulletRextX = mouseX+215;
int bulletRextY = mouseY+95;
int bulletT1X = mouseX+240;
int bulletT1Y = mouseY+100;
int bulletT2X = mouseX+215;
int bulletT2Y = mouseY+100;
void setup(){
size(1300,810);
}
void draw(){
background(0,0,0);
fill(121,17,17);
strokeWeight(1);
stroke(0,0,0);
triangle(mouseX+20,mouseY+50,mouseX+20,mouseY+150,mouseX+200,mouseY+100);
fill(255,255,210);
triangle(mouseX+150,mouseY+87,mouseX+150,mouseY+113,mouseX+200,mouseY+100);
//Left wing of Space Ship
beginShape();
stroke(255,255,255);
fill(121,17,17);
vertex(mouseX+20,mouseY+50);
vertex(mouseX+40,mouseY+40);
vertex(mouseX+200,mouseY+95);
vertex(mouseX+200,mouseY+105);
endShape();
//Right wing of Space Ship
beginShape();
stroke(255,255,255);
fill(121,17,17);
vertex(mouseX+20,mouseY+150);
vertex(mouseX+40,mouseY+160);
vertex(mouseX+200,mouseY+105);
vertex(mouseX+200,mouseY+100);
endShape();
//Inside white banner of Space Ship (top)
beginShape();
stroke(255,255,255);
fill(255,255,255);
vertex(mouseX+20,mouseY+65);
vertex(mouseX+55,mouseY+85);
vertex(mouseX+150,mouseY+90);
vertex(mouseX+150,mouseY+100);
vertex(mouseX+20,mouseY+100);
endShape();
//Inside white banner of Space Ship (bottom)
beginShape();
vertex(mouseX+20,mouseY+135);
vertex(mouseX+55,mouseY+115);
vertex(mouseX+150,mouseY+112);
vertex(mouseX+150,mouseY+100);
vertex(mouseX+20,mouseY+100);
endShape();
//White back of ship behind the cockpit
beginShape();
fill(255,255,255);
vertex(mouseX+5,mouseY+80);
vertex(mouseX+20,mouseY+65);
vertex(mouseX+20,mouseY+135);
vertex(mouseX+5,mouseY+115);
endShape();
fill(0,0,0);
ellipse(mouseX+30,mouseY+100,30,20);
ellipse(mouseX+60,mouseY+70,10,10);
ellipse(mouseX+60,mouseY+130,10,10);
stroke(0,0,0);
//square shapes inside ship
fill(255,255,255);
stroke(0,0,0);
rect(mouseX+100,mouseY+95,50,10);
rect(mouseX+105,mouseY+98,40,4);
//line design inside white part of Space Ship
line(mouseX+20,mouseY+100,mouseX+60,mouseY+70);
line(mouseX+30,mouseY+110,mouseX+60,mouseY+130);
//bullet
bulletP1X = mouseX+248;
bulletP1Y =mouseY+100;
bulletP2X = mouseX+207;
bulletP2Y = mouseY+100;
bulletRextX = mouseX+215;
bulletRextY = mouseY+95;
bulletT1X = mouseX+240;
bulletT1Y = mouseY+100;
bulletT2X = mouseX+215;
bulletT2Y = mouseY+100;
fill(255,0,0);
strokeWeight(5);
stroke(255,0,0);
//side tips
point(bulletP1X,bulletP1Y);
point(bulletP2X,bulletP2Y);
//bullet body
strokeWeight(1);
fill(255,255,255);
rect(bulletRextX,bulletRextY,24,10);
//TWO_PI IS FULL CIRCLE
//PI + HALF_PI == 1.5 PI == -0.5 PI
//TAU + HALF_PI == TWO_PI + HALF_PI == 2.5 PI == 0.5 PI
//round ends of bullet
arc(bulletT1X,bulletT1Y,12,12,-HALF_PI, HALF_PI);
arc(bulletT2X,bulletT2Y,13,12,radians(90),radians(270));
}
This is the part of my coding that should move the bullet on the x axis but it doesn't.
void keyPressed(){
background(0,0,0);
fill(255,0,0);
while (bulletP1X != width || bulletP1X < width || bulletP2X != width || bulletP2X < width
|| bulletRextX != width || bulletRextX < width || bulletT1X != width || bulletT1X < width || bulletT2X != width || bulletT2X < width){
fill(255,255,255);
stroke(0,0,0);
bulletP1X +=25;
bulletP2X+=25;
bulletRextX +=25;
bulletT1X+=25;
bulletT2X+=25;
fill(255,0,0);
strokeWeight(5);
stroke(255,0,0);
//side tips
point(bulletP1X,bulletP1Y);
point(bulletP2X,bulletP2Y);
//bullet body
strokeWeight(1);
fill(255,255,255);
rect(bulletRextX,bulletRextY,24,10);
//round ends of bullet
arc(bulletT1X,bulletT1Y,12,12,-HALF_PI, HALF_PI);
arc(bulletT2X,bulletT2Y,13,12,radians(90),radians(270));
}
if (bulletP1X == width || bulletP1X > width || bulletP2X == width || bulletP2X > width
|| bulletRextX == width || bulletRextX > width || bulletT1X == width || bulletT1X > width || bulletT2X == width || bulletT2X > width){
fill(255,255,255);
stroke(0,0,0);
//bullet
bulletP1X = mouseX+248;
bulletP1Y =mouseY+100;
bulletP2X = mouseX+207;
bulletP2Y = mouseY+100;
bulletRextX = mouseX+215;
bulletRextY = mouseY+95;
bulletT1X = mouseX+240;
bulletT1Y = mouseY+100;
bulletT2X = mouseX+215;
bulletT2Y = mouseY+100;
fill(255,0,0);
strokeWeight(5);
stroke(255,0,0);
//side tips
point(bulletP1X,bulletP1Y);
point(bulletP2X,bulletP2Y);
//bullet body
strokeWeight(1);
fill(255,255,255);
rect(bulletRextX,bulletRextY,24,10);
//round ends of bullet
arc(bulletT1X,bulletT1Y,12,12,-HALF_PI, HALF_PI);
arc(bulletT2X,bulletT2Y,13,12,radians(90),radians(270));
}
}
I'd recommend you some readings:
http://forum.processing.org/two/discussion/8087/what-are-setup-and-draw
http://forum.processing.org/two/discussion/8082/from-several-variables-to-arrays
http://forum.processing.org/two/discussion/8081/from-several-arrays-to-classes
The function keyPressed()
as it's siblings mousePressed()
, mouseClicked()
, etc... Are not "loops" as you said, they are callBack functions, meaning they are called once every time the event happens, so when a key
is pressed, the code inside keyPressed()
is executed once.
draw()
is a infinite loop that tries to run itself 60 times each second. The commands inside it are executed in order (top -> bottom) and the result is finally rendered at the end of each cycle.
This is why, usually, is not good idea to draw inside callback functions. Use it to set states or flags to drive things in draw()
. Also a while loop will hold execution, preventing draw to reach the end of the cycle and render the drawings...
There is also a built in variable keyPressed
that you can use in draw()
to check if some key is being pressed.
Also whenever you got a lot of someVar1
, someVar2
... You should consider using an array, and if you got some arrays holding related stuff, like xPos[]
, yPos[]
, xSpeed[]
and so on classes becomes really handy. It seems to be difficult but they actually make things easier right after you learn them.
Also to keep things in a desired range (xPos) the modulo %
operator is very helpful.
Here a simple way of making "bullet" travels when any key is pressed, being reset after reaching the edge:
int initial_x = 50;
int x_pos = initial_x;
void setup() {
size(300, 300);
background(0);
}
void draw() {
background(0);
if(keyPressed){
x_pos+=3;
if(x_pos >= width){
x_pos = initial_x;
}
}
ellipse(x_pos, 100, 10, 10);
}
A side note, you might consider wrapping all code to draw the ship in a function (or even better a class) so you can keep your draw()
tide. Something like:
void spaceShip(int x, int y){
//code to draw ship at (x,y)
}
and in draw you would have
spaceShip(mouseX, mouseY);
:)