如何修复命中箱的命中框,禁止调整每个图像的大小?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
参见 /a> and 我如何用pygame将图像播种?:
使用
pygame.surface。 get_bounding_rect()
和 pygame.surface.subsurface 从您的图像中获取一个裁剪的区域。例如:
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()
andpygame.Surface.subsurface
to get a cropped region from your image.e.g.: