在pygame中将冲突检测添加到平台

时间:2013-09-23 19:06:27

标签: python pygame collision-detection

我正在制作一个小型的平台游戏,你可以在其中放置块来制作关卡,然后播放它。

我有重力,跳跃,左右移动..但我不确定如何让玩家在向左或向右移动时与墙壁发生碰撞。

我希望它的工作方式是这样的 -

if key[K_LEFT]:

if not block to the left:

move to the left

我将如何做到这一点(相对于此来源):

import pygame,random
from pygame.locals import *
import itertools
pygame.init()
screen=pygame.display.set_mode((640,480))
class Block(object):
    sprite = pygame.image.load("texture\\dirt.png").convert_alpha()
    def __init__(self, x, y):
        self.rect = self.sprite.get_rect(centery=y, centerx=x)

class Player(object):
    sprite = pygame.image.load("texture\\playr.png").convert()
    sprite.set_colorkey((0,255,0))
    def __init__(self, x, y):
        self.rect = self.sprite.get_rect(centery=y, centerx=x)

blocklist = []
player = []
colliding = False

while True:
    screen.fill((25,30,90))
    mse = pygame.mouse.get_pos()
    key=pygame.key.get_pressed()

    if key[K_LEFT]:
        p.rect.left-=1
    if key[K_RIGHT]:
        p.rect.left+=1
    if key[K_UP]:
        p.rect.top-=10

    for event in pygame.event.get():
        if event.type == QUIT: exit()

        if key[K_LSHIFT]:
            if event.type==MOUSEMOTION:
                if not any(block.rect.collidepoint(mse) for block in blocklist):
                    x=(int(mse[0]) / 32)*32
                    y=(int(mse[1]) / 32)*32
                    blocklist.append(Block(x+16,y+16))
        else:
            if event.type == pygame.MOUSEBUTTONUP:
                if event.button == 1:
                    to_remove = [b for b in blocklist if b.rect.collidepoint(mse)]
                    for b in to_remove:
                        blocklist.remove(b)

                    if not to_remove:
                        x=(int(mse[0]) / 32)*32
                        y=(int(mse[1]) / 32)*32
                        blocklist.append(Block(x+16,y+16))

                elif event.button == 3:
                    x=(int(mse[0]) / 32)*32
                    y=(int(mse[1]) / 32)*32
                    player=[]
                    player.append(Player(x+16,y+16))

    for b in blocklist:
        screen.blit(b.sprite, b.rect)
    for p in player:
        if any(p.rect.colliderect(block) for block in blocklist):
            #collide
            pass
        else:
            p.rect.top += 1
        screen.blit(p.sprite, p.rect)
    pygame.display.flip()

2 个答案:

答案 0 :(得分:7)

一个常见的方法是将水平和垂直碰撞处理分成两个单独的步骤。

如果你这样做并且还跟踪你的播放器的速度,很容易知道碰撞发生在哪一侧。

首先,让我们给玩家一些属性来跟踪他的速度:

class Player(object):
    ...
    def __init__(self, x, y):
        self.rect = self.sprite.get_rect(centery=y, centerx=x)
        # indicates that we are standing on the ground
        # and thus are "allowed" to jump
        self.on_ground = True 
        self.xvel = 0
        self.yvel = 0
        self.jump_speed = 10
        self.move_speed = 8

现在我们需要一种方法来实际检查碰撞。如上所述,为了简化操作,我们使用xvelyvel来了解我们是否与左侧或右侧相撞等。这会进入Player类:

def collide(self, xvel, yvel, blocks):
    # all blocks that we collide with
    for block in [blocks[i] for i in self.rect.collidelistall(blocks)]:

        # if xvel is > 0, we know our right side bumped 
        # into the left side of a block etc.
        if xvel > 0: self.rect.right = block.rect.left
        if xvel < 0: self.rect.left = block.rect.right

        # if yvel > 0, we are falling, so if a collision happpens 
        # we know we hit the ground (remember, we seperated checking for
        # horizontal and vertical collision, so if yvel != 0, xvel is 0)
        if yvel > 0:
            self.rect.bottom = block.rect.top
            self.on_ground = True
            self.yvel = 0
        # if yvel < 0 and a collision occurs, we bumped our head
        # on a block above us
        if yvel < 0: self.rect.top = block.rect.bottom

接下来,我们将移动处理移至Player类。所以让我们在跟踪输入的对象上创建。在这里,我使用namedtuple,因为为什么不呢。

from collections import namedtuple
...
max_gravity = 100
Move = namedtuple('Move', ['up', 'left', 'right'])
while True:
    screen.fill((25,30,90))
    mse = pygame.mouse.get_pos()
    key = pygame.key.get_pressed()

    for event in pygame.event.get():
       ...

    move = Move(key[K_UP], key[K_LEFT], key[K_RIGHT])
    for p in player:
        p.update(move, blocklist)
        screen.blit(p.sprite, p.rect)

我们将blocklist传递给update的{​​{1}}方法,以便我们检查是否存在冲突。使用Player对象,我们现在知道玩家应该移动的位置,所以让我们实现move

Player.update

唯一剩下的就是使用Clock来限制帧率以让游戏以恒定速度运行。而已。

这是完整的代码:

def update(self, move, blocks):

    # check if we can jump 
    if move.up and self.on_ground: 
        self.yvel -= self.jump_speed

    # simple left/right movement
    if move.left: self.xvel = -self.move_speed
    if move.right: self.xvel = self.move_speed

    # if in the air, fall down
    if not self.on_ground:
        self.yvel += 0.3
        # but not too fast
        if self.yvel > max_gravity: self.yvel = max_gravity

    # if no left/right movement, x speed is 0, of course
    if not (move.left or move.right):
        self.xvel = 0

    # move horizontal, and check for horizontal collisions
    self.rect.left += self.xvel
    self.collide(self.xvel, 0, blocks)

    # move vertically, and check for vertical collisions
    self.rect.top += self.yvel
    self.on_ground = False;
    self.collide(0, self.yvel, blocks)

请注意,我更改了图像名称,所以我只需要一个图像文件来测试它。另外,我不知道你为什么把玩家留在一个列表中,但这里有一个很好的动画片我们的游戏:

enter image description here

答案 1 :(得分:1)

由于你正在使用pygame,你可以使用pygame的rect.colliderect()来查看玩家的精灵是否与一个块发生碰撞。然后创建一个函数,返回相对于另一个矩形的某个矩形所在的边:

def rect_side(rect1, rect2): # Returns side of rect1 relative to rect2.
    if rect1.x > rect2.x:
        return "right"
    else:
        return "left"
    # If rect1.x == rect2.x the function will return "left".

如果rect_side()返回"right",则限制向左移动,反之亦然。

PS:如果您想要相同但包括垂直移动,则可以将rect1.yrect2.y进行比较,并处理输出"up""down"。您可以创建一个表示水平和垂直方向的元组。