按坐标对矩形进行置位



在Pygame和Python 2.7中,我如何在某一点上用一组坐标表示矩形?

我知道我可以用这个:

screen.blit(img.image, img.rect.topleft)

但是我希望矩形在屏幕上的一个精确点

如果您需要在点(34,57)的矩形的左角,您可以

screen.blit(img.image, (34,57) )

img.rect.topleft = (34,57)
screen.blit(img.image, img.rect)

img.rect.x = 34
img.rect.y = 57
screen.blit(img.image, img.rect)

如果需要(34,57)点的矩形中心

img.rect.center = (34,57)
screen.blit(img.image, img.rect)

如果你需要屏幕中央的矩形:
(在需要显示文本时尤其有用)。("PAUSE")在屏幕中央,或文本在矩形中心,以创建按钮)

img.rect.center = screen.get_rect().center
screen.blit(img.image, img.rect)

如果你需要矩形触摸屏幕的右边框:

img.rect.right = screen.get_rect().right
screen.blit(img.image, img.rect)

如果你需要屏幕左下角的矩形:

img.rect.bottomleft = screen.get_rect().bottomleft
screen.blit(img.image, img.rect)

和你有更多-见pygame。矩形

x,y
top, left, bottom, right
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery

使用上述元素不会改变widthheight
如果您更改x(或其他值),那么您将自动获得left, right和其他的新值。

BTW:如您所见,您可以使用img.rect作为blit()

的参数

顺便说一句:你也可以这样做:(例如在__init__):

img.rect = img.image.get_rect(center=screen.get_rect().center)

到屏幕中央

BTW:你也可以使用它来blit图像/Surface在其他Surface在一个精确的点。你可以把文本放在某个平面的中心(例如:button)然后把这个平面放在屏幕的右下角

从你的代码:

screen.blit(img.image, img.rect.topleft)

将把图像置于(0,0)位置,因为该矩形是从尚未绘制到显示表面的图像中获得的。如果你想在一个特定的坐标绘制,只需这样做:

screen.blit(image, (x, y))     #x and y are the respective position coordinates

最新更新