In shooting game, How can I add gravity?

Hello, I’m korean student.
In shooting game, How Can I add gravity When falling start and when shape die and respawn, how does it restart gravity from 0 to end.
I’m not good at English and please when you answer my question, give me completed coding…

I’m sorry…

class Rect {
int posX, posY;
int RectSize = 10;
Rect() {
RectSize = 10;
initGame();
}
void ballHit(int x) {
boolean hit = false;
stroke(255, 0, 0);
fill(255, 0, 0);
if ( (x >= posX) && (x <= posX+RectSize) ) {
hit = true;
line(mouseX, mouseY, mouseX, posY);
ellipse(posX+5, posY+5, RectSize+25, RectSize+25);
initGame();
}
if (hit== false) {
line(mouseX, mouseY, mouseX, 0);
}
}
void rectDrop() {
stroke(255);
fill(255);
rect(posX, posY++, RectSize, RectSize);
}
void initGame() {
posX = int(random(width));
posY = 0;
}
void gameStop() {
if (posY == height-laserLimit) {
fill(255, 0, 0);
noLoop();
initGame();
}
}
}

and this will be not problem coding and but just perhaps…

PImage img;
int numBall = 3;
Rect rect = new Rect[numBall];
int triSize = 20;
boolean shoot=false;
int laserLimit = 80;

void setup() {
size(200,400);
img = loadImage(“backiee-90760.jpg”);
begin = millis();
for (int i=0; i<numBall; i++) {
rect[i] = new Rect();
}
}
void draw() {
image(img, 0, 0, 200, 400);
line(0, height-laserLimit, width, height-laserLimit);
if (time > 0){
time = duration - (millis() - begin)/1000;
text(time, 20, 20);
}
else{
textSize(30);
text(“GAME OVER”, 15, height/2);
noLoop();
}
spaceCraft();
for (int i=0; i<numBall; i++) {
rect[i].rectDrop();
rect[i].gameStop();
}
if (shoot == true) {
for (int i=0; i<numBall; i++) {
rect[i].ballHit(mouseX);
}
shoot = false;
}
}

int begin;
int duration = 20;
int time = 20;

void spaceCraft() {
if (mouseY >= height-laserLimit) {
fill(0, 255, 0);
stroke(0, 255, 0);
triangle(mouseX-triSize, mouseY, mouseX+triSize, mouseY, mouseX, mouseY-triSize);
}
}
void mousePressed() {
if (mouseY >= height-laserLimit)
shoot = true;
}
void keyPressed() {
loop();
if (time > 0) {
time = duration - (millis() - begin)/1000;
noLoop();
}
}

Hi @Pleasegod ,

First, please edit your post and wrap the code by selecting and pressing the </> button. This makes it easier for us to read and handle it.

As a starting point regarding your question you can have a look here: Gravity (example) from learning processing.

Cheers
—mnse

1 Like

Hello @Pleasegod,

Welcome to the forum.

This is an excellent resource to help with gravity:
https://natureofcode.com/book/

Please read our Processing Forum Guidelines:

Processing Forum Guidelines < Click here to open!

I am sharing these Processing forum guidelines as a courtesy to start you on your journey.

Please read:

Our homework policy:
FAQ - Processing Foundation

If this is a homework question please tag it as homework.

Please format and post code as per these instructions:
FAQ - Processing Foundation < It is Ctrl+T in Processing for Windows

Processing resources are here:
https://processing.org/

:)

1 Like