Python 敌方与玩家接触时的碰撞检测
我很困惑,为什么我的播放器可以检测到与敌人的碰撞,但它不能反过来。敌人似乎在碰他,但当我打印碰撞时,它仍然给我假消息。然而,如果我在我的player类中编写与我的敌人类类似的冲突语句,它就会工作。敌人的冲突声明在底部 谢谢Python 敌方与玩家接触时的碰撞检测,python,pygame,Python,Pygame,我很困惑,为什么我的播放器可以检测到与敌人的碰撞,但它不能反过来。敌人似乎在碰他,但当我打印碰撞时,它仍然给我假消息。然而,如果我在我的player类中编写与我的敌人类类似的冲突语句,它就会工作。敌人的冲突声明在底部 谢谢 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>=
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)
def碰撞检测(x1、y1、w1、h1、x2、y2、w2、h2):
如果x2+w2>=x1>=x2和y2+h2>=y1>=y2:
返回真值
如果x2+w2>=x1+w1>=x2和y2+h2>=y1+h1>=y2:
返回真值
如果x2+w2>=x1>=x2和y2+h2>=y1>=y2:
返回真值
如果x2+w2>=x1+w1>=x2和y2+h2>=y1+h1>=y2:
返回真值
其他:
返回错误
阶级敌人:
定义初始化(self,x,y):
self.x=x
self.y=y
自宽=55
自我高度=51
self.rect=pygame.rect(self.x、self.y、self.width、self.height)
自身速度=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,真,假)
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
冲突2=错误
self.TimeTarget=10
self.TimeNum=0
self.currentImage=0
def移动(自身,玩家):
如果player.x>100:
如果self.x>player.x:
self.x-=自速度
如果self.currentImage>3:
self.currentImage=0
elif self.xdef 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)
def碰撞检测(x1、y1、w1、h1、x2、y2、w2、h2):
返回abs(x1+w1/2-x2+w2/2)abs(b-e)<(w1/2+w2/2)=>abs((x1+w1/2)-(x2+w2/2))CollisionDetect不正确:语句1和3相等,语句2和语句4也相等。这不是核心问题,但可能是。如果你愿意,我可以为你重写。我很想看看你的版本!是的。。。我刚刚意识到。。我真的觉得很傻,没什么解释,希望能帮上忙