Python 3.9.5 Pygame "AttributeError: 'pygame.Rect' object has no attribute 'pygame'"
Python 3.9.5 Pygame "AttributeError: 'pygame.Rect' object has no attribute 'pygame'"
我正在做一个python游戏,需要做一个游戏菜单。
当我 运行 我的代码出现此错误时:
AttributeError: 'pygame.Rect' object has no attribute 'pygame'
def main():
screen.fill(fill_color)
flag = True
clock = pygame.time.Clock()
menu = Menu(20, 70, 30, 'hello!', (255, 0, 0))
while flag:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
menu.drow()
if event == pygame.MOUSEBUTTONDOWN:
mouse_pos = event.pos
if menu.pos.pygame.Rect.collidepoint(mouse_pos):
这里是 menu.draw():
font = pygame.font.SysFont('fonts/crackman.ttf', self.fs)
text = font.render(self.text, False, self.color)
screen.blit(text, (round(self.pos_x), round(self.pos_y)))
collidepoint
is a method of a pygame.Rect
对象。您必须从文本中创建一个 pygame.Rect
对象:
self.rect = text.get_rect(topleft = (round(self.pos_x), round(self.pos_y)))
使用矩形进行碰撞检测:
if menu.rect.pygame.Rect.collidepoint(mouse_pos):
#
if menu.rect.collidepoint(mouse_pos):
我正在做一个python游戏,需要做一个游戏菜单。
当我 运行 我的代码出现此错误时:
AttributeError: 'pygame.Rect' object has no attribute 'pygame'
def main():
screen.fill(fill_color)
flag = True
clock = pygame.time.Clock()
menu = Menu(20, 70, 30, 'hello!', (255, 0, 0))
while flag:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
menu.drow()
if event == pygame.MOUSEBUTTONDOWN:
mouse_pos = event.pos
if menu.pos.pygame.Rect.collidepoint(mouse_pos):
这里是 menu.draw():
font = pygame.font.SysFont('fonts/crackman.ttf', self.fs)
text = font.render(self.text, False, self.color)
screen.blit(text, (round(self.pos_x), round(self.pos_y)))
collidepoint
is a method of a pygame.Rect
对象。您必须从文本中创建一个 pygame.Rect
对象:
self.rect = text.get_rect(topleft = (round(self.pos_x), round(self.pos_y)))
使用矩形进行碰撞检测:
#if menu.rect.pygame.Rect.collidepoint(mouse_pos):
if menu.rect.collidepoint(mouse_pos):