# Coding a simple game jump

**URL:** <https://discourse.processing.org/t/coding-a-simple-game-jump/2181>\
**Category:** Beginners\
**Created:** [July 29, 2018, 5:10pm UTC](https://discourse.processing.org/t/coding-a-simple-game-jump/2181 "2018-07-29T17:10:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danny\_Posada](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@Danny\_Posada](https://discourse.processing.org/u/Danny_Posada)\
**Post date:** [July 29, 2018, 5:10pm UTC](https://discourse.processing.org/t/coding-a-simple-game-jump/2181/1 "2018-07-29T17:10:14Z")

</div>

Hello you all, I need your help. So, recently i’ve been trying to challenge myself by designing a simple game with a red ball that can jump up and down and avoid obstacles. I plan to add the obstacles later, I’m just trying to get the “physics” down first. The problem is though, whenever I press ‘w’ for the ball to jump it just soars into the sky until I stop pressing the key, where then it falls in a straight down. I’ve tried over and over again but still cannot figure out how to make it jump once, and then fall back down (take for example Mario). Here is my code. If anyone has any solutions or possible ways to figure this out I would be extremely thankful. Have a nice day.

Code:  
int X = 250;  
int Y =200;  
color up;  
color down;  
color left;  
color right;  
boolean w = false;  
boolean a = false;  
boolean d = false;  
void setup() {  
size(500,400);  
noStroke();  
}

void draw() {  
background(255);  
//Draws Plataform  
fill(50,255,20);  
rect(0,350,500,400);  
//Ball  
//Draws Ball  
fill(255,20,50);  
ellipse(X,Y,30,30);  
//Makes Ball Fall  
down = get(X,Y+15);  
if(down != color(50,255,20) ) {  
Y = Y+6;  
}  
//Creates border on World  
if(X \<= 15) {  
X+= 5;  
}  
if(X \>= 485) {  
X-= 5;  
}  
//Checks for Jump  
if(w && !a && !d) {  
Y-=15;  
}  
//Checks for Veritcal Jumping  
if(w && a && !d) {  
Y-=15;  
X-=5;  
}  
if(w && d && !a) {  
Y-=15;  
X+=5;  
}  
//Checks for Left  
if(a && !d && !w) {  
X-=5;  
}  
//Checks for Right  
if(d && !a && !w) {  
X+=5;  
}  
}  
//sees which keys are being pressed  
void keyPressed() {  
if(key == ‘w’) {  
w = true;  
}  
if(key == ‘a’) {  
a = true;  
}  
if(key == ‘d’) {  
d = true;  
}  
}  
//Sees which keys are being released  
void keyReleased() {  
if(key == ‘w’) {  
w = false;  
}  
if(key == ‘a’) {  
a = false;  
}  
if(key == ‘d’) {  
d = false;  
}  
}

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [July 29, 2018, 6:40pm UTC](https://discourse.processing.org/t/coding-a-simple-game-jump/2181/2 "2018-07-29T18:40:33Z")

</div>

You might try looking at this example of a jumping dinosaur I wrote a while back:

> [@Processing Dino Game](https://discourse.processing.org/t/processing-dino-game/114/2):
>
> I ran your code through the cleaner. You would do well to try to KISS when coding. KISS stands for Keep It Super Simple. Notice that the code now has more functions, but each one only does a very specific thing. Also notice that any “dino” variables are now inside the Dino class. class Dino { float baseY, px, py, vy, ay, w, h, speed; boolean canJump, isJumping; Dino(float lineY) { baseY = lineY; px = 100; py = baseY; vy = 0; ay = 0; h = 100; w = 50; speed =…

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 29, 2018, 6:45pm UTC](https://discourse.processing.org/t/coding-a-simple-game-jump/2181/3 "2018-07-29T18:45:37Z")

</div>

> [@TfGuy44](#):
>
> KISS stands for Keep It Super Simple.

> **[KISS principle](https://en.wikipedia.org/wiki/KISS_principle)**
>
> KISS, an acronym for "Keep it simple, stupid!", is a design principle first noted by the U.S. Navy in 1960. First seen partly in American English by at least 1938, the KISS principle states that most systems work best if they are kept simple rather than made complicated; therefore, simplicity should be a key goal in design, and unnecessary complexity should be avoided. The phrase has been associated with aircraft engineer Kelly Johnson. The term "KISS principle" was in popular use by 197 The acro...

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [July 29, 2018, 6:47pm UTC](https://discourse.processing.org/t/coding-a-simple-game-jump/2181/4 "2018-07-29T18:47:06Z")

</div>

My version is less mean.

---

<div class="post-metadata">

**Author:** ![erwrow](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@erwrow](https://discourse.processing.org/u/erwrow)\
**Post date:** [July 30, 2018, 11:57am UTC](https://discourse.processing.org/t/coding-a-simple-game-jump/2181/5 "2018-07-30T11:57:33Z")

</div>

if you know some physics, you’ll know that Y = Yo + Vo \* t + 1/2 \* A \* t\*\*2 (the final t is squeared) so, if you set your gravity aceleration and an initial speed (Vo) and an initial position (Yo) you’ll get the final position (Y) in the Y axis, t will be the time between the moment that you press the “w” and the current time (something like: (millis() - momentOfJump))

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [July 30, 2018, 8:17pm UTC](https://discourse.processing.org/t/coding-a-simple-game-jump/2181/6 "2018-07-30T20:17:41Z")

</div>

Shameless self-promotion: here is a guide on collision detection, including handling games where the player must collide with the ground:

> **[Processing Collision Detection](https://happycoding.io/tutorials/processing/collision-detection)**
>
> Learn how to trigger an action when objects overlap in Processing!

https://codepen.io/KevinWorkman/embed/preview/ybNPNb?height=300&slug-hash=ybNPNb&default-tabs=html,result&host=https://codepen.io
