kevin0304
kevin0304

Reputation: 19

Pygame velocity and collision trouble

i am currentely making my final project for my exam and i'm making a platform game. Actually i'm made the collisions and it works. But the actual problem is that if i jump on a platform and i keep my player static for 1 second the player disappears which is weird. Could you help me to solve this issue? I have a hint that this issue is related to the velocity (calles vitesse_x and vitesse_y).

import pygame
from pygame.locals import *
pygame.init()

fenetre = pygame.display.set_mode((1024,768))
pygame.display.set_caption("Portal Escape")

class Perso(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface((50,50))
        self.rect = self.image.get_rect(bottomleft=(0,740))
        self.image.fill((255, 0, 0))
        self.doit_monter = False
        self.vitesse_x = 0
        self.vitesse_y = 0

    def update(self):
        self.rect.x += self.vitesse_x
        self.rect.y += self.vitesse_y
        touche = pygame.key.get_pressed()
        if touche[pygame.K_RIGHT] and self.rect.x < 920:
            self.vitesse_x += 1
        if touche[pygame.K_LEFT] and self.rect.x > 0:
            self.vitesse_x -= 1
        collision = pygame.sprite.spritecollide(self, blocs, False)
        for bloc in collision:
            if self.vitesse_y >= 0:
                self.rect.bottom = bloc.rect.top
            elif self.vitesse_y < 0:
                self.rect.top = bloc.rect.bottom

perso = Perso()

class Bloc(pygame.sprite.Sprite): #pour creer un obstacle et éléments du jeu
    def __init__(self, x, y, w, h):
        super().__init__()
        self.image = pygame.Surface((w,h))
        self.image.fill((0, 255, 0))
        self.rect = self.image.get_rect(topleft=(x, y))

all_sprites = pygame.sprite.Group()
blocs = pygame.sprite.Group()

all_sprites.add(perso)

b1 = Bloc(200,500,250,50)
b2 = Bloc(500,600,200,50)
b3 = Bloc(0,740,1024,30)
blocs.add(b1,b2,b3)
all_sprites.add(b1,b2,b3)

new_y = 0

continuer = True
while continuer:
    pygame.time.Clock().tick(60)
    for event in pygame.event.get():  
        if event.type == QUIT:
            continuer = False
        if event.type == KEYDOWN:
            if event.key == pygame.K_SPACE:
                perso.doit_monter = True
                new_y = perso.rect.y - 100

        if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT and perso.vitesse_x < 0:
                    perso.vitesse_x = 0
                    perso.vitesse_y = 0
                if event.key == pygame.K_RIGHT and perso.vitesse_x > 0:
                    perso.vitesse_x = 0
                    perso.vitesse_y = 0
                if event.key == pygame.K_SPACE and perso.vitesse_y < 0:
                    perso.vitesse_y = 0

    if perso.doit_monter == True:
        if perso.rect.y > new_y and perso.rect.y > 0:
            perso.vitesse_y -= 5
        else:
            perso.doit_monter = False
    else:
        perso.vitesse_y += 1
    all_sprites.update()

    fenetre.fill((0,0,0))
    all_sprites.draw(fenetre)
    pygame.display.flip()

pygame.quit()

Upvotes: 1

Views: 158

Answers (1)

MegaIng
MegaIng

Reputation: 7886

Ok simple, problem, simple solution. You have to reset the y velocity if you are on the ground.

In Perso.update:

    for bloc in collision:
        if self.vitesse_y >= 0:
            self.vitesse_y = 0 # reset velocity, so it isn't increasing forever
            self.rect.bottom = bloc.rect.top
        elif self.vitesse_y < 0:
            self.rect.top = bloc.rect.bottom

If you don't do that, the velocity will increase, till it is high enough to jump throug the floor in one go without colliding with stopping box

Upvotes: 1

Related Questions