# In shooting game, How can I add gravity?

**URL:** <https://discourse.processing.org/t/in-shooting-game-how-can-i-add-gravity/37552>\
**Category:** Processing\
**Tags:** homework\
**Created:** [June 15, 2022, 2:20am UTC](https://discourse.processing.org/t/in-shooting-game-how-can-i-add-gravity/37552 "2022-06-15T02:20:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pleasegod](https://avatars.discourse-cdn.com/v4/letter/p/4491bb/32.png) [@Pleasegod](https://discourse.processing.org/u/Pleasegod)\
**Post date:** [June 15, 2022, 2:20am UTC](https://discourse.processing.org/t/in-shooting-game-how-can-i-add-gravity/37552/1 "2022-06-15T02:20:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [June 15, 2022, 5:39am UTC](https://discourse.processing.org/t/in-shooting-game-how-can-i-add-gravity/37552/2 "2022-06-15T05:39:59Z")

</div>

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)](http://learningprocessing.com/examples/chp05/example-05-09-gravity) from learning processing.

Cheers  
—mnse

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 15, 2022, 11:08am UTC](https://discourse.processing.org/t/in-shooting-game-how-can-i-add-gravity/37552/3 "2022-06-15T11:08:23Z")

</div>

Hello @Pleasegod,

Welcome to the forum.

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

> [@Pleasegod](#):
>
> I’m not good at English and please when you answer my question, give me completed coding…

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:
> 
> - [Processing FAQ - Frequently asked questions](https://discourse.processing.org/faq)
> - [Guidelines - Asking Questions](https://discourse.processing.org/t/guidelines-asking-questions/2147/4)
> - [Guidelines - Answering Questions](https://discourse.processing.org/t/guidelines-answering-questions/2145/1)
> - [p5.js Community Statement](https://p5js.org/community/)
> 
> Our homework policy:  
> [FAQ - Processing Foundation](https://discourse.processing.org/faq#homework)
> 
> If this is a homework question please tag it as _homework_.
> 
> Please format and post code as per these instructions:  
> [FAQ - Processing Foundation](https://discourse.processing.org/faq#format-your-code) \< It is Ctrl+T in Processing for Windows
> 
> Processing resources are here:  
> [https://processing.org/](https://processing.org/)

`:)`
