在 pygame 中添加对象碰撞

Adding object collision in pygame

我正在尝试在我的游戏中实现碰撞,但我现在这样做的方式是玩家卡在碰撞箱中,我不知道该怎么做。我目前试图碰撞的对象是 wall1。为了清楚起见,我只添加了墙对象和主要功能

                class wall(object):
                    def __init__(self, x, y, width, height):
                        self.x = x
                        self.y = y
                        self.width = width
                        self.height = height 
                        #self.rect = pygame.Rect(x, y, width, height)
                        global ColliderRect 
                        ColliderRect = pygame.Rect(x, y, width, height)
                    def playerCollide(self):
                        if (man.x + man.vel > self.x - 45 and man.y + man.vel > self.y -50 and man.y + man.vel < self.y+self.height and man.x + man.vel < self.x + self.width):
                            return True
                        else:
                            return False

                #mainloop
                collider1 = wall(500, 400, 200, 200)

                man = player(200, 410, 64,64)
                run = True
                while run:
                    clock.tick(60)
                    wall1 = collider1.playerCollide()

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

                    keys = pygame.key.get_pressed()

                    if keys[pygame.K_a] and man.x > man.vel and  wall1== False:
                        man.x -= man.vel
                        man.left = True
                        man.right = False
                        print(wall1)
                    elif keys[pygame.K_d] and man.x < scrWidth - man.width - man.vel and wall1 == False:
                        man.x += man.vel
                        man.right = True
                        man.left = False
                        print(wall1)
                    elif keys[pygame.K_w] and man.y > man.vel and wall1 == False:
                        man.y -= man.vel
                        man.left = False
                        man.right = False
                        print(wall1)
                    elif keys[pygame.K_s] and man.y < scrHeight - man.height - man.vel and wall1 == False:
                        man.y += man.vel
                        man.left = False
                        man.right = False
                        print(wall1)
                    else:
                        man.right = False
                        man.left = False
                        man.walkCount = 0
                        if (wall1 == True):
                            wall1 = False
                    man.hitbox(15, 0, 31, 17)
                    #man.drawhitbox()
                    redrawGameWindow()
                pygame.quit()

使用pygame.Rect objects and colliderect()进行碰撞测试。

将一个pygame.Rect对象传递给方法playerCollide并测试它是否与墙碰撞:

class wall(object):
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height 
        self.rect = pygame.Rect(x, y, width, height)
        self.rect = pygame.Rect(x, y, width, height)
    
    def playerCollide(self, test_rect):
        return self.rect.colliderect(test_rect)

计算玩家的新位置(xy)。在新位置创建一个 pygame.Rect 和玩家的大小 (player_rect)。改变玩家的实际位置,如果player_rect不撞墙:

collider1 = wall(500, 400, 200, 200)
man = player(200, 410, 64,64)
run = True
while run:
    clock.tick(60)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    x, y = man.x, man.y

    if keys[pygame.K_a] and man.x > man.vel:
        x -= man.vel
        man.left = True
        man.right = False
    elif keys[pygame.K_d] and man.x < scrWidth - man.width - man.vel:
        x += man.vel
        man.right = True
        man.left = False
    elif keys[pygame.K_w] and man.y > man.vel:
        y -= man.vel
        man.left = False
        man.right = False
    elif keys[pygame.K_s] and man.y < scrHeight - man.height - man.vel:
        y+= man.vel
        man.left = False
        man.right = False
    else:
        man.right = False
        man.left = False
        man.walkCount = 0
        
    player_rect = pygame.Rect(x, y, 64, 64)
    if not collider1.playerCollide(player_rect):
        man.x, man.y = x, y