在PyGame中添加对象碰撞

问题描述

我正在尝试在游戏中实现Collision,但是现在我的方式是,玩家被卡在了HitBox中,我不知道该怎么做。我当前尝试与之碰撞的对象是wall1。为了清楚起见,我只添加了Wall对象和main函数

                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,height)
                        global ColliderRect 
                        ColliderRect = pygame.Rect(x,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,31,17)
                    #man.drawhitBox()
                    redrawGameWindow()
                pygame.quit()

解决方法

使用pygame.Rect个对象和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,height)
        self.rect = pygame.Rect(x,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,64)
    if not collider1.playerCollide(player_rect):
        man.x,man.y = x,y