与玩家接触的敌人的碰撞检测

时间:2015-12-29 11:32:22

标签: python pygame

我很困惑为什么我的玩家能够发现与敌人的碰撞,但不能反过来。敌人似乎正在触碰他,但是当我打印出碰撞时,它仍然让我虚伪。但是如果我在我的玩家类中编写我的碰撞语句,它类似于我的敌人类,它就可以了。敌人的碰撞声明在底部。

由于

def CollisionDetect(x1,y1,w1,h1,x2,y2,w2,h2):

    if x2+w2>=x1>=x2 and y2+h2>=y1>=y2:
        return True
    elif x2+w2>=x1+w1>=x2 and y2+h2>=y1+h1>=y2:
        return True
    elif x2+w2>=x1>=x2 and y2+h2>=y1>=y2:
        return True
    elif x2+w2>=x1+w1>=x2 and y2+h2>=y1+h1>=y2: 
        return True
    else:
        return False


class Enemy:
    def __init__(self,x,y):    
        self.x=x
        self.y=y
        self.width=55
        self.height = 51
        self.rect = pygame.Rect(self.x,self.y,self.width,self.height)
        self.speed=1
        self.s0 = pygame.image.load("s0.png")
        self.s1 = pygame.image.load("s1.png")
        self.s2 = pygame.image.load("s2.png")
        self.s3 = pygame.image.load("s3.png")
        self.attack = pygame.image.load("attack.png")
        self.rotateds0 = pygame.transform.flip(self.s0 ,True, False)
        self.rotateds1 = pygame.transform.flip(self.s1 ,True, False)
        self.rotateds2 = pygame.transform.flip(self.s2 ,True, False)
        self.rotateds3 = pygame.transform.flip(self.s3 ,True, False)
        self.rotate = False   
        collision2 = False
        self.TimeTarget=10
        self.TimeNum=0
        self.currentImage=0        

    def move(self,player):
        if player.x > 100:
            if self.x > player.x:
                self.x -= self.speed
                if self.currentImage > 3:
                    self.currentImage = 0                    
            elif self.x < player.x:
                self.x += self.speed
                if self.currentImage < 4:    
                    self.currentImage = 4

            if self.x < player.x:
                if self.x > player.x:
                    if self.currentImage > 3:
                        self.currentImage = 0                    


    def update(self,CollisionDetect,player):        
        self.TimeNum+=1
        if self.TimeNum == self.TimeTarget:                
            if self.currentImage ==0:
                self.currentImage=1   
            elif self.currentImage ==1:
                self.currentImage=2    
            elif self.currentImage == 2:
                self.currentImage=3    
            elif self.currentImage ==3:
                self.currentImage =0                
            elif self.currentImage ==4:
                self.currentImage=5    
            elif self.currentImage ==5:
                self.currentImage=6    
            elif self.currentImage == 6:
                self.currentImage=7    
            elif self.currentImage ==7:
                self.currentImage = 4    

            self.TimeNum=0

        if self.currentImage==0:    
            screen.blit(self.s0, (self.x,self.y))    
        elif self.currentImage==1:
            screen.blit(self.s1, (self.x,self.y))                
        elif self.currentImage==2:
            screen.blit(self.s2, (self.x,self.y))    
        elif self.currentImage ==3:
            screen.blit(self.s3, (self.x,self.y))            

        elif self.currentImage==4:
            screen.blit(self.rotateds0, (self.x,self.y))    
        elif self.currentImage==5:
            screen.blit(self.rotateds1, (self.x,self.y))                
        elif self.currentImage==6:
            screen.blit(self.rotateds2, (self.x,self.y))    
        elif self.currentImage ==7:
            screen.blit(self.rotateds3, (self.x,self.y))

        collision2 = CollisionDetect(self.x,self.y,self.width,self.height,player.x,player.y,player.width,player.height)

1 个答案:

答案 0 :(得分:0)

也许可以提供帮助:

def CollisionDetect(x1,y1,w1,h1,x2,y2,w2,h2):
     return abs(x1+w1/2-x2+w2/2)<((w1+w2)/2) and abs(y1+h1/2-y2+h2/2)<((h1+h2)/2)

这是一个更简单的版本,我会尝试更多地解释它:

使用1维可能更容易理解:

abs(x1 + w1 / 2-x2 + w2 / 2)表示我正在搜索两段中间的距离。

------- [A - B - C] --------- [d-E-F] -------

  • a:x1
  • b:x1 + w1 / 2
  • c:x1 + w1
  • d:x2
  • e:x2 + w2 / 2
  • f:x2 + w2

我想要的是距离两个中点的距离并不比每个点的中间到边界的宽度重要。

所以,abs(b - e)&lt; ([b-c] + [d-e])=&gt; abs(b-e)&lt; (w1 / 2 + w2 / 2)=&gt; abs((x1 + w1 / 2) - (x2 + w2 / 2))&lt; (W1 / 2 + W 2/2)