class Entity():
def __init__(self, char_type, x, y, scale):
self.char_type = char_type
self.flip = False
self.direction = 1
self.vel_y = 0
self.jump = False
self.attacking = False
self.animation_list = []
self.frame_index = 0
self.action = 0
self.update_time = pygame.time.get_ticks()
#load all images
animation_types = ['idle', 'run', 'jump', 'attack', 'death', 'hit']
for animation in animation_types:
#reset temporary list of images
temp_list = []
#count number of files in the folder
num_of_frames = len(os.listdir(f"img/{self.char_type}/{animation}"))
for i in range(num_of_frames-2):
img = pygame.image.load(f"img/{self.char_type}/{animation}/{i}.png")
img = pygame.transform.scale(img, (img.get_width()*scale,img.get_height()*3))
temp_list.append(img)
self.animation_list.append(temp_list)
self.image = self.animation_list[self.action][self.frame_index]
self.rect = self.image.get_rect()
self.rect.center = (x, y)
This is my first time attempting a pygame project and i'm having issues. Basically the frames are really big so the rectangle I made with self.rect = self.image.get_rect()
to create the hitbox for my playable character is massive. what it looks like I tried solving this by using self.rect = self.image.get_bounding_rect()
This did solve the issue with having a massive rectangle however it made the image which I drew using the following method
def draw(self, surface):
img = pygame.transform.flip(self.image, self.flip, False)
pygame.draw.rect(surface, (255, 0 , 0), self.rect)
surface.blit(img, self.rect)
to not be centered over the rectangle which should be its hitbox. That ended up looking like this. I think that the issue is that
See How to get the correct dimensions for a pygame rectangle created from an image and How can I crop an image with Pygame?:
Use pygame.Surface.get_bounding_rect()
and pygame.Surface.subsurface
to get a cropped region from your image.
e.g.:
original_image = pygame.image.load(f"img/{self.char_type}/{animation}/{i}.png")
crop_rect = original_image.get_bounding_rect()
cropped_image = original_image.subsurface(crop_rect).copy()
img = pygame.transform.scale(cropped_image,
(cropped_image.get_width()*scale, cropped_image.get_height()*3))