def generate_chunk(self, x, y, game_map): size = game_map.chunk_size assert x % size == 0 assert y % size == 0 game_map.chunks.add((x, y)) height_map, max_noise, min_noise = make_noise_map(x, y, size) for (j, k) in height_map: height_map[(j, k)] = (height_map[(j, k)] - min_noise) / ( max_noise - min_noise) terrain = self.world.create_entity(name='Terrain') self.world.add_component(terrain, Position(j, k)) self.world.add_component(terrain, Renderable('.', color='dark yellow', layer=Layers.MAP)) self.world.add_component(terrain, Physics()) if height_map[(j, k)] < 0.3: water = self.world.create_entity(name='Water') self.world.add_component(water, Position(j, k)) self.world.add_component(water, Renderable('~', color='blue',layer=Layers.MAP)) self.world.add_component(water, Physics()) elif height_map[(j, k)] < 0.7: if randint(0, 100) > 90: tree = self.world.create_entity(name='Tree') self.world.add_component(tree, Position(j, k)) self.world.add_component(tree, Physics(collidable=True)) self.world.add_component(tree, Renderable('*', color='green',layer=Layers.MAP)) elif height_map[(j, k)] < 1: mountain = self.world.create_entity(name='Mountain') self.world.add_component(mountain, Position(j, k)) self.world.add_component(mountain, Physics(collidable=True)) self.world.add_component(mountain, Renderable('^', color='grey',layer=Layers.MAP))
def test_position_class_instantiates_with_data(): simple_fixture() with freeze_time(dt(2017, 3, 7)): vcn = Position("VCN.TO", data=Data()) assert vcn is not None assert vcn.ticker_name == "VCN.TO" assert "total_returns" in vcn.__repr__() assert "total_returns" in str(vcn) simple_fixture_teardown()
def __init__(self, accounts=None, from_day=None, tickers=None, data=None): self._data = data self.accounts = determine_accounts(accounts) if not tickers: tickers = Tickers(from_day, data=self._data) self.ticker_names = tickers.ticker_names self.positions = { name: Position( name, accounts=self.accounts, from_day=from_day, ticker=tickers.tickers[name], data=self._data, ) for name in self.ticker_names } if len(self.ticker_names) > 0: self.units = self._collect_feature("units") self.costs = self._collect_feature("cost") self.costs_per_unit = self._collect_feature("cost_per_unit") self.current_prices = self._collect_feature("current_price") self.market_values = self._collect_feature("market_value") self.open_profits = self._collect_feature("open_profit") self.distributions = self._collect_feature("distributions") self.distribution_returns = self._collect_feature( "distribution_returns") self.appreciation_returns = self._collect_feature( "appreciation_returns") self.total_returns = self._collect_feature("total_returns")
def create_boss(self): def en_collision(state, e1, e2): if e2.contains_component(Player): combat = e1.get_component(InitiateCombat) state.combat_launch_entity = e1 combat.on_interact(state) self.combat_mechanic.enemies[1] = copy.copy( e1.get_component(InitiateCombat).combat_entities[0]) self.combat_mechanic.enemies[1].get_component( Stats).reinit_HP() return Entity('Boss').add_component( Combat(copy.deepcopy(self.load_combat_images.obtain('Boss'))), Stats(10, (4, 8, 3, 1)), Position(None, None, None), AlignedBox(-4.5, -4.5, 4.5, 4.5), Collider(en_collision), GridMovement(C.PLAYER_WALK_RELOAD), Orientable(C.DIRECTION_E, C.PLAYER_WALK_RELOAD), Sprite({ tuple(self.load_images.obtain_entity('Boss', 1)): lambda e: e.get_component(Orientable).orientation == C. DIRECTION_N, tuple(self.load_images.obtain_entity('Boss', 1)): lambda e: e.get_component(Orientable ).orientation == C.DIRECTION_O, tuple(self.load_images.obtain_entity('Boss', 1)): lambda e: e.get_component(Orientable ).orientation == C.DIRECTION_S, tuple(self.load_images.obtain_entity('Boss', 1)): lambda e: True }))
def drop_item(self, entity, item): self.world.add_component(item, Position()) item_pos = self.world.component_for_entity(item, Position) entity_pos = self.world.component_for_entity(entity, Position) item_pos.x = entity_pos.x item_pos.y = entity_pos.y self.remove_item(entity, item)
def create_devil(self): sprite_img = self.load_combat_images.obtain('Devil') sprite_img.width = 48 sprite_img.height = 48 return Entity('Devil').add_component( Combat(copy.deepcopy(self.load_combat_images.obtain('Devil'))), Stats(20, (0.2, 0, 2, 1)), Position(None, None, None), AlignedBox(-0.5, -0.5, 0.5, 0.5), GridMovement(C.PLAYER_WALK_RELOAD), Orientable(C.DIRECTION_E, C.PLAYER_WALK_RELOAD), Sprite({sprite_img: lambda e: True}))
def create_npc_dialogue(self): return Entity().add_component( Position(None, None, None), AlignedBox(-0.5, -0.5, 0.5, 0.5), GridMovement(C.PLAYER_WALK_RELOAD), Orientable(C.DIRECTION_O, C.PLAYER_WALK_RELOAD), Sprite({ tuple(self.load_images.obtain_entity('NPC Intro', 3)): lambda e: e.get_component(Orientable ).orientation == C.DIRECTION_N, tuple(self.load_images.obtain_entity('NPC Intro', 1)): lambda e: e.get_component(Orientable ).orientation == C.DIRECTION_O, tuple(self.load_images.obtain_entity('NPC Intro', 0)): lambda e: e.get_component(Orientable).orientation == C. DIRECTION_S, tuple(self.load_images.obtain_entity('NPC Intro', 2)): lambda e: True }))
def create_player(self): return Entity().add_component( Stats(5), Inventory(), Controllable(), GridMovement(C.PLAYER_WALK_RELOAD), Orientable(C.DIRECTION_O, C.PLAYER_WALK_RELOAD), Position(None, None, None), AlignedBox(-0.5, -0.5, 0.5, 0.5), Sprite({ tuple(self.load_images.obtain_entity('Player', 3)): lambda e: e.get_component(Orientable ).orientation == C.DIRECTION_N, tuple(self.load_images.obtain_entity('Player', 1)): lambda e: e.get_component(Orientable).orientation == C. DIRECTION_O, tuple(self.load_images.obtain_entity('Player', 0)): lambda e: e.get_component(Orientable ).orientation == C.DIRECTION_S, tuple(self.load_images.obtain_entity('Player', 2)): lambda e: True }), Player())
def create_wolf(self): return Entity('Wolf').add_component( Combat(copy.deepcopy(self.load_combat_images.obtain('Wolf'))), Stats(30, (0.2, 0, 3, 1)), Position(None, None, None), AlignedBox(-0.5, -0.5, 0.5, 0.5), GridMovement(C.PLAYER_WALK_RELOAD), Orientable(C.DIRECTION_E, C.PLAYER_WALK_RELOAD), Sprite({ tuple(self.load_images.obtain_entity('Wolf', 3)): lambda e: e.get_component(Orientable).orientation == C. DIRECTION_N, tuple(self.load_images.obtain_entity('Wolf', 1)): lambda e: e.get_component(Orientable ).orientation == C.DIRECTION_O, tuple(self.load_images.obtain_entity('Wolf', 0)): lambda e: e.get_component(Orientable ).orientation == C.DIRECTION_S, tuple(self.load_images.obtain_entity('Wolf', 2)): lambda e: True }))
def get_game_variables(): fighter_component = Fighter(hp=100, defense=1, power=2) inventory_component = Inventory(26) level_component = Level() equipment_component = Equipment() position_component = Position(0, 0) player = Entity('@', tcod.white, 'Player', blocks=True, position=position_component, render_order=RenderOrder.ACTOR, fighter=fighter_component, inventory=inventory_component, level=level_component, equipment=equipment_component) entities = [player] equippable_component = Equippable(EquipmentSlots.MAIN_HAND, power_bonus=2) dagger = Entity('-', tcod.sky, 'Dagger', equippable=equippable_component, position=position_component) player.inventory.add_item(dagger) player.equipment.toggle_equip(dagger) game_map = GameMap(c.MAP_WIDTH, c.MAP_HEIGHT) game_map.make_map(c.MAX_ROOMS, c.ROOM_MIN_SIZE, c.ROOM_MAX_SIZE, c.MAP_WIDTH, c.MAP_HEIGHT, player, entities) message_log = MessageLog(c.MESSAGE_X, c.MESSAGE_WIDTH, c.MESSAGE_HEIGHT) game_state = GameStates.PLAYERS_TURN return player, entities, game_map, message_log, game_state
def test_values(): simple_fixture() with freeze_time(dt(2017, 3, 7)): vcn = Position("VCN.TO", data=Data()) vcn.calc_weight(vcn.values["market_value"]) assert vcn.units("2017-03-01") is None assert vcn.units("2017-03-02") == 0 assert vcn.units("2017-03-03") == 100 assert vcn.units("2017-03-04") == 100 assert vcn.units("2017-03-08") is None assert vcn.cost("2017-03-01") is None assert vcn.cost("2017-03-02") == 0 assert vcn.cost("2017-03-03") == 3010.35 assert vcn.cost("2017-03-04") == 3010.35 assert vcn.cost("2017-03-08") is None assert vcn.cost_per_unit("2017-03-01") is None assert math.isnan(vcn.cost_per_unit("2017-03-02")) assert vcn.cost_per_unit("2017-03-03") == 30.1035 assert vcn.cost_per_unit("2017-03-04") == 30.1035 assert vcn.cost_per_unit("2017-03-08") is None assert vcn.current_price("2017-03-01") is None assert vcn.current_price("2017-03-02") == 30.00 assert vcn.current_price("2017-03-03") == 30.10 assert vcn.current_price("2017-03-06") == 29.85 assert vcn.current_price("2017-03-08") is None assert vcn.market_value("2017-03-01") is None assert vcn.market_value("2017-03-02") == 0 assert vcn.market_value("2017-03-03") == 3010.0 assert vcn.market_value("2017-03-06") == 2985.0 assert vcn.market_value("2017-03-08") is None assert vcn.open_profit("2017-03-01") is None assert vcn.open_profit("2017-03-02") == 0 assert vcn.open_profit("2017-03-03") == approx(-0.35) assert vcn.open_profit("2017-03-06") == approx(-25.35) assert vcn.open_profit("2017-03-08") is None assert vcn.distributions("2017-03-01") is None assert vcn.distributions("2017-03-02") == 0 assert vcn.distributions("2017-03-03") == 10.1 assert vcn.distributions("2017-03-06") == 20.0 assert vcn.distributions("2017-03-08") is None assert vcn.distribution_returns("2017-03-01") is None assert math.isnan(vcn.distribution_returns("2017-03-02")) assert vcn.distribution_returns("2017-03-03") == 10.1 / 3010.35 assert vcn.distribution_returns("2017-03-06") == 20.0 / 3010.35 assert vcn.distribution_returns("2017-03-08") is None assert vcn.appreciation_returns("2017-03-01") is None assert math.isnan(vcn.appreciation_returns("2017-03-02")) assert vcn.appreciation_returns("2017-03-03") == (3010.0 - 3010.35) / 3010.35 assert vcn.appreciation_returns("2017-03-06") == (2985.0 - 3010.35) / 3010.35 assert vcn.appreciation_returns("2017-03-08") is None assert vcn.total_returns("2017-03-01") is None assert vcn.total_returns("2017-03-02") == 0 assert vcn.total_returns("2017-03-03") == approx( (3010.0 + 10.1 - 3010.35) / 3010.35) assert vcn.total_returns("2017-03-06") == approx( (2985.0 + 20.0 - 3010.35) / 3010.35) assert vcn.total_returns("2017-03-08") is None assert vcn.weight("2017-03-01") is None assert vcn.weight("2017-03-02") == 0 assert vcn.weight("2017-03-06") == 1 simple_fixture_teardown()
def test_position_class_instantiates(): vcn = Position("VCN.TO", data=Data()) assert vcn is not None assert vcn.ticker_name == "VCN.TO" assert "Empty" in vcn.__repr__() assert "Empty" in str(vcn)
def play_game(): world = World() player = world.create_entity(Renderable(char='@', layer=Layers.ENTITIES), Position(x=10, y=10), Action(cost=1), Camera(width=20, height=20), Damager(attack_power=100), Physics(collidable=True), Inventory(26), Health(), Joystick(), Condition(), name='Player') world.create_entity(GameMap(player=player)) world.create_entity(Item(coast=5, weight=4), Position(x=1, y=10), Renderable(char='-', color='blue', layer=Layers.ITEMS), Physics(), name='Dagger') world.create_entity(Renderable(char='s', color='red', layer=Layers.ENTITIES), Position(x=19, y=10), Action(cost=3), Physics(collidable=True), Damager(), Health(), Condition(), Ai(NPC()), name='Worm') world.create_entity(Renderable(char='g', color='green', layer=Layers.ENTITIES), Position(x=2, y=10), Action(cost=2), Physics(collidable=True), Damager(), Health(), Condition(), Ai(NPC()), name='Goblin') processors = [ InputProcessor, MapProcessor, ActionProcessor, PhysicProcessor, ConditionProcessor, EventSystem, CameraProcessor, RenderProcessor ] for prior, processor in enumerate(processors): world.add_processor(processor(), priority=prior) world.get_processor(MapProcessor).get_current_map() world.get_processor(MapProcessor).process() world.get_processor(CameraProcessor).get_camera() world.get_processor(CameraProcessor).process() world.get_processor(EventSystem).process() world.get_processor(RenderProcessor).process() blt.refresh() while True: world.process()
def make_map(self, max_rooms: int, room_min_size: int, room_max_size: int, map_width: int, map_height: int, player, entities): rooms: List[Rect] = [] num_rooms: int = 0 item_components = get_item_classes() center_of_last_room_x: int center_of_last_room_y: int for r in range(max_rooms): w: int = randint(room_min_size, room_max_size) h: int = randint(room_min_size, room_max_size) x: int = randint(0, map_width - w - 1) y: int = randint(0, map_height - h - 1) new_room: Rect = Rect(x, y, w, h) # for-else loop for other_room in rooms: if new_room.intersect(other_room): break else: # this means there are no intersections, so this room is valid # "paint" it to the map's tiles self.create_room(new_room) # center coordinates of new room, will be useful later (new_x, new_y) = new_room.center() center_of_last_room_x = new_x center_of_last_room_y = new_y if num_rooms == 0: # this is the first room, where the player starts at player.pos.x = new_x player.pos.y = new_y else: # all rooms after the first: # connect it to the previous room with a tunnel # center coordinates of previous room (prev_x, prev_y) = rooms[num_rooms - 1].center() # flip a coin (random number that is either 0 or 1) if randint(0, 1) == 1: # first move horizontally, then vertically self.create_h_tunnel(prev_x, new_x, prev_y) self.create_v_tunnel(prev_y, new_y, new_x) else: # first move vertically, then horizontally self.create_v_tunnel(prev_y, new_y, prev_x) self.create_h_tunnel(prev_x, new_x, new_y) self.place_entities(new_room, entities, item_components) # finally, append the new room to the list rooms.append(new_room) num_rooms += 1 stairs_component = Stairs(self.dungeon_level + 1) pos_comp = Position(center_of_last_room_x, center_of_last_room_y) down_stairs = Entity('>', tcod.white, 'Stairs', position=pos_comp, render_order=RenderOrder.STAIRS, stairs=stairs_component) entities.append(down_stairs)
def place_entities(self, room: Rect, entities, item_components): # Get a random number of monsters max_monsters_per_room = from_dungeon_level([[2, 1], [3, 4], [5, 6]], self.dungeon_level) max_items_per_room = from_dungeon_level([[1, 1], [2, 4]], self.dungeon_level) number_of_monsters = randint(0, max_monsters_per_room) number_of_items = randint(0, max_items_per_room) monster_chances = { 'orc': 80, 'troll': from_dungeon_level([[15, 3], [30, 5], [60, 7]], self.dungeon_level) } item_chances = { 'healing_potion': 35, 'sword': from_dungeon_level([[5, 4]], self.dungeon_level), 'shield': from_dungeon_level([[15, 8]], self.dungeon_level), 'lightning_scroll': from_dungeon_level([[25, 4]], self.dungeon_level), 'fireball_scroll': from_dungeon_level([[25, 6]], self.dungeon_level), 'confusion_scroll': from_dungeon_level([[10, 2]], self.dungeon_level) } for i in range(number_of_monsters): # Choose a random location in the room x: int = randint(room.x1 + 1, room.x2 - 1) y: int = randint(room.y1 + 1, room.y2 - 1) pos_comp = Position(x, y) if not any([ entity for entity in entities if entity.pos.x == x and entity.pos.y == y ]): monster_choice = random_choice_from_dict(monster_chances) if monster_choice == 'orc': fighter_component = Fighter(hp=20, defense=0, power=4, xp=35) ai_component = BasicMonster() monster = Entity('o', tcod.desaturated_green, 'Orc', blocks=True, position=pos_comp, render_order=RenderOrder.ACTOR, fighter=fighter_component, ai=ai_component) elif monster_choice == 'troll': fighter_component = Fighter(hp=30, defense=2, power=8, xp=100) ai_component = BasicMonster() monster = Entity('T', tcod.darker_green, 'Troll', blocks=True, position=pos_comp, render_order=RenderOrder.ACTOR, fighter=fighter_component, ai=ai_component) entities.append(monster) for i in range(number_of_items): x = randint(room.x1 + 1, room.x2 - 1) y = randint(room.y1 + 1, room.y2 - 1) pos_comp = Position(x, y) if not any([ entity for entity in entities if entity.pos.x == x and entity.pos.y == y ]): item_choice = random_choice_from_dict(item_chances) if item_choice == 'healing_potion': item_component = item_components['HealingPotion'](1) item = Entity('!', tcod.violet, 'Healing Potion', position=pos_comp, render_order=RenderOrder.ITEM, item=item_component) elif item_choice == 'sword': equippable_component = Equippable(EquipmentSlots.MAIN_HAND, power_bonus=3) item = Entity('/', tcod.sky, 'Sword', position=pos_comp, equippable=equippable_component) elif item_choice == 'shield': equippable_component = Equippable(EquipmentSlots.OFF_HAND, defense_bonus=1) item = Entity('[', tcod.darker_orange, 'Shield', position=pos_comp, equippable=equippable_component) elif item_choice == 'fireball_scroll': item_component = item_components['FireballScroll'](1) item = Entity('#', tcod.red, 'Fireball Scroll', position=pos_comp, render_order=RenderOrder.ITEM, item=item_component) elif item_choice == 'confusion_scroll': item_component = item_components['ConfuseScroll'](1) item = Entity('#', tcod.light_pink, 'Confusion Scroll', position=pos_comp, render_order=RenderOrder.ITEM, item=item_component) elif item_choice == 'lightning_scroll': item_component = item_components['LightningScroll'](1) item = Entity('#', tcod.yellow, 'Lightning Scroll', position=pos_comp, render_order=RenderOrder.ITEM, item=item_component) entities.append(item)