Ejemplo n.º 1
0
def main():
    """
    Entry point
    """
    try:

        SIZE_Y = 30
        SIZE_X = 60
        screen = [[True for _ in range(SIZE_Y + 2)] for _ in range(SIZE_X + 2)]

        for _ in range(4):
            p1 = Vect(0, 0) | Vect(SIZE_X, SIZE_Y)
            p2 = Vect(0, 0) | Vect(SIZE_X, SIZE_Y)
            for pos in p1.g_bresenham_line(p2):
                screen[pos.x][pos.y] = False
                screen[pos.x + 1][pos.y] = False

        # Calculs
        center = Vect(30, 15)
        for pos in g_case_visible(screen, center, 10):
            screen[pos.x][pos.y] = 'F'

        screen[center.x][center.y] = '@'

        # affichage
        for y in range(SIZE_Y):
            for x in range(SIZE_X):
                char = screen[x][SIZE_Y - y - 1]
                if char is False:
                    print_char = "\033[0;34;41m" + ' ' + "\033[0m"
                if char is True:
                    print_char = ' '
                if char == 'F':
                    print_char = "\033[0;34;43m" + ' ' + "\033[0m"
                if char == '@':
                    print_char = "\033[0;31;45m" + ' ' + "\033[0m"
                if char == 'B':
                    print_char = "\033[0;34;40m" + 'X' + "\033[0m"
                print(print_char, end='')
            print("")
    finally:
        print("END")
Ejemplo n.º 2
0
class Player():
    """
    Classe Player :
    """

    BULLET_MAX = 10
    HP_MAX = 10
    START_MONEY = 0

    def __init__(self):
        """
        Personnage
        """
        self.pos = Vect(0, 0)
        self.direction = Vect(1, 0)
        self.distance_view = 7

        self.bullet = self.BULLET_MAX
        self.hp = self.HP_MAX
        self.level = 0
        self.money = self.START_MONEY
        self.sword_damage = 1
        self.gun_damage = 2

    def level_up(self, pos):
        """
        Le personnage gagne un level
        """
        self.level += 1
        self.pos = pos

    def g_case_visible(self, mat_collide):
        """
        retourne sur toutes les cases visibles
        par self dans mat_collide
        """
        # Nb : prend les segments depuis un cercle et non un rect
        # n'est pas OK
        border = self.pos.g_rect(Vect(self.distance_view, self.distance_view))
        for bordure_pos in border:
            for pos in self.pos.g_bresenham_line(bordure_pos):
                if self.pos.distance(pos) >= self.distance_view:
                    break
                if not Vect(0, 0) <= pos < Vect(len(mat_collide),
                                                len(mat_collide[0])):
                    break
                if not mat_collide[pos.x][pos.y]:
                    yield pos
                    break
                yield pos

    def shoot(self):
        """
        Tire une nouvelle balle
        """
        self.bullet -= 1
        return Bullet(self.pos, self.direction, self.gun_damage)

    def strike(self, mat_collide):
        """
        Donne un coup d'épée
        """
        return Sword(self.pos + self.direction, self.sword_damage)

    def add_money(self, value):
        """
        Ajoute des pièces au Player
        """
        assert value >= 0
        self.money += value
        return True

    def add_hp(self, value):
        """
        Ajoute des HP au Player
        """
        assert value >= 0
        if self.hp == self.HP_MAX:
            return False
        self.hp = min(self.hp + value, self.HP_MAX)
        return True

    def add_bullets(self, value):
        """
        Ajoute des balles au Player
        """
        assert value >= 0
        if self.bullet == self.BULLET_MAX:
            return False
        self.bullet = min(self.bullet + value, self.BULLET_MAX)
        return True

    def update(self, mat_collide, depl_vect):
        """
        Met à jour la position du personnage en fonction des evenements
        et de mat_collide
        """
        if depl_vect != Vect(0, 0):
            self.direction = depl_vect
            new_pos = self.pos + depl_vect
            # Tests de collision (Diagonales)
            if mat_collide[new_pos.x][self.pos.y]:
                # premier chemin libre en x
                if mat_collide[new_pos.x][new_pos.y]:
                    # deuxieme chemin libre en y
                    self.pos = new_pos
                else:
                    # deuxieme chemin bloque en y
                    self.pos.x = new_pos.x
            elif mat_collide[self.pos.x][new_pos.y]:
                # premier chemin libre en y
                if mat_collide[new_pos.x][new_pos.y]:
                    # deuxieme chemin libre en x
                    self.pos = new_pos
                else:
                    # deuxieme chemin bloque en x
                    self.pos.y = new_pos.y
            else:
                # Aucun chemin libre
                # Do nothind
                pass

    def render(self):
        """
        Retourne le char à afficher
        """
        return chars.C_PLAYER

    def __str__(self):
        """
        Retourne une chaine d'affichage
        """
        heal_str = ('\u2665' * int(self.hp / self.HP_MAX * 10) + ' ' *
                    (10 - int(self.hp / self.HP_MAX * 10)))
        bullet_str = ('|' * int(self.bullet) +
                      ' ' * int(self.BULLET_MAX - self.bullet))
        return ('Position : {} | HP : [' + heal_str + '] | Bullets [' +
                bullet_str + ']')