我很困惑,为什么我的球员可以发现与敌人的碰撞,但它不能反过来。敌人似乎在碰他,但当我打印碰撞时,它仍然给我假。但是,如果我在我的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>=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)
也许能帮上忙:
这是一个更简单的版本,我将尝试解释更多:
使用一维可能更容易理解:
abs(x1+w1/2-x2+w2/2)表示我在寻找两段中间的距离。你知道吗
-[a b c]-[d-e-f]-
我想要的是两个中点之间的距离并不比每个点从中间到边界的宽度更重要。你知道吗
所以,abs(b-e)<;([b-c]+[d-e])=>;abs(b-e)<;(w1/2+w2/2)=>;abs((x1+w1/2)-(x2+w2/2))<;(w1/2+w2/2)
相关问题 更多 >
编程相关推荐