Reputation: 47
I'm not sure why my attack animation is lower than the other animations for my enemy. Every time it attacks it's y
seems lower. I'm not sure why, even though I centered all of them the same.
player_rect = player_image.get_rect(center = self.get_rect().center)
player_rect.centerx += 0
player_rect.centery += -6
window.blit(player_image, player_rect)
My enemies class:
class rat:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.upshot = [pygame.image.load("rawlk (" + str(i) + ").png").convert_alpha() for i in range(1, 12)]
self.idle = [pygame.image.load("idle (" + str(i) + ").png").convert_alpha() for i in range(1, 21)]
self.id = [pygame.image.load("id (" + str(i) + ").png").convert_alpha() for i in range(1, 21)]
self.move2 = [pygame.image.load("walk (" + str(i) + ").png").convert_alpha() for i in range(1, 12)]
self.att = [pygame.image.load("att (" + str(i) + ").png").convert_alpha() for i in range(1, 8)]
self.at = [pygame.image.load("at (" + str(i) + ").png").convert_alpha() for i in range(1, 8)]
self.color = color
self.rect = pygame.Rect(x,y,height,width)
self.fps = 60
self.clock = pygame.time.Clock()
self.next_frame_time = 0
self.anim_index = 0
self.time = 0
self.direction = "move"
self.direction = "move2"
self.direction = "idle"
self.direction = "att"
self.direction = "at"
self.direction = "id"
self.speed = 1
self.image = pygame.image.load("ratso.png").convert_alpha()
self.image2 = pygame.image.load("ratso2.png").convert_alpha()
self.font = pygame.font.Font("Gugi-Regular.ttf", 20)
self.scoretext = self.font.render("Lv 1", True, (255,255,255))
self.scorerect = self.scoretext.get_rect()
self.scorerect.center = ((400,550))
self.hitbox2 = (self.x - 47, self.y + 11, 169, 52) # NEW
self.hitbox = (self.x + 17, self.y + 11, 129, 52) # NEW
def get_rect(self):
self.rect.topleft = (self.x,self.y)
return self.rect
def draw(self):
self.rect.topleft = (self.x,self.y)
if self.direction == "move":
self.clock.tick(self.fps)
image_list = self.upshot
elif self.direction == "idle":
self.clock.tick(self.fps)
image_list = self.idle
elif self.direction == "id":
self.clock.tick(self.fps)
image_list = self.id
elif self.direction == "move2":
self.clock.tick(self.fps)
image_list = self.move2
elif self.direction == "att":
self.clock.tick(self.fps)
image_list = self.att
elif self.direction == "at":
self.clock.tick(30)
image_list = self.at
time_now = pygame.time.get_ticks()
if (time_now > self.next_frame_time):
# Time until the next game
inter_time_delay = 2500 // self.fps
self.next_frame_time = time_now + inter_time_delay
# Showing next frame
self.anim_index += 1
if self.anim_index >= len(image_list):
self.anim_index = 0
if self.anim_index >= len(image_list):
self.anim_index = 0
player_image = image_list[self.anim_index]
self.hitbox = (self.x, self.y + 30, 46,60)
# --------------------------------------------------------------------- center all of them
player_rect = player_image.get_rect(center = self.get_rect().center)
player_rect.centerx += 0
player_rect.centery += -6
window.blit(player_image, player_rect)
# --------------------------------------------------------------------- center all of them
window.blit(self.scoretext,(self.x + 15,self.y - 50))
self.hitbox2 = (self.x - 190, self.y - 22, 159, 40) # NEW
self.hitbox = (self.x + 80, self.y - 22, 159, 40) # NEW
Upvotes: 0
Views: 90
Reputation: 210946
Not all frames used for animation appear to be the same height. To compensate for this, and since the rat is on the floor, you need to define the bottom of the rectangle (midbottom
), not the center:
player_rect = player_image.get_rect(center = self.get_rect().center)
player_rect = player_image.get_rect(midbottom = self.get_rect().midbottom)
window.blit(player_image, player_rect)
Note, If the animation frames are of different sizes or if the rat's position on the sprite changes, the rat's position in the game will also change during the animation.
Upvotes: 2