# How can I make my character jump (python)(complete beginner)

**URL:** <https://discourse.processing.org/t/how-can-i-make-my-character-jump-python-complete-beginner/37146>\
**Category:** Processing.py\
**Created:** [May 27, 2022, 5:10pm UTC](https://discourse.processing.org/t/how-can-i-make-my-character-jump-python-complete-beginner/37146 "2022-05-27T17:10:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bruhmoment14](https://avatars.discourse-cdn.com/v4/letter/b/9fc348/32.png) [@bruhmoment14](https://discourse.processing.org/u/bruhmoment14)\
**Post date:** [May 27, 2022, 5:10pm UTC](https://discourse.processing.org/t/how-can-i-make-my-character-jump-python-complete-beginner/37146/1 "2022-05-27T17:10:19Z")

</div>

When I move my character they have complete freedom. I want the game to control similarly to Mario, I don’t know how to make it so when you hit the up arrow key, the character moves up once instead of constantly moving up until the key is released.

```auto
import math
import random
global cWidth
global cHeight
cWidth = 900
cHeight = 700
gravity = 1
class player(object):
    def __init__ (self):
        self.x = 0
        self.y = 0
        self.dx = 0
        self.dy = 0
        self.up = 0
        self.down = 0
        self.left = 0
        self.right = 0
        self.speed = 7
        self.h = 20
        self.w = 20
    def show(self):
        fill(255)
        rect(self.x,self.y,self.w,self.h)
    def update(self):
        self.x = self.x + (self.right - self.left)*self.speed
        self.y += self.dy
        self.dy += gravity
        
        if not (self.x >= 0):
            self.x = 0
        if not (self.x <= (cWidth - self.w)):
            self.x = (cWidth - self.w)
        if not (self.y >= 0):
            self.y = 0
        if not (self.y <= (cHeight - 200)):
            self.y = (cHeight - 200)

            

show = "Welcome"

def setup():
    size(cWidth,cHeight)
    global p
    frameRate(60)
    p = player()
    
def draw():
    noStroke()
    background(0)
    if (show=='Welcome'):
        textSize(40)
  
        fill("#FF0852")
        # Sets the button at 50,250 -- width and height
        rect(50,250, 200, 50)
        fill(255)
        text('Start',65,290)
 
        fill("#FF0852")
        rect(50,400, 200, 50)
        fill(255)
        text('Quit',65,440)
    elif (show=='Start'):
        p.show()
        p.update()
        fill(155)
        rect(0,520,900,200)
        fill("#FF0852")
        rect(50,600, 200, 50)
        fill(255)
        text('Menu',65,640)
 
        

    
def mouseClicked():
    global show, shots #this in
    #Handles interaction of user with the various user interface buttons
    if (mouseX>50 and mouseX<50+200 and mouseY>250 and mouseY<250+50 and show=='Welcome'):
        show='Start'
    elif (mouseX>50 and mouseX<50+200 and mouseY>400 and mouseY<400+50 and show=='Welcome'):
        exit()
    elif (mouseX>50 and mouseX<50+200 and mouseY>600 and mouseY<600+50 and show=='Start'):
        show='Welcome'

s = second()

def keyPressed():
    if key == 'w':
        p.up =1
    if key == 's':
        p.down=1
    if key == 'a':
        p.left=1
    if key == 'd':
        p.right=1
    
        
def keyReleased():
    if key == 'w':
        p.up=0
    if key == 's':
        p.down=0
    if key == 'a':
        p.left=0
    if key == 'd':
        p.right=0

```

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [May 27, 2022, 10:45pm UTC](https://discourse.processing.org/t/how-can-i-make-my-character-jump-python-complete-beginner/37146/2 "2022-05-27T22:45:41Z")

</div>

Welcome @bruhmoment14

There are many ways to approach this. First, I’d say you need to add ‘gravity’ to your game – like a force that constantly pulls your character down; the floor and any jumping act against gravity. But this isn’t trivial stuff to program – and things will get far more complicated when you begin adding walls and platforms. I’d recommend that you at least work with [PVectors](https://py.processing.org/reference/PVector.html), or probably better, a [physics engine](https://gist.github.com/tabreturn/cce7083e944b95d3bd1ffd4f48700c8e).

---

<div class="post-metadata">

**Author:** ![bruhmoment14](https://avatars.discourse-cdn.com/v4/letter/b/9fc348/32.png) [@bruhmoment14](https://discourse.processing.org/u/bruhmoment14)\
**Post date:** [May 27, 2022, 11:50pm UTC](https://discourse.processing.org/t/how-can-i-make-my-character-jump-python-complete-beginner/37146/3 "2022-05-27T23:50:20Z")

</div>

I’ve already added gravity, it’s near the top. The problem is the player has far too much control and can move against the gravity. I’m finding it hard to make it so the up arrow key just makes the player move up once, instead of constantly moving up until the key is released. Thanks for the reply though

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [May 28, 2022, 12:08am UTC](https://discourse.processing.org/t/how-can-i-make-my-character-jump-python-complete-beginner/37146/4 "2022-05-28T00:08:56Z")

</div>

You’ll need to check that ‘Mario’ is grounded before he can jump – perhaps using some `grounded` variable. Then, maybe reduce the force of the `jump` acting against `gravity` with each frame –

```python
position, gravity = PVector(245, 50), PVector(0, 5)
jump = PVector(0, 0)
grounded = False

def setup(): size(500, 200)

def draw():
    global position, grounded
    background(0)
    if position.y < height-10:
        position += gravity 
        grounded = False
    else:
        grounded = True
    if jump.y < 0:
        jump.y += 1
    position += jump
    square(position.x, position.y, 10)

```

I’ve just used a mouse-click to invoke the jump –

```python
def mouseClicked():
    global jump
    if grounded:
        jump.y = -20

```

But as I warned earlier, it gets a lot more complicated from here on in (and a physics engine will make things a lot easier).

---

<div class="post-metadata">

**Author:** ![bruhmoment14](https://avatars.discourse-cdn.com/v4/letter/b/9fc348/32.png) [@bruhmoment14](https://discourse.processing.org/u/bruhmoment14)\
**Post date:** [May 28, 2022, 12:54am UTC](https://discourse.processing.org/t/how-can-i-make-my-character-jump-python-complete-beginner/37146/5 "2022-05-28T00:54:02Z")

</div>

tysm this helped a lot, I’ll look into the physics engine as well

---

<div class="post-metadata">

**Author:** ![bagginstyrone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bagginstyrone/32/12600_2.png) [@bagginstyrone](https://discourse.processing.org/u/bagginstyrone)\
**Post date:** [May 28, 2022, 10:32am UTC](https://discourse.processing.org/t/how-can-i-make-my-character-jump-python-complete-beginner/37146/6 "2022-05-28T10:32:15Z")

</div>

I have just started working with the PVectors. What do you guys suggest for me?

---

<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:** [May 28, 2022, 11:18am UTC](https://discourse.processing.org/t/how-can-i-make-my-character-jump-python-complete-beginner/37146/7 "2022-05-28T11:18:57Z")

</div>

Hello @bagginstyrone and @bagginstyrone,

Some PVector resources here:

- [https://processing.org/](https://processing.org/) \< Always a good starting point to search from!
- [https://py.processing.org/reference/](https://py.processing.org/reference/) \< There is a PVector in there.
- [https://processing.org/tutorials/pvector](https://processing.org/tutorials/pvector)
- [https://processing.org/reference/PVector.html](https://processing.org/reference/PVector.html)
- [1.2: PVector class - The Nature of Code](https://www.youtube.com/watch?v=7nTLzLf7jUg)
- [https://processing.github.io/processing-javadocs/core/processing/core/PVector.html](https://processing.github.io/processing-javadocs/core/processing/core/PVector.html)
- [The Nature of Code](https://natureofcode.com/book/chapter-1-vectors/) \< An excellent resource!  
Topics include:  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/0/5/05bcb90aac0768a1d3fecc9cbced17b06762892e.png)

And the Examples that come with the Processing PDE (IDE):

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/7/1775fc88ae0c97215aba5807f76cb5982c36fd2c.png)

You will have to adapt JAVA versions to Python.

`:)`

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [May 28, 2022, 11:26am UTC](https://discourse.processing.org/t/how-can-i-make-my-character-jump-python-complete-beginner/37146/8 "2022-05-28T11:26:39Z")

</div>

To add to @glv’s excellent list –

- a repository for a Python port of _The Nature of Code_ sketches: [https://github.com/nature-of-code/noc-examples-python](https://github.com/nature-of-code/noc-examples-python)
