Exemplo n.º 1
0
class TestGametile:
    def setup(self):
        self.t1 = GameTile(2, 2)
        self.t2 = GameTile(3, 4.5)

    def test_tile_add(self):
        t = self.t1 + self.t2

        assert t.x == 5
        assert t.y == 6.5

    def test_tile_sub(self):
        t = self.t2 - self.t1

        assert t.x == 1
        assert t.y == 2.5

    def test_tile_dist(self):
        # +1 +0.5 is a hex move
        t1 = GameTile(5, 2.5)
        t2 = GameTile(3, 1.5)

        d = t1.dist(t2)

        assert round(d, ndigits=10) == 2

    def test_tile_compare(self):
        t1_bis = GameTile(2.0, 2)

        assert self.t1 == t1_bis
        assert self.t2 != t1_bis

    def test_mouse_location(self):
        xt1, yt1 = self.t1.display_location()

        assert GameTile.get_tile_for_mouse((xt1, yt1)) == self.t1
        assert GameTile.get_tile_for_mouse((xt1 + 20, yt1 + 20)) == self.t1

    def test_raycast(self):
        ray = list(self.t1.raycast(self.t2))

        assert GameTile(2, 3) in ray
        assert GameTile(3, 3.5) in ray
        assert GameTile(3, 4.5) in ray
        assert len(ray) == 3
Exemplo n.º 2
0
class WorldInterface(Interface, CascadeElement):
    def __init__(self, father):
        CascadeElement.__init__(self)
        self.inventory_display = StatusDisplay(self)
        self.mob_list = []
        self.current_question_key = ''
        self.previous_question = ''
        self.current_question = None
        self.bg = SimpleSprite('menu.png')
        self.inventory = []
        self.cursor = SimpleSprite('icons/magnifyingglass.png')
        self.pc_position = GameTile(0, 0)
        self.pc_sprite = SimpleSprite('tiles/Fighter.png')
        self.map = WorldMap(0)
        self.subsprites = [
            self.bg, self.inventory_display, self.map, self.pc_sprite,
            self.cursor
        ]
        self.formation = [
            (-2, 4),
            (-1, 4.5),
            (0, 4),
            (1, 4.5),
            (2, 4),
        ]
        Interface.__init__(self,
                           father,
                           keys=[
                               ('(up|down)(left|right)?', self.move),
                               (K_ESCAPE, self.quit),
                           ])

    def on_return(self, defunct=None):
        self.pc_list = [pc for pc in self.pc_list if pc.health > 0]
        if not self.pc_list:
            self.erase_save()
            game_over = GameOverModal(self)
            self.desactivate()
            game_over.activate()

    def on_click(self, mouse_pos):
        self.inventory_display.on_click(mouse_pos)

    def new_game(self, slot):
        self.slot = slot
        self.party_gold = 0
        self.party_food = 400
        self.level = 1
        self.map.level = 1
        self.pc_list = [
            Creature('Fighter', is_pc=True),
            Creature('Barbarian', is_pc=True),
            Creature('Archer', is_pc=True),
            Creature('Wizard', is_pc=True),
            Creature('Enchantress', is_pc=True),
        ]
        self.pc_position = self.map.gen_random()

    def display_choices(self):
        pass

    def move(self, key):
        moves = {
            'down': 1,
            'downleft': 0,
            'downright': 2,
            'up': 4,
            'upright': 5,
            'upleft': 3
        }
        index = moves[key]
        new_position = self.pc_position.neighbours()[index]
        if self.map.board[new_position].is_wall:
            return
        self.party_food -= len(self.pc_list)
        if self.party_food < 0:
            self.erase_save()
            game_over = GameOverModal(self)
            self.desactivate()
            game_over.activate()
        self.pc_position = new_position
        self.map.board[self.pc_position].on_step(self)

    def update(self, mouse_pos):
        self.inventory_display.update(mouse_pos)
        self.pc_sprite.rect.x, self.pc_sprite.rect.y = self.pc_position.display_location(
        )
        self.map.update(self.pc_position)
        self.cursor.rect.x, self.cursor.rect.y = mouse_pos
        self.map.update(self.pc_position)
        self.display()

    def start_combat(self, mobs):
        self.save_game()
        gi = CombatInterface(self, mobs)
        gi.activate()
        self.desactivate()

    def save_game(self):
        pc_dump = [pc.dict_dump() for pc in self.pc_list]
        inventory_dump = [item.name for item in self.inventory]
        save = {
            'pcs': pc_dump,
            'gold': self.party_gold,
            'food': self.party_food,
            'level': self.level,
            'map': self.map.dict_dump(),
            'inventory_dump': inventory_dump,
            'pc_position': self.pc_position.dict_dump()
        }
        with open('save%d.json' % self.slot, 'w') as f:
            f.write(json.dumps(save))

    def quit(self, mouse_pos):
        self.save_game()
        self.done()

    def erase_save(self):
        try:
            os.unlink('save%d.json' % self.slot)
        except FileNotFoundError:
            pass

    def load_game(self, slot):
        self.slot = slot
        with open('save%d.json' % slot) as f:
            d = json.loads(f.read())
        self.party_gold = d['gold']
        self.pc_position = GameTile.from_string(d['pc_position'])
        self.level = d['level']
        self.party_food = d['food']
        self.map.level = self.level
        for key in d['inventory_dump']:
            item_class = items.ITEMS[key][0]
            item_args = items.ITEMS[key][1]
            self.inventory.append(item_class(*item_args))
        self.pc_list = [
            Creature.dict_load(pc, self.inventory) for pc in d['pcs']
        ]
        self.map.load_dict(d['map'])
        self.map.board[self.pc_position].on_step(self)

    def pay(self, amount):
        self.party_gold -= amount