The mask seems to do nothing. As can be seen, I have tried several ways.
Image Loading with Scaling
def load_image(filename, scale):
image = pygame.image.load(filename).convert_alpha()
return pygame.transform.scale(image, scale)
— Sprite Classes —
class Ship(pygame.sprite.Sprite):
def init(self):
super().init()
self.original_image = load_image(“player_ship.png”, (30, 30))
self.image = self.original_image.copy()
self.rect = self.image.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
# Load the mask image and scale it to match the ship image
self.mask_image = load_image("mask_test.png", (30, 30))
# Create the mask from the mask image
self.mask = pygame.mask.from_surface(self.mask_image)
# Since the mask is based on the mask_test.png image, we need to align it properly
# Get the rect of the mask image
self.mask_rect = self.mask_image.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)) #center=self.rect.center) #(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
# Center the mask rect within the ship's rect
#self.mask_rect.topleft = self.rect.topleft
# Apply the mask rect to the mask, so it matches the ship's position
#self.mask_rect = self.t(ask_rect.topleft) #self.image.get_rect(center=self.rect.center) #self.topleft(mask_rect.topleft)
I have a player_ship png that is rect image, but the shape of the ship is triangular. Using GIMP, I can create an image with transparency. I was trying to get a mask such that it only shows the actual ship and build to nice collision detection from there.
Kevin Thoele is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.