def bring(self, minion): if minion == "gatherer": if self.gathval: mini = Sprite(load_animation("resources/gatherer_coming.gif"), position = (spawn_place[minion][0] + minion_move_to[minion][0], spawn_place[minion][1] + minion_move_to[minion][1])) mini.scale = minion_scale self.add(mini) mini.do(MoveTo(spawn_place[minion], minion_move_time) + CallFunc(mini.kill)) self.gathval = False else: mini1 = Sprite(load_animation("resources/gatherer.gif"), position = spawn_place[minion]) mini1.scale = minion_scale self.add(mini1) mini1.do(MoveBy(minion_move_to[minion], minion_move_time) + CallFunc(mini1.kill)) self.gathval = True elif minion == "miner": if self.minval: mini = Sprite(load_animation("resources/miner.gif"), position = (spawn_place[minion ][0] + minion_move_to[minion][0], spawn_place[minion][1] + minion_move_to[minion][1])) mini.scale = minion_scale mini.scale_x = -minion_scale self.add(mini) mini.do(MoveTo( (spawn_place[minion]), minion_move_time) + CallFunc(mini.kill) ) self.minval = False else: mini1 = Sprite(load_animation("resources/miner.gif"), position = spawn_place[minion]) mini1.scale = minion_scale self.add(mini1) mini1.do(MoveBy(minion_move_to[minion], minion_move_time) + CallFunc(mini1.kill)) self.minval = True
def challenge(self, type, pos): hunter = Sprite(load_animation("resources/" + type + ".gif"), position = (random.randint(483, 826), 105)) hunter.scale = minion_scale self.add(hunter) hunter.do(MoveTo(pos, minion_move_time)) return hunter
class Attack(Sprite): attack_animation = image.load_animation('images/Attack.gif', None, None) def __init__(self, game_scale): super().__init__(img=Attack.attack_animation) self.scale = game_scale self.opacity = 128 def scale_attack(self, game_scale): self.scale = game_scale def update_self(self, player_x, player_y, direction): if (direction == Direction.NORTH): this_x = player_x this_y = player_y + (40 * self.scale) self.update(x=this_x, y=this_y, rotation=0) elif (direction == Direction.EAST): this_x = player_x + (40 * self.scale) this_y = player_y + (40 * self.scale) self.update(x=this_x, y=this_y, rotation=90) elif (direction == Direction.SOUTH): this_x = player_x + (40 * self.scale) this_y = player_y self.update(x=this_x, y=this_y, rotation=180) else: this_x = player_x this_y = player_y self.update(x=this_x, y=this_y, rotation=270) def reset_animation(self): self.image = Attack.attack_animation
def image_load(self): """Loads the sprites used for player sprite: standing, jumping, running around. """ self.standing = img.load('assets/corgi1.png') self.running = img.load_animation('assets/corgi1.gif') self.running.add_to_texture_bin(img.atlas.TextureBin()) self.jumping = img.load('assets/jump.png')
def __init__(self): super().__init__(255, 255, 255, 255) # method 1 anim1 = resource.animation('dinosaur.gif', flip_x=True) self.sprite1 = Sprite(anim1, (150, 250)) self.add(self.sprite1) anim2 = image.load_animation("dinosaur.gif") self.sprite2 = Sprite(anim2, (350, 250)) self.add(self.sprite2) # method 2 sprite_set = image.load("spriteset.png") image_grid = image.ImageGrid(sprite_set, 2, 5) frame_seq1 = image_grid[5:8] anim3 = image.Animation.from_image_sequence(frame_seq1, 0.1) anim3_flip = anim3.get_transform(flip_x=True) self.sprite3 = Sprite(anim3_flip, (150, 100)) self.add(self.sprite3) # method 3 self.sprite4 = Sprite(frame_seq1[0], (350, 100)) self.add(self.sprite4) self.schedule(self.update, frame_seq1) self.elapsed = 0 self.frame_idx = 0
def bring(self, minion): if minion == "gatherer": if self.gathval: mini = Sprite(load_animation("resources/gatherer_coming.gif"), position=(spawn_place[minion][0] + minion_move_to[minion][0], spawn_place[minion][1] + minion_move_to[minion][1])) mini.scale = minion_scale self.add(mini) mini.do( MoveTo(spawn_place[minion], minion_move_time) + CallFunc(mini.kill)) self.gathval = False else: mini1 = Sprite(load_animation("resources/gatherer.gif"), position=spawn_place[minion]) mini1.scale = minion_scale self.add(mini1) mini1.do( MoveBy(minion_move_to[minion], minion_move_time) + CallFunc(mini1.kill)) self.gathval = True elif minion == "miner": if self.minval: mini = Sprite(load_animation("resources/miner.gif"), position=(spawn_place[minion][0] + minion_move_to[minion][0], spawn_place[minion][1] + minion_move_to[minion][1])) mini.scale = minion_scale mini.scale_x = -minion_scale self.add(mini) mini.do( MoveTo((spawn_place[minion]), minion_move_time) + CallFunc(mini.kill)) self.minval = False else: mini1 = Sprite(load_animation("resources/miner.gif"), position=spawn_place[minion]) mini1.scale = minion_scale self.add(mini1) mini1.do( MoveBy(minion_move_to[minion], minion_move_time) + CallFunc(mini1.kill)) self.minval = True
def challenge(self, type, pos): hunter = Sprite(load_animation("resources/" + type + ".gif"), position=(random.randint(483, 826), 105)) hunter.scale = minion_scale self.add(hunter) hunter.do(MoveTo(pos, minion_move_time)) return hunter
def __init__(self, logic): super().__init__() self.monster = Sprite(load_animation("resources/monster.gif"), position = monster_pos) self.gold = Gold(self) logic.push_handlers(self.gold) self.add(self.monster)
def invoke(self, minion): mini = Sprite(load_animation("resources/" + minion + ".gif"), position=spawn_place[minion]) mini.scale = minion_scale self.add(mini) mini.do( MoveBy(minion_move_to[minion], minion_move_time) + CallFunc(mini.kill))
def __init__(self): self.animation = load_animation(self.src) self.animation.frames[-1].duration = None # stop loop super(ExplosionTankAnimation, self).__init__(self.animation) self.anim = OnceAnimation(self.animation) self.anim.image_anchor = (self.animation.get_max_width() / 2, self.animation.get_max_height() / 4) self.anim.scale = 0.5
def __init__(self, logic): super().__init__() self.monster = Sprite(load_animation("resources/monster.gif"), position=monster_pos) self.gold = Gold(self) logic.push_handlers(self.gold) self.add(self.monster)
def __init__(self): self.animation = load_animation(self.src) self.animation.frames[-1].duration = None # stop loop super(explosionStandartBulletAnimation, self).__init__(self.animation) # self.anim = sprite.Sprite(self.animation) self.anim = OnceAnimation(self.animation) self.anim.image_anchor = (self.animation.get_max_width() / 2, self.animation.get_max_height() / 4) self.anim.scale = 0.2
def load_anim(self): print('load_anim') self.animation = load_animation(self.src) #self.bin = TextureBin() self.animation.frames[-1].duration = None # stop loop #self.anim = sprite.Sprite(self.animation) # self.anim = OnceAnimation(self.animation) # self.anim.image_anchor = (self.animation.get_max_width() / 2, self.animation.get_max_height() / 4) # self.anim.scale = 0.2 self.duration = self.animation.get_duration() + 1
class Visibility(Sprite): darkness = image.load_animation('images/Visibility.gif', None, None) def __init__(self, a_scale): super().__init__(img=Visibility.darkness) self.scale = 4 * a_scale self.game_scale = a_scale def update_coords(self, aX, aY): self.x = aX - (self.scale * 600) self.y = aY - (self.scale * 600)
def _load_content(self): """generates content from resources""" for extension in Config.graphics_format: if extension in self.resources: for resource in self.resources[extension]: pattern = r"(?P<type>[A-Za-z]+)_(?P<name>[\w_\.]+)\.(?P<extension>\w+)" matched_file_name = match(pattern, resource) content_group = matched_file_name.group("type") if content_group not in self.content: self.content[content_group] = {} if extension == "gif": self.content[content_group][matched_file_name.group( "name")] = load_animation("{}/{}".format( Config.resources, resource)) else: self.content[content_group][matched_file_name.group( "name")] = load("{}/{}".format( Config.resources, resource))
def invoke(self, minion): mini = Sprite(load_animation("resources/" + minion + ".gif"), position = spawn_place[minion]) mini.scale = minion_scale self.add(mini) mini.do(MoveBy(minion_move_to[minion], minion_move_time) + CallFunc(mini.kill))
self.next_frame(0) def next_frame(self, dt): self.index = (self.index + 1) % len(self.animation.frames) frame = self.animation.frames[self.index] if frame.duration is not None: delay = frame.duration - (self.expected_delay - dt) delay = min(max(0, delay), frame.duration) clock.schedule_once(self.next_frame, delay) self.expected_delay = delay def blit(self, x, y): self.animation.frames[self.index].image.blit(x, y) try: animation = image.load_animation(sys.argv[1]) except image.codecs.ImageDecodeException: from pyglet import media source = media.load(sys.argv[1]) source._seek(0) animation = source.get_animation() except IndexError: sys.exit('usage: animation.py <image>') w = window.Window() clock.tick() player = AnimationPlayer(animation) while not w.has_exit:
class Player(Sprite): north_standing_img = image.load('images/PlayerNorthStanding.png') east_standing_img = image.load('images/PlayerEastStanding.png') south_standing_img = image.load('images/PlayerSouthStanding.png') west_standing_img = image.load('images/PlayerWestStanding.png') north_animation = image.load_animation('images/PlayerNorth.gif', None, None) east_animation = image.load_animation('images/PlayerEast.gif', None, None) south_animation = image.load_animation('images/PlayerSouth.gif', None, None) west_animation = image.load_animation('images/PlayerWest.gif', None, None) def __init__(self, a_scale, given_name, backgroundX, backgroundY, darkness): super().__init__(img=Player.south_standing_img) self.game_scale = a_scale self.scale = a_scale self.name = given_name self.x = backgroundX + (480 * a_scale) self.y = backgroundY + (480 * a_scale) self.nextBoxCoord = 0 self.level = 0 self.room_number = 0 self.life = Life(game_scale=a_scale, backX=backgroundX, backY=backgroundY) self.experience = Experience(game_scale=a_scale, backX=backgroundX, backY=backgroundY) self.next_up_stat = 0 self.stat_boosted = Label("++Defense++", font_name='Times New Roman', font_size=32 * a_scale, x=backgroundX + (500 * a_scale), y=backgroundY + (980 * a_scale), color=(55, 235, 52, 0), align='center', anchor_x='center', anchor_y='center', bold=True) self.defense = 1 self.speed = 240 * a_scale self.attack = 0 self.player_inventory = Inventory(game_scale=a_scale, backX=backgroundX, backY=backgroundY, the_player=self) self.facing = Direction.SOUTH self.is_moving = False self.scheduled_moving = False self.is_attacking = False self.queued_direction = None self.selected_weapon = None self.selected_helmet = None self.selected_chestpiece = None self.selected_leggings = None self.selected_footwear = None self.selected_torch = None self.attack_sprite = Attack(game_scale=a_scale) self.visibility = darkness self.add_to_inventory(to_add=Type.Torch) def scale_player(self, game_scale, x_offset): self.x = (self.x * game_scale) + x_offset self.y = (self.y * game_scale) self.game_scale = game_scale self.scale = game_scale if self.is_facing == Direction.NORTH or self.is_facing == Direction.SOUTH: self.nextBoxCoord = (self.nextBoxCoord * game_scale) else: self.nextBoxCoord = (self.nextBoxCoord * game_scale) + x_offset self.stat_boosted.font_size = self.stat_boosted.font_size * game_scale self.stat_boosted.x = (self.stat_boosted.x * game_scale) + x_offset self.stat_boosted.y = (self.stat_boosted.y * game_scale) self.speed = self.speed * game_scale self.visibility.game_scale = game_scale self.visibility.scale = self.visibility.scale * game_scale self.attack_sprite.scale_attack(game_scale) self.life.scale_life(game_scale, x_offset) self.experience.scale_experience(game_scale, x_offset) self.player_inventory.scale_inventory(game_scale, x_offset) def remove(self): self.attack_sprite.delete() for heart in self.life.life_array: heart.delete() for row in self.player_inventory.array: for slot in row: if not (slot.item is None): slot.item.cracks.delete() slot.item.rarity_img.delete() slot.item.delete() slot.delete() self.experience.bar.delete() self.experience.exp.delete() self.delete() def add_to_inventory(self, to_add) -> bool: return self.player_inventory.add(obj=to_add) def update(self, dt): if (self.facing == Direction.SOUTH): self.y = self.y - (self.speed * dt) elif (self.facing == Direction.WEST): self.x = self.x - (self.speed * dt) elif (self.facing == Direction.NORTH): self.y = self.y + (self.speed * dt) else: self.x = self.x + (self.speed * dt) if (not self.selected_footwear is None): result = self.selected_footwear.take_damage(damage=1 / 60.0) if (result): self.selected_footwear = None def change_direction(self, direc): self.facing = direc if (direc == Direction.NORTH): self.image = Player.north_standing_img elif (direc == Direction.EAST): self.image = Player.east_standing_img elif (direc == Direction.SOUTH): self.image = Player.south_standing_img else: self.image = Player.west_standing_img def start_moving(self): clock.schedule_interval(self.update, 1 / 60.0) if (self.facing == Direction.SOUTH): self.image = Player.south_animation elif (self.facing == Direction.WEST): self.image = Player.west_animation elif (self.facing == Direction.NORTH): self.image = Player.north_animation else: self.image = Player.east_animation self.is_moving = True def stop_moving(self): clock.unschedule(self.update) if (self.facing == Direction.SOUTH): self.image = Player.south_standing_img elif (self.facing == Direction.WEST): self.image = Player.west_standing_img elif (self.facing == Direction.NORTH): self.image = Player.north_standing_img else: self.image = Player.east_standing_img self.is_moving = False #Returns if dead def change_life(self, change) -> bool: self.life.change_health(amount=change, defense=self.defense) if (change < 0): if (not self.selected_chestpiece is None): result = self.selected_chestpiece.take_damage(damage=(-1 * change)) if (result): self.selected_chestpiece = None if (not self.selected_helmet is None): result = self.selected_helmet.take_damage(damage=(-1 * change)) if (result): self.selected_helmet = None if (not self.selected_leggings is None): result = self.selected_leggings.take_damage(damage=(-1 * change)) if (result): self.selected_leggings = None return self.life.life_array[0].life == 0 def increase_life_max(self): self.life.add_heart() def increase_inventory_max(self): self.player_inventory.add_slot() def add_experience(self, exp): result = self.experience.add_exp(amount=exp) for i in range(result): self.stat_boost() def stat_boost(self): clock.unschedule(self.fade_stat_text) if (self.next_up_stat == 0): self.next_up_stat = 1 self.defense = self.defense + 0.1 self.stat_boosted.text = "++Defense++" elif (self.next_up_stat == 1): self.next_up_stat = 2 self.speed = self.speed + 10 self.stat_boosted.text = "++Speed++" elif (self.next_up_stat == 2): self.next_up_stat = 3 self.increase_inventory_max() self.stat_boosted.text = "++Inventory++" else: self.next_up_stat = 0 self.increase_life_max() self.stat_boosted.text = "++Health++" self.stat_boosted.color = (55, 235, 52, 128) clock.schedule_interval(self.fade_stat_text, 1 / 60.0) def fade_stat_text(self, dt): if (self.stat_boosted.color[3] >= 2): self.stat_boosted.color = (55, 235, 52, self.stat_boosted.color[3] - 2) else: self.stat_boosted.color = (55, 235, 52, 0) clock.unschedule(self.fade_stat_text) def draw(self): Sprite.draw(self) self.life.draw() self.experience.draw() self.stat_boosted.draw() if (self.is_attacking): self.attack_sprite.update_self(player_x=self.x, player_y=self.y, direction=self.facing) self.attack_sprite.draw() def stop_attack(self, dt): self.is_attacking = False def fade(self): clock.schedule_interval(self.decrease_opacity, 1 / 60) def decrease_opacity(self, dt): if (self.opacity >= 2): self.opacity = self.opacity - 2 else: self.opacity = 0 clock.unschedule(self.decrease_opacity) def draw_inventory(self): self.player_inventory.draw() def discard_item(self): self.player_inventory.discard_item() def change_highlight(self, direc): self.player_inventory.update_highlight(direction=direc) def toggle_select_highlight(self): current_slot = self.player_inventory.get_curr_slot() slot_type = current_slot.get_item_type() if (not slot_type is None): if (slot_type == Type.Weapon): if (not self.selected_weapon is None): self.selected_weapon.toggle_select() if (self.selected_weapon is None or not (current_slot.x == self.selected_weapon.x and current_slot.y == self.selected_weapon.y)): self.selected_weapon = current_slot self.attack = self.selected_weapon.item.attack_strength_defense self.selected_weapon.toggle_select() else: self.selected_weapon = None self.attack = 0 elif (slot_type == Type.Helmet): if (not self.selected_helmet is None): self.defense = self.defense - self.selected_helmet.item.attack_strength_defense self.selected_helmet.toggle_select() if (self.selected_helmet is None or not (current_slot.x == self.selected_helmet.x and current_slot.y == self.selected_helmet.y)): self.selected_helmet = current_slot self.defense = self.defense + self.selected_helmet.item.attack_strength_defense self.selected_helmet.toggle_select() else: self.selected_helmet = None elif (slot_type == Type.Chestpiece): if (not self.selected_chestpiece is None): self.defense = self.defense - self.selected_chestpiece.item.attack_strength_defense self.selected_chestpiece.toggle_select() if (self.selected_chestpiece is None or not (current_slot.x == self.selected_chestpiece.x and current_slot.y == self.selected_chestpiece.y)): self.selected_chestpiece = current_slot self.defense = self.defense + self.selected_chestpiece.item.attack_strength_defense self.selected_chestpiece.toggle_select() else: self.selected_chestpiece = None elif (slot_type == Type.Leggings): if (not self.selected_leggings is None): self.defense = self.defense - self.selected_leggings.item.attack_strength_defense self.selected_leggings.toggle_select() if (self.selected_leggings is None or not (current_slot.x == self.selected_leggings.x and current_slot.y == self.selected_leggings.y)): self.selected_leggings = current_slot self.defense = self.defense + self.selected_leggings.item.attack_strength_defense self.selected_leggings.toggle_select() else: self.selected_leggings = None elif (slot_type == Type.Footwear): if (not self.selected_footwear is None): self.speed = self.speed - self.selected_footwear.item.attack_strength_defense self.selected_footwear.toggle_select() if (self.selected_footwear is None or not (current_slot.x == self.selected_footwear.x and current_slot.y == self.selected_footwear.y)): self.selected_footwear = current_slot self.speed = self.speed + self.selected_footwear.item.attack_strength_defense self.selected_footwear.toggle_select() else: self.selected_footwear = None elif (slot_type == Type.Torch): if (not self.selected_torch is None): self.visibility.scale = self.visibility.scale - self.selected_torch.item.attack_strength_defense self.visibility.update_coords( aX=(self.x + (20 * self.game_scale)), aY=(self.y + (20 * self.game_scale))) self.selected_torch.toggle_select() if (self.selected_torch is None or not (current_slot.x == self.selected_torch.x and current_slot.y == self.selected_torch.y)): self.selected_torch = current_slot self.visibility.scale = self.visibility.scale + self.selected_torch.item.attack_strength_defense self.visibility.update_coords( aX=(self.x + (20 * self.game_scale)), aY=(self.y + (20 * self.game_scale))) self.selected_torch.toggle_select() else: self.selected_torch = None else: current_slot.toggle_select()
class Monster(Sprite): bat_east_standing = image.load('images/BatEastStanding.png') bat_north_standing = image.load('images/BatNorthStanding.png') bat_south_standing = image.load('images/BatSouthStanding.png') bat_west_standing = image.load('images/BatWestStanding.png') bat_east_moving = image.load_animation('images/BatEast.gif', None, None) bat_north_moving = image.load_animation('images/BatNorth.gif', None, None) bat_south_moving = image.load_animation('images/BatSouth.gif', None, None) bat_west_moving = image.load_animation('images/BatWest.gif', None, None) slime_standing = image.load('images/SlimeNSStanding.png') slime_NS_moving = image.load_animation('images/SlimeNS.gif') slime_east_moving = image.load_animation('images/SlimeEast.gif') slime_west_moving = image.load_animation('images/SlimeWest.gif') skeleton_east_standing = image.load('images/SkeletonEastStanding.png') skeleton_north_standing = image.load('images/SkeletonNorthStanding.png') skeleton_south_standing = image.load('images/SkeletonSouthStanding.png') skeleton_west_standing = image.load('images/SkeletonWestStanding.png') skeletont_east_moving = image.load_animation('images/SkeletonEast.gif', None, None) skeleton_north_moving = image.load_animation('images/SkeletonNorth.gif', None, None) skeleton_south_moving = image.load_animation('images/SkeletonSouth.gif', None, None) skeleton_west_moving = image.load_animation('images/SkeletonWest.gif', None, None) skeletont_east_attack = image.load_animation('images/SkeletonAEast.gif', None, None) skeleton_north_attack = image.load_animation('images/SkeletonANorth.gif', None, None) skeleton_south_attack = image.load_animation('images/SkeletonASouth.gif', None, None) skeleton_west_attack = image.load_animation('images/SkeletonAWest.gif', None, None) def __init__(self, game_scale, backgroundX, backgroundY, this_room): super().__init__(img=Monster.bat_south_standing) self.scale = game_scale self.startX = backgroundX self.startY = backgroundY self.multiplier = 0 self.next_coord = None self.curr_room = this_room self.monster_type = None self.standing_img_east = None self.standing_img_north = None self.standing_img_south = None self.standing_img_west = None self.moving_img_east = None self.moving_img_north = None self.moving_img_south = None self.moving_img_west = None self.attacking_img_east = None self.attacking_img_north = None self.attacking_img_south = None self.attacking_img_west = None self.health = None self.speed = None self.attack = None self.sight = None self.is_moving = False self.is_transfer_moving = False self.is_attacking = False self.is_dead = False self.facing = Direction.SOUTH self.pick_random_monster() self.pick_random_location() def scale_entity(self, game_scale, x_offset): self.scale = game_scale self.startX = x_offset if (self.facing == Direction.SOUTH or self.facing == Direction.NORTH) and not (self.next_coord is None): self.next_coord = self.next_coord * game_scale elif not (self.next_coord is None): self.next_coord = (self.next_coord * game_scale) + x_offset self.x = (self.x * game_scale) + x_offset self.y = (self.y * game_scale) def pick_random_monster(self): rand_num = random.randint(0, 4) expansion_factor = 0.1448823074 phase_shift = 0.1800373888 self.multiplier = ( math.atan(expansion_factor * self.curr_room.level + phase_shift) / (math.pi / 2)) + (random.randint(-5, 5) / 100) if (rand_num <= 1): self.monster_type = 'Bat' self.standing_img_south = Monster.bat_south_standing self.moving_img_south = Monster.bat_south_moving self.standing_img_east = Monster.bat_east_standing self.moving_img_east = Monster.bat_east_moving self.standing_img_north = Monster.bat_north_standing self.moving_img_north = Monster.bat_north_moving self.standing_img_west = Monster.bat_west_standing self.moving_img_west = Monster.bat_west_moving self.attacking_img_east = Monster.bat_east_moving self.attacking_img_north = Monster.bat_north_moving self.attacking_img_south = Monster.bat_south_moving self.attacking_img_west = Monster.bat_west_moving self.health = 17.5 * self.multiplier self.speed = 240 * self.scale self.attack = 18 * self.multiplier self.sight = 14 * self.multiplier elif (rand_num <= 3): self.monster_type = 'Slime' self.standing_img_south = Monster.slime_standing self.moving_img_south = Monster.slime_NS_moving self.standing_img_east = Monster.slime_standing self.moving_img_east = Monster.slime_east_moving self.standing_img_north = Monster.slime_standing self.moving_img_north = Monster.slime_NS_moving self.standing_img_west = Monster.slime_standing self.moving_img_west = Monster.slime_west_moving self.attacking_img_east = Monster.slime_east_moving self.attacking_img_north = Monster.slime_NS_moving self.attacking_img_south = Monster.slime_NS_moving self.attacking_img_west = Monster.slime_west_moving self.health = 35 * self.multiplier self.speed = 80 * self.scale self.attack = 36 * self.multiplier self.sight = 10 * self.multiplier else: self.monster_type = 'Skeleton' self.standing_img_south = Monster.skeleton_south_standing self.moving_img_south = Monster.skeleton_south_moving self.standing_img_east = Monster.skeleton_east_standing self.moving_img_east = Monster.skeletont_east_moving self.standing_img_north = Monster.skeleton_north_standing self.moving_img_north = Monster.skeleton_north_moving self.standing_img_west = Monster.skeleton_west_standing self.moving_img_west = Monster.skeleton_west_moving self.attacking_img_east = Monster.skeletont_east_attack self.attacking_img_north = Monster.skeleton_north_attack self.attacking_img_south = Monster.skeleton_south_attack self.attacking_img_west = Monster.skeleton_west_attack self.health = 87.5 * self.multiplier self.speed = 120 * self.scale self.attack = 72 * self.multiplier self.sight = 20 * self.multiplier self.image = self.standing_img_south def pick_random_location(self): rand_x = (random.randint(0, 24) * (40 * self.scale)) + self.startX rand_y = (random.randint(0, 24) * (40 * self.scale)) + self.startY while (self.curr_room.is_entity(aX=rand_x, aY=rand_y)): rand_x = (random.randint(0, 24) * (40 * self.scale)) + self.startX rand_y = (random.randint(0, 24) * (40 * self.scale)) + self.startY self.x = rand_x self.y = rand_y def remove_self(self): self.delete() self.curr_room.entities.remove(self) def take_damage(self, dmg) -> int: self.color = (128, 0, 0) clock.schedule_once(self.revert_color, 0.25) self.health = self.health - dmg if (self.health <= 0): self.is_dead = True return (5 * (1 + self.multiplier)) else: return 0 def revert_color(self, dt): self.color = (255, 255, 255) def move_east(self, dt): if (self.is_dead): clock.unschedule(self.move_east) elif (self.x < self.next_coord): self.x = self.x + (self.speed * dt) else: self.x = self.next_coord clock.unschedule(self.move_east) self.is_moving = False self.is_transfer_moving = True def move_west(self, dt): if (self.is_dead): clock.unschedule(self.move_west) elif (self.x > self.next_coord): self.x = self.x - (self.speed * dt) else: self.x = self.next_coord clock.unschedule(self.move_west) self.is_moving = False self.is_transfer_moving = True def move_south(self, dt): if (self.is_dead): clock.unschedule(self.move_south) elif (self.y > self.next_coord): self.y = self.y - (self.speed * dt) else: self.y = self.next_coord clock.unschedule(self.move_south) self.is_moving = False self.is_transfer_moving = True def move_north(self, dt): if (self.is_dead): clock.unschedule(self.move_north) elif (self.y < self.next_coord): self.y = self.y + (self.speed * dt) else: self.y = self.next_coord clock.unschedule(self.move_north) self.is_moving = False self.is_transfer_moving = True def move_block(self, playerX, playerY): dif_x = self.x - playerX dif_y = self.y - playerY if (math.fabs(dif_x) >= math.fabs(dif_y)): if (dif_x < 0): if ((not self.is_transfer_moving) or self.facing != Direction.EAST): self.image = self.moving_img_east self.facing = Direction.EAST self.next_coord = self.x + (40 * self.scale) clock.schedule_interval(self.move_east, 1 / 60.0) else: if ((not self.is_transfer_moving) or self.facing != Direction.WEST): self.image = self.moving_img_west self.facing = Direction.WEST self.next_coord = self.x - (40 * self.scale) clock.schedule_interval(self.move_west, 1 / 60.0) else: if (dif_y < 0): if ((not self.is_transfer_moving) or self.facing != Direction.NORTH): self.image = self.moving_img_north self.facing = Direction.NORTH self.next_coord = self.y + (40 * self.scale) clock.schedule_interval(self.move_north, 1 / 60.0) else: if ((not self.is_transfer_moving) or self.facing != Direction.SOUTH): self.image = self.moving_img_south self.facing = Direction.SOUTH self.next_coord = self.y - (40 * self.scale) clock.schedule_interval(self.move_south, 1 / 60.0) def return_to_standing(self, dt): if (self.facing == Direction.EAST): self.image = self.standing_img_east elif (self.facing == Direction.WEST): self.image = self.standing_img_west elif (self.facing == Direction.NORTH): self.image = self.standing_img_north else: self.image = self.standing_img_south def set_attacking_img(self): if (self.facing == Direction.EAST): self.image = self.attacking_img_east elif (self.facing == Direction.WEST): self.image = self.attacking_img_west elif (self.facing == Direction.NORTH): self.image = self.attacking_img_north else: self.image = self.attacking_img_south def face_player(self, playerX, playerY): dif_x = self.x - playerX dif_y = self.y - playerY if (dif_x < 0): self.facing = Direction.EAST elif (dif_x > 0): self.facing = Direction.WEST elif (dif_y > 0): self.facing = Direction.SOUTH else: self.facing = Direction.NORTH def done_attacking(self, dt): self.is_attacking = False def update(self, dt, player_x, player_y) -> int: dif_x = math.fabs(self.x - player_x) / (40 * self.scale) dif_y = math.fabs(self.y - player_y) / (40 * self.scale) distance = round(dif_x, 1) + round(dif_y, 1) if ((distance <= self.sight) and (not self.is_moving) and (distance > 1)): self.is_transfer_moving = False self.is_moving = True self.move_block(playerX=player_x, playerY=player_y) if (self.is_dead): clock.unschedule(self.return_to_standing) clock.unschedule(self.done_attacking) clock.unschedule(self.revert_color) self.remove_self() return 0 elif (distance <= 1 and (not self.is_attacking)): self.is_transfer_moving = False self.return_to_standing(dt=0) self.face_player(playerX=player_x, playerY=player_y) self.is_attacking = True self.set_attacking_img() clock.schedule_once(self.return_to_standing, self.image.get_duration()) clock.schedule_once(self.done_attacking, 1) if (self.is_dead): clock.unschedule(self.return_to_standing) clock.unschedule(self.done_attacking) clock.unschedule(self.revert_color) self.remove_self() return self.attack else: if (self.is_transfer_moving): self.is_transfer_moving = False self.return_to_standing(dt=0) if (self.is_dead): clock.unschedule(self.return_to_standing) clock.unschedule(self.done_attacking) clock.unschedule(self.revert_color) self.remove_self() return 0
def next_frame(self, dt): self.index = (self.index + 1) % len(self.animation.frames) frame = self.animation.frames[self.index] if frame.delay is not None: delay = frame.delay - (self.expected_delay - dt) delay = min(max(0, delay), frame.delay) clock.schedule_once(self.next_frame, delay) self.expected_delay = delay def blit(self, x, y): self.animation.frames[self.index].image.blit(x, y) try: animation = image.load_animation(sys.argv[1]) except image.codecs.ImageDecodeException: from pyglet import media source = media.load(sys.argv[1]) source._seek(0) animation = source.get_animation() clock.tick() player = AnimationPlayer(animation) while not w.has_exit: clock.tick() w.dispatch_events() w.clear() player.blit(w.width // 2, w.height // 2)