簡體   English   中英

Pygame中的平台碰撞

[英]Platformer Collision in Pygame

我試圖在Pygame中編寫平台游戲,但是在過去的幾周里,我一直被困在英雄與平台之間的碰撞檢測上。 這是我的課程:

class Platform(object):
    def __init__(self, colour, rect):
        self.colour = colour
        self.rect = rect

    def draw(self, screen):
        pygame.draw.rect(screen, self.colour, self.rect)


class Character(Block):
    def __init__(self, colour, rect, speed):
        super().__init__(colour, rect)
        (self.dx, self.dy) = speed
        self.is_falling = True

    def update(self, platforms):
        self.is_falling = True
        for platform in platforms:
            if self.is_on(platform):
                self.rect.bottom = platform.rect.top
                self.dy = 0
                self.is_falling = False

        if self.is_falling:
            self.gravity()
        self.rect.x += self.dx
        self.rect.y += self.dy

    def is_on(self, platform):
        return (pygame.Rect(self.rect.x, self.rect.y + self.dy,
                            self.rect.width, self.rect.height)
                .colliderect(platform.rect) and self.dy > 0)

    def left(self):
        self.dx = -5

    def right(self):
        self.dx = 5

    def stop_x(self):
        self.dx = 0

    def jump(self):
        if self.dy == 0:
            self.dy = -25

    def gravity(self):
        self.dy += 5

這是我的功能:

def key_down(event, character):
    if event.key == pygame.K_LEFT:
        character.left()
    elif event.key == pygame.K_RIGHT:
        character.right()
    elif event.key == pygame.K_UP:
        character.jump()

def key_up(event, character):
    if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
        character.stop_x()

def main():
    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    pygame.display.set_caption("PyPlatformer")
    hero = Character((255, 255, 0), pygame.Rect(100, 0, 20, 20), (0, 0))
    platform1 = Block((0, 255, 255), pygame.Rect(100, 100, 100, 10))
    platform2 = Block((0, 255, 255), pygame.Rect(150, 150, 100, 10))
    platforms = (platform1, platform2)
    clock = pygame.time.Clock()
    while True:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                key_down(event, hero)
            elif event.type == pygame.KEYUP:
                key_up(event, hero)

        screen.fill((0, 0, 0))
        hero.update(platforms)
        [platform.draw(screen) for platform in platforms]
        hero.draw(screen)
        pygame.display.update()

但是,當Character坐在Block ,碰撞檢測失敗,並且角色掉落在方塊上。 但是,當它在塊中移動時,檢測成功,角色回到坐在塊上。 然后重復此循環,角色看起來像在塊上彈跳。

是什么導致此錯誤? 我如何解決它?

這個版本適合我:

def is_on(self, platform):
    return (pygame.Rect(self.rect.x, self.rect.y + self.dy+1, # +1
                        self.rect.width, self.rect.height)
            .colliderect(platform.rect)) 

我加+1並刪除dy > 0


順便說一句:我發現了其他問題。

如果平台離得太近(請參閱platform2 128

platform1 = Block((0, 255, 255), pygame.Rect(100, 100, 100, 10))
platform2 = Block((0, 255, 255), pygame.Rect(150, 128, 100, 10)) # 128

站在platform2上的玩家可以觸摸platform1並且他在platform1上移動

它可以使我真正感動:)

platforms = []
platforms.append(Block((0, 255, 255), pygame.Rect(100, 400, 100, 10)))
platforms.append(Block((0, 255, 255), pygame.Rect(150, 428, 100, 10)))
platforms.append(Block((0, 255, 255), pygame.Rect(250, 28, 10, 400)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM