Pygame Image position

后端 未结 1 416
情歌与酒
情歌与酒 2020-12-07 06:17

I\'ve recentley started using pygame to see what I could come up with and come across a problem which I can\'t find an answer to without this pygame.sprite.Sprite and rect t

相关标签:
1条回答
  • 2020-12-07 06:55

    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)

    0 讨论(0)
提交回复
热议问题