0

I'm making a simple game where you can jump, and there's gravity.

Gravity is represented using the following function:

def updatecollision(self):
    global colision
    colision = self.hitbox.colliderect(suelo.rect)
if not collision:
    self.rect.y += 40

As you can see, whenever player collides with suelo.rect, he stops falling. The problem is that there is a point where self.rect.y applies a movement that passes through suelo.rect, without collision.

The hitbox rect actually is a rect that covers my PNG file, whereas Rect is the entire section of the png, including transparent pixels.

I thought about using regular rect to check when theres less than 20 pixels beetween the suelo.rect and hitbox.rect, so the change of the position modifier makes up and doesn't phase.

A bigger section of my code:

def controlarkeys(self):
    global jump
    global contadorSalto
    global saltoMax

    if jump:
            
        self.rect.y -= contadorSalto

        if contadorSalto > -saltoMax:
            contadorSalto -= 3
        else:
            jump = False
def updatecolisiones(self):
    global colision
    colision = self.hitbox.colliderect(suelo.rect)
def dibujar(self, pantalla):
    
    if not colision:
        self.rect.y *= 1.02
while True:

    for evento in pygame.event.get():
        if evento.type==pygame.QUIT:
            sys.exit()
        if evento.type == pygame.KEYDOWN:
                    if not jump and colision and evento.key == pygame.K_SPACE:
                        jump = True
                        contadorSalto = saltoMax
        print(evento)

    migue.updatecolisiones()

    migue.controlarkeys()

    pantalla.fill(color)

    suelo.updt(pantalla)

    migue.dibujar(pantalla)
The_spider
  • 1,202
  • 1
  • 8
  • 18
  • Where do you update `self.hitbox`? Is the problem just that the position of `self.hitbox` is never changed? – Rabbid76 Feb 20 '23 at 19:18

0 Answers0