AttributeError: 'pygame.mask.Mask' object has no attribute 'rect'
AttributeError: 'pygame.mask.Mask' object has no attribute 'rect'
我正在尝试获得像素完美碰撞,但我一直 运行 进入此错误:
Traceback (most recent call last):
File "D:\Projects\games\venv\lib\site-packages\pygame\sprite.py", line 1528, in collide_circle
xdistance = left.rect.centerx - right.rect.centerx
AttributeError: 'pygame.mask.Mask' object has no attribute 'rect'
Process finished with exit code 1
我不确定出了什么问题。是我的实现或逻辑还是两者兼而有之?
我的代码:
class AlienShooting(pygame.sprite.Sprite):
def __init__(self, w=640, h=640):
# init display
# init game state
self.spaceship_main = pygame.image.load('spacecraft_1.png').convert_alpha()
self.alien_spacecraft1 = pygame.image.load('spacecraft_alien.png').convert_alpha()
self.alien_spacecraft_rec1 = self.alien_spacecraft1.get_rect()
self.mask1 = pygame.mask.from_surface(self.alien_spacecraft1)
self.main_spaceship_mask = pygame.mask.from_surface(self.spaceship_main)
def play_step(self):
# user input
# Move the spaceship
# check if game over
game_over = False
if self._is_collision():
game_over = True
# Add new alien spaceships
# Randomly move alien spaceships
# update ui and clock
# return game over and score
return False
def _is_collision(self):
if pygame.sprite.collide_circle(self.mask1, self.main_spaceship_mask):
return True
return False
可能是因为我的主space飞船和外星人space飞船在同一个class,但我不确定。
查看 How do I detect collision in pygame? and . If you want to use pygame.sprite.collide_circle()
or pygame.sprite.collide_mask
you have to create pygame.sprite.Sprite
个对象。
spaceship_main
和 alien_spacecraft1
必须是 Sprite 对象。 Sprite 对象必须具有 rect
和 image
属性,并且可以选择具有 mask
属性。
例如:
class AlienShooting():
def __init__(self, w=640, h=640):
self.spaceship_main = pygame.sprite.Sprite()
self.spaceship_main.image = pygame.image.load('spacecraft_1.png').convert_alpha()
self.spaceship_main.rect = self.spaceship_main.image.get_rect(center = (140, 140))
self.spaceship_main.mask = pygame.mask.from_surface(self.spaceship_main.image)
self.alien_spacecraft1 = pygame.sprite.Sprite()
self.alien_spacecraft1.image = pygame.image.load('spacecraft_alien.png').convert_alpha()
self.alien_spacecraft1.rect = self.alien_spacecraft1.image.get_rect(center = (160, 160))
self.alien_spacecraft1.mask = pygame.mask.from_surface(self.alien_spacecraft1.image)
def _is_collision(self):
if pygame.sprite.collide_mask(self.spaceship_main, self.alien_spacecraft1):
return True
return False
我正在尝试获得像素完美碰撞,但我一直 运行 进入此错误:
Traceback (most recent call last):
File "D:\Projects\games\venv\lib\site-packages\pygame\sprite.py", line 1528, in collide_circle
xdistance = left.rect.centerx - right.rect.centerx
AttributeError: 'pygame.mask.Mask' object has no attribute 'rect'
Process finished with exit code 1
我不确定出了什么问题。是我的实现或逻辑还是两者兼而有之?
我的代码:
class AlienShooting(pygame.sprite.Sprite):
def __init__(self, w=640, h=640):
# init display
# init game state
self.spaceship_main = pygame.image.load('spacecraft_1.png').convert_alpha()
self.alien_spacecraft1 = pygame.image.load('spacecraft_alien.png').convert_alpha()
self.alien_spacecraft_rec1 = self.alien_spacecraft1.get_rect()
self.mask1 = pygame.mask.from_surface(self.alien_spacecraft1)
self.main_spaceship_mask = pygame.mask.from_surface(self.spaceship_main)
def play_step(self):
# user input
# Move the spaceship
# check if game over
game_over = False
if self._is_collision():
game_over = True
# Add new alien spaceships
# Randomly move alien spaceships
# update ui and clock
# return game over and score
return False
def _is_collision(self):
if pygame.sprite.collide_circle(self.mask1, self.main_spaceship_mask):
return True
return False
可能是因为我的主space飞船和外星人space飞船在同一个class,但我不确定。
查看 How do I detect collision in pygame? and pygame.sprite.collide_circle()
or pygame.sprite.collide_mask
you have to create pygame.sprite.Sprite
个对象。
spaceship_main
和 alien_spacecraft1
必须是 Sprite 对象。 Sprite 对象必须具有 rect
和 image
属性,并且可以选择具有 mask
属性。
例如:
class AlienShooting():
def __init__(self, w=640, h=640):
self.spaceship_main = pygame.sprite.Sprite()
self.spaceship_main.image = pygame.image.load('spacecraft_1.png').convert_alpha()
self.spaceship_main.rect = self.spaceship_main.image.get_rect(center = (140, 140))
self.spaceship_main.mask = pygame.mask.from_surface(self.spaceship_main.image)
self.alien_spacecraft1 = pygame.sprite.Sprite()
self.alien_spacecraft1.image = pygame.image.load('spacecraft_alien.png').convert_alpha()
self.alien_spacecraft1.rect = self.alien_spacecraft1.image.get_rect(center = (160, 160))
self.alien_spacecraft1.mask = pygame.mask.from_surface(self.alien_spacecraft1.image)
def _is_collision(self):
if pygame.sprite.collide_mask(self.spaceship_main, self.alien_spacecraft1):
return True
return False