solo_fight.py

Created by danio

Created on February 13, 2026

7 KB


import random
import time
from ion import *

def printHealthBar (hp, max_hp, name, power, length = 15, fill = ''):
    percent = int(100 * (hp / float(max_hp)))
    filledLength = int(length * hp // max_hp)
    bar = fill * filledLength + '-' * (length - filledLength)
    #print('\r' + name + "\t |" + bar + "| " + str(percent) + " HP (" + power + ") \r\n")
    print(name + " (" + power + ")")
    print(" |" + bar + "| " + str(percent) + " HP")

class Power:
    def __init__(self, name, bonus_atk, bonus_heal, rarity, type):
        self.name = name
        self.bonus_atk = bonus_atk
        self.bonus_heal = bonus_heal
        self.rarity = rarity
        self.type = type

    def present(self):
        txt_atk = ""
        if(self.bonus_atk != 0):
            txt_atk = "Malus"
            if(self.bonus_atk > 0):
                txt_atk = "Bonus"
            txt_atk += " ATTAQUE " + prt(self.bonus_atk)
        
        txt_heal = ""
        if(self.bonus_heal != 0):
            txt_heal = "Malus"
            if(self.bonus_heal > 0):
                txt_heal = "Bonus"
            txt_heal += " SOIN " + prt(self.bonus_heal)
        
        if(txt_atk != ""):
            if(txt_heal != ""):
                return txt_atk + " | " + txt_heal
            return txt_atk
        if(txt_heal != ""):
            return txt_heal

class Player:
    def __init__(self, name, key_atk, key_heal, key_draw, powers, is_bot=False):
        self.name = name
        self.hp = 100
        self.atk = 20
        self.potion = 30
        self.key_atk = key_atk
        self.key_heal = key_heal
        self.key_draw = key_draw
        self.power = None
        self.all_powers = powers
        self.is_bot = is_bot
        self.strategy = random.randint(0, 2)
        self.quit = False
        self.max_hp = 200
    
    def attack(self, enemy):
        atk = random.randint(1, self.atk)
        str_atk = prt(-atk)
        if(self.power != None):
            if(self.power.bonus_atk != 0):
                atk += self.power.bonus_atk
                str_atk += prt(-self.power.bonus_atk) + " = " + prt(-atk)
                print(self.name + " a utilisé le bonus d'attaque " + self.power.name + " : " + self.power.present())
                if(self.power.bonus_heal != 0):
                    self.hp += self.power.bonus_heal
                    print("Le bonus d'attaque a modifié ses HP : " + prt(self.power.bonus_heal))
                self.power = None
        enemy.hp -= atk
        self.clip_hp()
        enemy.clip_hp()
        print(self.name + " attaque : " + str_atk)
    
    def heal(self):
        potion = random.randint(1, self.potion)
        str_heal = prt(potion)
        if(self.power != None):
            if(self.power.bonus_heal != 0):
                potion += self.power.bonus_heal
                str_heal += prt(self.power.bonus_heal) + " = " + prt(potion)
                print(self.name + " a utilisé le bonus de soin " + self.power.name + " : " + self.power.present())
                self.power = None
        self.hp += potion
        self.clip_hp()
        print(self.name + " se soigne : " + str_heal)
    
    def draw_power(self):
        power = random.choice(self.all_powers)
        nb = random.randint(1, 10)
        if(nb > power.rarity):
            self.power = power
            print(self.name + " a tiré " + power.name + " : " + power.present())
        else:
            self.power = None
            print(self.name + " n'a tiré aucun pouvoir...")
    
    def clip_hp(self):
        if(self.hp < 0):
            self.hp = 0
        elif(self.hp > self.max_hp):
            self.hp = self.max_hp

    def is_dead(self):
        return self.hp <= 0
    
    def round_header(self):
        print("\nAu tour de " + self.name + " de jouer :")
    
    def bot_play(self, enemy):
        prob = self.bot_strategy()
        max = sum(prob)
        nb = random.randint(1, max)
        if(nb <= prob[0]):
            self.attack(enemy)
        elif(nb <= prob[0]+prob[1]):
            self.heal()
        else:
            self.draw_power()

    def bot_strategy(self):
        if(self.strategy == 0):
            return [1, 1, 1]
        elif(self.strategy == 1):
            return [4, 1, 0]
        if(self.strategy == 2):
            prob = [2, 1, 1]
            if(self.power != None):
                if(self.power.type == 0):
                    prob = [1, 0, 0]
                elif(self.power.type == 1):
                    prob = [0, 1, 0]
                elif(self.power.type == 2):
                    prob = [0, 0, 1]
            else:
                if(self.hp > 90):
                    prob = [8, 1, 1]
                elif(self.hp < 20):
                    prob = [1, 8, 1]
            return prob

        

def recap(p1, p2):
    power1 = " - "
    if(p1.power != None):
        power1 = p1.power.name
    power2 = " - "
    if(p2.power != None):
        power2 = p2.power.name
    #print(f"\n{p1.name}: {p1.hp} HP ({power1}) \t\t {p2.name}: {p2.hp} HP ({power2})\n")
    print()
    printHealthBar(p1.hp, 100, p1.name, power1, fill='X')
    printHealthBar(p2.hp, 100, p2.name, power2, fill='X')
    #print('\n', '-'*100, '\n')

def is_end_game(p1, p2, rounds):
    if(p1.is_dead()):
        if(p2.is_dead()):
            print("\nLes deux joueurs n'ont \nplus de vie. Egalité...\n")
        else:
            print("\n" + p1.name + " n'a plus de vie. \nVictoire pour " + p2.name + " en " + str(rounds) + " rounds !\n")
    elif(p2.is_dead()):
        print("\n" + p2.name + " n'a plus de vie. \nVictoire pour " + p1.name + " en " + str(rounds) + " rounds !\n")
    elif(p1.quit or p2.quit):
        print("\nL'un des deux joueurs \ra abandonné le combat...\n")
    else:
        return False
    return True

def prt(nb):
    if(nb >= 0):
        return "+" + str(nb)
    return str(nb)

def play_round(self, enemy, rounds):
    if(keydown(KEY_UP)):
        self.attack(enemy)
        return continue_round(self, enemy, rounds)
    elif(keydown(KEY_DOWN)):
        self.heal()
        return continue_round(self, enemy, rounds)
    elif(keydown(KEY_RIGHT)):
        self.draw_power()
        return continue_round(self, enemy, rounds)
    return False

def continue_round(player, enemy, rounds):
    delay = 0.5 + random.random()
    time.sleep(delay)
    enemy.bot_play(player)
    recap(player, enemy)
    rounds += 1
    if(is_end_game(player, enemy, rounds)):
        return True
    player.round_header()
    return False

def main():
    pow1 = Power("Double Sword", 10, 0, 5, 0)
    pow2 = Power("Potion Magique", 0, 20, 8, 1)
    pow3 = Power("Poison au ketchup", -5, -5, 5, 2)
    pow4 = Power("Coup de tête puis Doliprane", 20, -5, 9, 0)
    pow5 = Power("Petit Ecolier", 0, 5, 0, 1)
    powers = [pow1, pow2, pow3, pow4, pow5]

    name = "Joueur"
    p1 = Player(name, 'a', 'q', 's', powers)
    name = "BOT"
    p2 = Player(name, 'p', 'm', 'l', powers, True) # True pour jouer contre un BOT
    
    rounds = 1
    OVER = False
    
    print("=== FIGHT ===\n")
    
    while not OVER:
        OVER = play_round(p1, p2, rounds)
        time.sleep(0.01)

main()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.