如何获取从图像创建的 pygame 矩形的正确尺寸 [英] How to get the correct dimensions for a pygame rectangle created from an image

查看:51
本文介绍了如何获取从图像创建的 pygame 矩形的正确尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用命令 pygame.image.get_rect() 创建了精灵的图像矩形.在bottomleft等矩形上通过画点定位关键点的坐标时,我发现矩形的尺寸与图像的尺寸完全不同.在这种情况下,图像只是箭头.矩形的右上角坐标是正确的,但左下角坐标不正确.

绘制点的代码:

pygame.draw.circle(simulation_screen, red, velocity_arrow.rect.topright, 2)pygame.draw.circle(simulation_screen,red,velocity_arrow.rect.bottomleft,2)

如何调整矩形的大小以使其适合图像?

解决方案

导入pygamedef getMaskRect(surf, top = 0, left = 0):冲浪面具 = pygame.mask.from_surface(surf)rect_list = surf_mask.get_bounding_rects()surf_mask_rect = rect_list[0].unionall(rect_list)surf_mask_rect.move_ip(顶部,左侧)返回 surf_mask_rectpygame.init()窗口 = pygame.display.set_mode((400, 400))时钟 = pygame.time.Clock()尝试:my_image = pygame.image.load('Bomb-256.png')除了:my_image = pygame.Surface((200, 200), pygame.SRCALPHA)pygame.draw.circle(my_image, (0, 128, 0), (60, 60), 40)pygame.draw.circle(my_image, (0, 0, 128), (100, 150), 40)运行 = 真运行时:时钟滴答(60)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误pos = window.get_rect().centermy_image_rect = my_image.get_rect(center = pos)my_image_mask_rect = getMaskRect(my_image, *my_image_rect.topleft)window.fill((255, 255, 255))window.blit(my_image, my_image_rect)pygame.draw.rect(window, (0, 0, 0), my_image_rect, 3)pygame.draw.rect(window, (255, 0, 0), my_image_mask_rect, 3)pygame.display.flip()pygame.quit()出口()

I have created an image's rectangle of a sprite using the command, pygame.image.get_rect(). When locating coordinates of key points on the rectangle such as bottomleft by drawing dots, I see that the rectangle's dimensions are completely different to the image's dimensions. In this case, the image is just the arrow head. The top right coordinate of the rectangle is correct, however the bottom left coordinate isn't.

Test to see rectangle

code to draw dots:

pygame.draw.circle(simulation_screen, red, velocity_arrow.rect.topright, 2)
pygame.draw.circle(simulation_screen,red,velocity_arrow.rect.bottomleft,2)

How can I resize the rectangle so that it fits the image?

解决方案

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object. This function does not consider the drawing area in the image. If you want to find the bounding rectangle of the painted area in the surface, you need to create a mask.

pygame.mask.from_surface creates a pygame.mask.Mask object form a pygame.Surface.
A Surface is bitmap. A Mask is an 2 dimensional array with Boolean values. The Mask created is the size of the _Surface. A field is True if the corresponding pixel in the surface is not transparent, and False if it is transparent:

surf_mask = pygame.mask.from_surface(surf)

Get a list containing a bounding rectangles (sequence of pygame.Rect objects) for each connected component with get_bounding_rects.
pygame.mask.Mask.get_bounding_rects creates a list of pygame.Rect objects. Each rectangle describes a bounding area of connected pixles. If the Surface contains exactly 1 connected image, you will get exactly 1 rectangle surrounding the image:

rect_list = surf_mask.get_bounding_rects()

Create the union rectangle of the sequence of rectangles with unionall:

surf_mask_rect = rect_list[0].unionall(rect_list)


See the example. The black rectangle is the Surface rectangle and the red rectangle is the union of the mask component rectangles:

import pygame

def getMaskRect(surf, top = 0, left = 0):
    surf_mask = pygame.mask.from_surface(surf)
    rect_list = surf_mask.get_bounding_rects()
    surf_mask_rect = rect_list[0].unionall(rect_list)
    surf_mask_rect.move_ip(top, left)
    return surf_mask_rect

pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

try:
    my_image = pygame.image.load('Bomb-256.png')
except:
    my_image = pygame.Surface((200, 200), pygame.SRCALPHA)
    pygame.draw.circle(my_image, (0, 128, 0), (60, 60), 40)
    pygame.draw.circle(my_image, (0, 0, 128), (100, 150), 40)

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    pos = window.get_rect().center
    my_image_rect = my_image.get_rect(center = pos)
    my_image_mask_rect = getMaskRect(my_image, *my_image_rect.topleft)

    window.fill((255, 255, 255))
    window.blit(my_image, my_image_rect)
    pygame.draw.rect(window, (0, 0, 0), my_image_rect, 3)
    pygame.draw.rect(window, (255, 0, 0), my_image_mask_rect, 3)
    pygame.display.flip()

pygame.quit()
exit()

这篇关于如何获取从图像创建的 pygame 矩形的正确尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆