You can use .get_rect()
for image to get image rectangle (pygame.Rect()
)
space_ship = pygame.image.load(spaceship).convert_alpha()
space_ship_rect = space_ship.get_rect()
and than you can get x
, y
, width
, height
and even centerx
, centery
, center
etc.
print space_ship_rect.x, space_ship_rect.y,
print space_ship_rect.centerx, space_ship_rect.centery,
print space_ship_rect.center
print space_ship_rect.left, space_ship_rect.right
print space_ship_rect.top, space_ship_rect.bottom
print space_ship_rect.topleft, space_ship_rect.bottomright
print space_ship_rect.width, space_ship_rect.height
by the way: .get_rect()
works also with your screen
and other pygame.Surface()
.
But you can't assign new values to image so you have to keep space_ship_rect
and change values in it (so use get_rect()
only once to get image size)
space_ship_rect.x = 100
space_ship_rect.y = 200
# or
space_ship_rect.centerx = 100
space_ship_rect.centery = 200
If you change x
, y
rectangle recalculate centerx
, centery
using width
and height
. If you change centerx
, centery
rectangle recalculate x
and y
You could create class with self.image
for bitmap and self.rect
for image size and position.
ps. you can use screen.blit(space_ship, space_ship_rect)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…