I got collisions working but character falls through block after colliding with it

I am a beginner and making a platformer game. I took a while but I got jumping and collisions to work. Only problem now is when my character is on top of the block he falls through it to the ground. I have no clue how to fix this

position, gravity = PVector(245, 50), PVector(0, 5)
jump = PVector(0, 0)
grounded = False
import math
import random
global cWidth
global cHeight
cWidth = 900
cHeight = 700
gravity = 1
# Character Properties ------------
class player(object):
    def __init__(self):
        self.x = 0
        self.y = 520
        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
# Show Character -------------------
    def show(self):
        fill(255)
        rect(self.x,self.y,self.w,self.h)
# Movement -------------------------
    def update(self):
        self.x = self.x + (self.right - self.left)*self.speed
        self.y += self.dy
        self.dy += gravity
# Colisions with ends of screen ----
        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)
# Jumping --------------------------
        
    def jump(self):
        if self.y < 500 :
            grounded = False
        else:
            self.dy = -20
            grounded = True

# Objects --------------------------
    def colupdate1(self):
 
        if (self.y < 400 and self.x >= 680 and self.x <= 850 and self.y > 380):
            self.y = 400
        elif (self.y <= 380 and self.x >= 680 and self.x <= 850 and self.y >= 360): 
            self.y = 360
            grounded = True
    
        
show = "Welcome"

def setup():
    size(cWidth,cHeight)
    global p
    frameRate(60)
    p = player()
    
def draw():
    noStroke()
    background(0)
    f = loadFont("CenturyGothic-48.vlw")
    fb = loadFont("CenturyGothic-100.vlw")
# Screens ---------------------------
    if (show=='Welcome'):
        textSize(40)
        fill(255)
        textFont(fb)
        text('Game Name',140,140) 
        textSize(40)
        fill("#FF0852")
        rect(350,240, 200, 1)
        fill("#FF0852")
        rect(350,305, 200, 1)
        fill(255)
        textFont(f)
        text('Start',395,290) 
        
        fill("#FF0852")
        rect(350,370, 200, 1)
        fill("#FF0852")
        rect(350,430, 200, 1)
        fill(255)
        textFont(f)
        text('Options',360,415)
        
        fill("#FF0852")
        rect(350,495, 200, 1)
        fill("#FF0852")
        rect(350,555, 200, 1)
        fill(255)
        textFont(f)
        text('Quit',395,540)
    elif (show=='Options'):
        fill(155)
        textSize(40)
        fill("#FF0852")
        rect(350,195, 200, 1)
        fill("#FF0852")
        rect(350,255, 200, 1)
        fill(255)
        textFont(f)
        text('Controls',355,240)
        
        fill("#FF0852")
        rect(350,495, 200, 1)
        fill("#FF0852")
        rect(350,555, 200, 1)
        fill(255)
        textFont(f)
        text('Back',390,540)  
    elif (show=='OptControls'):
        fill(155)
        textSize(40)
        fill(255)
        textFont(f)
        text('A = Move Left',300,150)
        fill(255)
        textFont(f)
        text('D = Move Right',300,250)
        fill(255)
        textFont(f)
        text('S = Move Down',300,350)
        fill(255)
        textFont(f)
        text('W = Jump',300,450)
        
        fill("#FF0852")
        rect(350,495, 200, 1)
        fill("#FF0852")
        rect(350,555, 200, 1)
        fill(255)
        textFont(f)
        text('Back',390,540)  
    elif (show=='Start'):
        p.show()
        p.update()
        fill(200)
        rect(0,520,900,200)
        fill(0)
        rect(50,595, 270, 60)
        fill("#FF0852")
        rect(50,595, 270, 2)
        fill("#FF0852")
        rect(50,655, 270, 2)
        fill("#FF0852")
        textFont(f)
        text('Main Menu',55,640)
        # Blocks / Obstacles -------------
        p.colupdate1()
        fill(200)
        rect(700,380,150,20)

        

# Clicking Screen / Interaction ----------
def mouseClicked():
    global show, shots  
    # Handles interaction of user with the various user interface buttons
    if (mouseX>350 and mouseX<350+200 and mouseY>250 and mouseY<250+50 and show=='Welcome'):
        show='Start'
    elif (mouseX>350 and mouseX<350+200 and mouseY>500 and mouseY<500+50 and show=='Welcome'):
        exit()
    elif (mouseX>50 and mouseX<50+270 and mouseY>600 and mouseY<600+50 and show=='Start'):
        show='Welcome'
    elif (mouseX>350 and mouseX<350+200 and mouseY>375 and mouseY<375+50 and show=='Welcome'):
        show='Options'
    elif (mouseX>350 and mouseX<350+200 and mouseY>200 and mouseY<200+50 and show=='Options'):
        show='OptControls'
    elif (mouseX>350 and mouseX<350+200 and mouseY>500 and mouseY<500+50 and show=='OptControls'):
        show='Options'
    elif (mouseX>350 and mouseX<350+200 and mouseY>500 and mouseY<500+50 and show=='Options'):
        show='Welcome'


# Movement ------------------------------

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

Your grounded variable has no effect. You toggle it between True and False, but there’s no condition that utilizes its state. I assume you should use grounded to negate gravity’s effect on the player (when ‘Mario’ is standing on a platform or base of the level)?

To prove grounded is currently redundant, you can comment out every grounded = ... line in your code; everything runs the same.

1 Like

Yeah you’re right I’m hoping to stop gravity when on the platform but I didn’t realize grounded had no effect. I assume I have to use global on it?

edit: I used global on grounded above the def jump() and put the self.dy = grounded shown below. This fixed the problem of falling through the platform but I can’t jump while on it now.

   def colupdate1(self):
 
        if (self.y < 400 and self.x >= 680 and self.x <= 850 and self.y > 380):
            self.y = 400
        elif (self.y <= 380 and self.x >= 680 and self.x <= 850 and self.y >= 360): 
            self.y = 360
            self.dy = grounded

Hi @bruhmoment14,

your condition to jump is
def jump(self):
if self.y < 500 :
if you are standing on the block this is not given anymore …
you should consider to change youe logic from fixed numbers to anything more robust technics :slight_smile:

Cheers
— mnse

1 Like

thank you this is a big help