class Weapon(pygame.sprite.Sprite): def __init__(self, damage, w_type, sprite_sheets): super().__init__() self.idle = Animation(sprite_sheets[0], 128, 8, 1) self.attack = Animation(sprite_sheets[1], 128, 8, 4) self.block = Animation(sprite_sheets[2], 128, 8, 1) self.state = "idle" self.count = 0 self.image = self.idle.images[0] self.mask = pygame.mask.from_surface(self.image) self.image_original = self.idle.images[0] self.rect = self.image.get_rect() self.damage = damage self.type = w_type def rot_center(self): """rotate an image while keeping its center and size""" x, y = pygame.mouse.get_pos() relx, rely = x - self.rect.x, y - self.rect.y angle = math.atan2(relx, rely) angle = math.degrees(angle) orig_rect = self.image_original.get_rect() rot_image = pygame.transform.rotate(self.image_original, angle) rot_rect = orig_rect.copy() rot_rect.center = rot_image.get_rect().center rot_image = rot_image.subsurface(rot_rect).copy() return rot_image def special_ability(self): pass def update(self, player): self.count += 1 if self.state == "idle": self.image_original = self.idle.images[0] self.count = 0 if self.state == "attack": self.image_original = self.attack.update() if self.count >= self.attack.frame_loop: self.state = "idle" if self.state == "block": self.image_original = self.block.update() if self.count >= self.block.frame_loop: self.state = "idle" self.image = self.rot_center() self.rect.centerx = player.rect.centerx self.rect.centery = player.rect.centery self.mask = pygame.mask.from_surface(self.image)
class Player(pygame.sprite.Sprite): """Player class that is used to create player sprite as well as control player attributes""" def __init__(self): """initialisation of class""" super().__init__() sprite_sheet = SpriteSheet('character.png') self.image = sprite_sheet.get_image(0, 0, 128, 128) self.image_original = self.image self.mask = pygame.mask.from_surface(self.image) self.rect = self.image.get_rect() self.rect.x = 540 - self.rect.centerx self.rect.y = 360 - self.rect.centery self.speed = 5 self.health = 100 self.max_health = 100 self.dead = False self.ghost = Animation("ghost.png", 64, 8, 3) def rot_center(self): """rotate an image while keeping its center and size""" x, y = pygame.mouse.get_pos() relx, rely = x - self.rect.x, y - self.rect.y angle = math.atan2(relx, rely) angle = math.degrees(angle) orig_rect = self.image_original.get_rect() rot_image = pygame.transform.rotate(self.image_original, angle) rot_rect = orig_rect.copy() rot_rect.center = rot_image.get_rect().center rot_image = rot_image.subsurface(rot_rect).copy() return rot_image def update(self): """Rotates the player so that it follows the mouse""" if not self.dead: self.image = self.rot_center() self.mask = pygame.mask.from_surface(self.image) if self.health <= 0: self.dead = True self.image = self.ghost.update() self.mask = pygame.mask.from_surface(self.image)
class NPC(pygame.sprite.Sprite): def __init__(self, speed, sprite_sheets, screensize=(1080, 720)): super().__init__() self.idle = Animation(sprite_sheets[0], 64, 6, 3) self.image = self.idle.images[0] self.mask = pygame.mask.from_surface(self.image) self.image_original = self.idle.images[0] self.rect = self.image.get_rect() self.speed = speed self.time = 0 self.move = "L" self.screensize = screensize self.health = 20 self.dead = False self.vulnerability = False self.vul_count = 0 def update(self, player): """General updates for NPC""" self.image = self.idle.update() self.mask = pygame.mask.from_surface(self.image) if self.vulnerability: self.vul_count += 1 if self.vul_count >= 15: self.vul_count = 0 self.vulnerability = False if self.health <= 0: self.dead = True def damaged(self, amount): self.health -= amount self.vulnerability = True def movex(self, speed): self.rect.x += speed def movey(self, speed): self.rect.y += speed