I'm making a platform game and I got player standing and walking animations. I used tempbottom and tempcenterx to keep the player's position so he doesn't flash backwards or forwards when start walking.
But I want to keep the standing rect while walking for convenience. Is there any way to do that? Or is there any way to leave rect unchanged while changing the relative position when changing image?
Here's my sprites, they got different canvas size when exported:
Standing
Walking
Here's my code up to walking part:
class Player():
def __init__(self,x,y):
self.img_stand = pygame.image.load('D:\ThingThing\inthething\H stand.png')
self.img_standL = pygame.image.load('D:\ThingThing\inthething\H standL.png')
#walk animation
self.images_right = []
self.images_left = []
self.index = 0
self.counter = 0
self.direction = 0
for num in range(1,17):
img_right = pygame.image.load(f'D:\ThingThing\inthething\H walk{num}.png')
self.images_right.append(img_right)
for num in range(1,17):
img_left = pygame.image.load(f'D:\ThingThing\inthething\H walkL{num}.png')
self.images_left.append(img_left)
self.image = self.img_stand
self.width = self.image.get_width()
self.height = self.image.get_height()
self.rect = self.image.get_rect()
print(self.rect)
self.rect.x = x
self.rect.y = y
self.width = self.image.get_width()
self.height = self.image.get_height()
self.vel_y = 0
self.jumped = False
def update(self):
dx = 0
dy = 0
walk_cooldown = 8
#get key
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
dx -= 1
self.image = self.images_left[self.index]
self.counter += 1
self.tempx = self.rect.centerx
self.tempy = self.rect.bottom
self.rect = self.image.get_rect()
self.rect.centerx = self.tempx
self.rect.bottom = self.tempy
self.direction = -1
if key[pygame.K_RIGHT]:
dx += 1
self.image = self.images_right[self.index]
self.counter += 1
self.tempx = self.rect.centerx
self.tempy = self.rect.bottom
self.rect = self.image.get_rect()
self.rect.centerx = self.tempx
self.rect.bottom = self.tempy
self.direction = +1
I just don't know how to modify the rects. I exported the standing animation with the image tightly cornered by borders and I did the same for walking animation. I managed to reposition but that would change the rect, I really want to keep the standing rect.