如何在pygame中检测碰撞?

7 浏览
0 Comments

如何在pygame中检测碰撞?

我使用下面的类创建了一个子弹列表和一个精灵列表。如何检测子弹是否与精灵碰撞,并删除该精灵和子弹?

#定义精灵类

class 精灵:

def __init__(self,x,y, 名称):

self.x=x

self.y=y

self.图像 = pygame.image.load(名称)

self.矩形 = self.图像.get_rect()

def 渲染(self):

窗口.blit(self.图像, (self.x,self.y))

#定义子弹类以创建子弹

class 子弹:

def __init__(self,x,y):

self.x = x + 23

self.y = y

self.子弹 = pygame.image.load("user_bullet.BMP")

self.矩形 = self.子弹.get_rect()

def 渲染(self):

窗口.blit(self.子弹, (self.x, self.y))

0