Python 你能移动图像的直线中心吗?
我的图像的矩形就在图像的右侧,我想将矩形向左移动几个像素或将图像向右移动。此外,是否有一种方法可以将矩形向下移动或使顶部变小,同时矩形底部保持不变Python 你能移动图像的直线中心吗?,python,pygame,collision,rect,Python,Pygame,Collision,Rect,我的图像的矩形就在图像的右侧,我想将矩形向左移动几个像素或将图像向右移动。此外,是否有一种方法可以将矩形向下移动或使顶部变小,同时矩形底部保持不变 PLAYER_RECT_HIT = py.Rect(0, 0, 33, 33) class Player(py.sprite.Sprite): def __init__(self, game, x, y): self._layer = PLAYER_LAYER self.groups = game.all_sp
PLAYER_RECT_HIT = py.Rect(0, 0, 33, 33)
class Player(py.sprite.Sprite):
def __init__(self, game, x, y):
self._layer = PLAYER_LAYER
self.groups = game.all_sprites
py.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.running = False
self.jumping = False
self.wallSlide = False
self.current_frame = 0
self.last_update = 0
self.load_images()
self.image = self.idle_frames[0]
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.hit_rect = PLAYER_RECT_HIT
self.hit_rect.center = self.rect.center
self.pos = vec(x, y)
self.vel = vec(0, 0)
self.acc = vec(0, 0)
这里有一个例子,红色的框是我的矩形,我想把它移到左边,这样我的图像就会接触到蓝色框的矩形
如果要使矩形顶部变小,而底部保持在同一位置,可以执行以下操作:
newRect = pygame.Rect(
oldRect.top - 10,
oldRect.left,
oldRect.width - 10,
oldRect.height
)
希望这有帮助 我的图像的矩形就在图像的右边,这到底意味着什么?您调用self.rect=self.image.get_rect,因此rect正好是图像的大小。