如何修复命中箱的命中框,禁止调整每个图像的大小?

发布于 2025-02-04 15:49:39 字数 1839 浏览 4 评论 0原文

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)

这是我第一次尝试使用Pygame项目,我有问题。基本上,这些帧真的很大,因此我用self.Rect = self.image.get_rect()创建我可玩角色的hitbox的矩形。 它的外观我尝试使用self.Rect = self.image尝试解决此问题.get_bounding_rect()这确实解决了具有巨大矩形的问题,但是它使我使用以下方法绘制了图像,

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)

以不在矩形上,这应该是其hitbox。最终看起来像 this。我认为问题是

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千秋岁 2025-02-11 15:49:39

参见 /a> and 我如何用pygame将图像播种?

使用 pygame.surface。 get_bounding_rect() pygame.surface.subsurface 从您的图像中获取一个裁剪的区域。

例如:

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))

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文