Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

python - Pygame - Collisions on the sides of a platform

I am currently trying to make my own platformer on pygame. I have coded the collisions for the top and bottom of the platform so that the player can only jump on top of the platform if they are falling onto it. However, if the player walks into the side of the platform, he snaps on top of it. How can I make it so the sides of a platform act as a border? This is my update section in the main game loop:

def update(self):
    self.all_sprites.update() 
    hits = p.sprite.spritecollide(self.player, self.platforms, False) 
    if self.player.vel.y > 0: 
        if hits: 
            self.player.rect.bottom = hits[0].rect.top + 1 
            self.player.pos = self.player.rect.midbottom 
            self.player.vel.y = 0 
    if self.player.vel.y < 0: 
        if hits: 
            self.player.rect.top = hits[0].rect.bottom 
            self.player.pos = self.player.rect.midbottom 
            self.player.vel.y = 0 

I have tried using this, but it didnt work:

if self.player.vel.x > 0:
    if hits:
        self.player.rect.right = hits[0].rect.left
        self.player.pos = self.player.rect.midleft
        self.player.vel.x = 0
if self.player.vel.x < 0:
    if hits:
        self.player.rect.left = hits[0].rect.right
        self.player.pos = self.player.rect.midright
        self.player.vel.x = 0

This is my player class:

vec = p.math.Vector2 

class Player(p.sprite.Sprite): 
    def __init__(self, game):
        p.sprite.Sprite.__init__(self) 
        self.game = game 
        self.image = p.Surface((p_width, p_height)) 
        self.image.fill(p_colour) 
        self.rect = self.image.get_rect() 
        self.rect.center = (0, height - 30) 
        self.pos = vec(0, height - 30) 
        self.vel = vec(0, 0) 
        self.acc = vec(0, 0)

    def jump(self): 
       
        self.rect.y += 1 
        hits = p.sprite.spritecollide(self, self.game.platforms, False) 
        self.rect.y -= 1 
        if hits: 
            self.vel.y = p_jump 
    def update(self): 
        self.acc = vec(0, p_grav) 

        keys = p.key.get_pressed() 

        if keys[p.K_LEFT]: 
            self.acc.x = -p_acc 
        if keys[p.K_RIGHT]: 
            self.acc.x = p_acc 

        self.acc.x += self.vel.x * p_friction 

        self.vel += self.acc 
        self.pos += self.vel + 0.5 * self.acc 

        if self.rect.right > width - 1: 
            self.rect.right = width - 1 
            self.pos = self.rect.midbottom 
        if self.rect.left < 1: 
            self.rect.left = 1 
            self.pos = self.rect.midbottom 
        self.rect.midbottom = self.pos 

Any help is appreciated. I tried keeping the code minimal


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...