PyGame:使用块进行碰撞检测

时间:2014-01-31 23:32:09

标签: python python-2.7 python-3.x pygame

在平台游戏中我正在制作我已经生成的地形但没有碰撞。这是我试图用来检查碰撞的代码:

def player_move(self):

    self.player.rect.x += self.player.velX
    self.check_collision(self.player, self.player.velX, 0)
    self.player.rect.y += self.player.velY
    self.check_collision(self.player, 0, self.player.velY)

def check_collision(self, sprite, x_vel, y_vel):
    # for every tile in Background.levelStructure, check for collision
    for block in self.moon.get_surrounding_blocks(sprite):
        if block is not None:
            if pygame.sprite.collide_rect(sprite, block):
                # we've collided! now we must move the collided sprite a step back
                if x_vel < 0:
                    sprite.rect.x = block.rect.x + block.rect.w

                if x_vel > 0:
                    sprite.rect.x = block.rect.x - sprite.rect.w

                if y_vel < 0:
                    sprite.rect.y = block.rect.y + block.rect.h

然后我还有级别生成的代码,如果这会有所帮助:

import pygame, random

# Level
class Block(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load('data/images/block.png')

        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def render(self, surface):
        surface.blit(self.image, self.rect)



class Background():

    def __init__(self):
        self.bg_image = pygame.image.load('data/images/background.jpg')


        # create a 2-dimensional array (etc. tile[x][y]) for holding the level structure
        self.levelStructure = [[None for i in range(50)] for i in range(50)]

        # generate level on initialization
        self.generateLevel()

    def generateLevel(self):
        # fill self.levelStructure with tiles
        for x in range(20):
            for y in range(9):
                if y < 11:
                    # make top space for the player to be
                    continue

                tempBlock = Block(x*40, y*40)

                # add block to both and self.levelStructure
                self.levelStructure[x][y] = tempBlock

        # generate some random terrain
        for x in range(25):
            # get a random height
            height = random.randint(2, 5)

            for y in range(height):
                y += 15
                tempBlock = Block(x*40, (y-height)*40)
                self.levelStructure[x][y-height] = tempBlock

    def get_surrounding_blocks(self, sprite):
        # calculate the grid position of the sprite
        sprite_x = sprite.rect.x // 40
        sprite_y = sprite.rect.y // 40

        # generate a list of surrounding sprites
        blocks = []
        for x in range(-2, 2+1):
            for y in range(-2, 2+1):
                blocks.append(self.levelStructure[sprite_x + x][sprite_y + y])

        return blocks

    def render(self, surface):
        surface.blit(self.bg_image, (0, 0))

        # also draw the blocks we created
        for x in range(22):
            for y in range(15):
                block = self.levelStructure[x][y]

                # check if there is a block, or if the grid is empty
                if block is not None:
                    block.render(surface)

非常感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

有几个问题。

首先,你永远不会告诉速度改变......你可能希望它停止,或当你碰到一个阻挡时反转。

其次,您将遍历所有块两次,一次用于检查X轴,一次用于Y.对于大型或复杂的地图,这将使其慢两倍。

我还建议不要使用collide_rect函数来测试此实例中的碰撞,除非您非常确定速度不能高于块(或精灵)的总大小。如果您移动71px一帧,但块大小为20px宽,并且您的播放器只有50px宽,您可以跳过一个块而不会碰到它!

要注意的一件事是,你正在移动物体,然后检查碰撞。这可能会导致问题,因为您必须将其移回原来的状态。

我建议将结构更改为:

def player_move(self):

    # create some local vars for ease of reading...
    x = self.player.rect.x
    x_next = x + self.player.velX

    y = self.player.rect.y
    y_next = y + self.player.vely

    w = self.player.rect.w
    h = self.player.rect.h

    # has a collision happened?
    hitX = False
    hitY = False

    for block in self.moon.get_surrounding_blocks(self.player):

        # empty blocks are 'None' in this map system...
        if not block:
            continue

        if self.player.velX > 0:  # moving right
            if x + w < block.x and x_next + w >= block.x:
                # collision!
                x_next =  block.x - w
                hitX = True
        elif self.player.velX < 0: # moving left
            if x > block.x + block.w and x_next > block.x + block.w:
                # collision!
                x_next = block.x + block.w
                hitX = True

         if self.player.velY...... # do the same for Y axis...

    # now update rect to have the new collision fixed positions:

    self.player.rect.x = x_next
    self.player.rect.y = y_next

    # and if there was a collision, fix the velocity.

    if hitX:
        self.velX = 0  # or 0 - velX to bounce instead of stop
    if hitY:
        self.velY = 0  # or 0 - velY to bounce instead of stop

希望这有助于将事情朝着正确的方向发展......

另外一个想法,当我几年前写一个平台引擎时,我记得我在一个阶段遇到了一些问题,玩家对象击中了块,停了下来,然后重新开始。最后,它在代码中的某处出现了一个错误。可能值得添加一堆调试打印(最好使用'日志'包...),然后大幅降低帧速率,这样你就可以看到到底发生了什么。

祝你好运!写游戏很有趣,但有时候也很令人沮丧。