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

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.

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
1 Like

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, or probably better, a physics engine.

3 Likes

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

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 –

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 –

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).

1 Like

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

1 Like

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

Hello @bagginstyrone and @bagginstyrone,

Some PVector resources here:

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

You will have to adapt JAVA versions to Python.

:)

1 Like

To add to @glv’s excellent list –

2 Likes