簡體   English   中英

pygame Platformer中的碰撞檢測

[英]Collision detection in pygame platformer

現在,我正在開發一款平台游戲,並且在碰撞檢測方面遇到很多麻煩。 問題之一是,當角色在盒子上時,圖片不斷從跳躍變為站立。 另一個是當角色在盒子上並碰到另一個盒子時,它們會立即被運送到底部盒子一側。 如果您對此問題有任何了解,那就太好了。 這是我當前的碰撞檢測程序:

def update(self):

    global playerRL, jump, playerUD, counter, inMotion, onGround, onBox, jumpSize, fallSpeed, jumpCounter, allBoxes, boxes, direction, gravity, bottomTouching, boxTouching
    keys = pygame.key.get_pressed()
    previousX = self.rect.left
    previousY = self.rect.bottom
    inMotion = False
    bottomTouching = False

    if counter > 20:
        counter = 0

    def collisionDetection():
        global jump, playerRL, playerUD, counter, inMotion, onGround, onBox, jumpSize, fallSpeed, jumpCounter, allBoxes, boxes, direction, gravity, bottomTouching, boxTouching
        collided = False
        boxCounter = 0
        for plat in boxes:
            if self.rect.colliderect(plat.rect):
                if self.rect.left < plat.rect.right and self.rect.right > plat.rect.left and self.rect.bottom > plat.rect.top and self.rect.bottom < plat.rect.bottom:
                    self.rect.bottom = plat.rect.top
                    playerUD = 0
                    jumpCounter = 0
                    gravity = 0
                    onGround = True
                    collided = True
                    boxCounter += 1
                    print 'hi'
                if self.rect.right <= plat.rect.right and self.rect.left <= plat.rect.left and self.rect.bottom > plat.rect.top and self.rect.top < plat.rect.bottom:
                    self.rect.right = plat.rect.left
                    print 'right'
                if self.rect.left >= plat.rect.left and self.rect.right >= plat.rect.right and self.rect.bottom > plat.rect.top and self.rect.top < plat.rect.bottom:
                    self.rect.left = plat.rect.right
                if self.rect.left <= plat.rect.right and self.rect.right >= plat.rect.left and self.rect.top < plat.rect.bottom and self.rect.bottom > plat.rect.top:
                    self.rect.top = plat.rect.bottom
                    jumpCounter = 0
                    playerUD = 0


        if self.rect.left < 0:
            if playerRL < 0:
                self.rect.left = 0
        if self.rect.top < 0:
            if playerUD < 0:
                self.rect.top = 0
        if self.rect.right >= 960:
            if playerRL > 0:
                self.rect.right = 960
        if self.rect.bottom > 640:
            self.rect.bottom = 640
            playerUD = 0
        if self.rect.bottom == 640:
            onGround = True
        else:
            if collided == False:
                onGround = False

    keys = pygame.key.get_pressed()

    if keys[K_d] or keys[K_RIGHT]:
        direction = 'right'
        inMotion = True
        if rightTouch == False:
            counter += 1
            playerRL += 4

    if keys[K_a] or keys[K_LEFT]:
        direction = 'left'
        inMotion = True
        if leftTouch == False:
            counter += 1
            playerRL -= 4

    if keys[K_w] or keys[K_UP]:
        if onGround == True:
            bottomTouching = False
            onGround = False
            jumpSize = 8
            playerUD -= jumpSize
            jumpCounter = 11
            gravity = 1


    if onGround == True:
        if inMotion == True and counter > 10:
            if direction == 'left':
                self.image = backwardMovingPlayer
                self.mask = pygame.mask.from_surface(self.image)
                self.rect = self.image.get_rect()
                self.rect.bottom = previousY
                self.rect.left = previousX

            else:
                self.image = forwardMovingPlayer
                self.mask = pygame.mask.from_surface(self.image)
                self.rect = self.image.get_rect()
                self.rect.bottom = previousY
                self.rect.left = previousX

        else:
            if direction == 'left':
                self.image = backwardStationaryPlayer
                self.mask = pygame.mask.from_surface(self.image)
                self.rect = self.image.get_rect()
                self.rect.bottom = previousY
                self.rect.left = previousX

            else:
                self.image = forwardStationaryPlayer
                self.mask = pygame.mask.from_surface(self.image) #masks are used for cutouts
                self.rect = self.image.get_rect()
                self.rect.bottom = previousY
                self.rect.left = previousX  

    else:
        if direction == 'left':
            self.image = backwardJumpingPlayer
            self.mask = pygame.mask.from_surface(self.image) #masks are used for cutouts
            self.rect = self.image.get_rect()
            self.rect.bottom = previousY
            self.rect.left = previousX

        else:
            self.image = forwardJumpingPlayer
            self.mask = pygame.mask.from_surface(self.image) #masks are used for cutouts
            self.rect = self.image.get_rect()
            self.rect.bottom = previousY
            self.rect.left = previousX          


    if onGround == False:
        if jumpCounter > 0:
            jumpCounter -= 1
            playerUD -= jumpSize
        else:
            playerUD += gravity
            gravity += 1

    self.rect.left += playerRL
    self.rect.bottom += playerUD

    collisionDetection()

    playerUD = 0
    playerRL = 0

Pygame內置了碰撞檢測功能: firstrect.colliderect(secondrect)

它返回一個布爾值。

暫無
暫無

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

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