POMMIER Paulin
1 year ago
1 changed files with 101 additions and 0 deletions
@ -0,0 +1,101 @@ |
|||||
|
import sqlite3 |
||||
|
import random |
||||
|
|
||||
|
class Personnage: |
||||
|
def __init__(self, nom, cat): |
||||
|
self.nom = nom |
||||
|
self.pdv = 20 |
||||
|
self.exp = 1 |
||||
|
self.cat = cat |
||||
|
self.inventaire = [] |
||||
|
|
||||
|
if cat == 'guerrier': |
||||
|
self.inventaire = ['épée', 'potion'] |
||||
|
elif cat == 'magicien': |
||||
|
self.inventaire = ['bâton', 'potion'] |
||||
|
elif cat == 'voleur': |
||||
|
self.inventaire = ['dague', 'potion'] |
||||
|
elif cat == 'elfe': |
||||
|
self.inventaire = ['arc', 'potion'] |
||||
|
|
||||
|
def jet_attaque(self): |
||||
|
de_a_20_faces = random.randint(1, 20) |
||||
|
coefficient_classe = 10 if self.cat in ['guerrier', 'magicien'] else 3 if self.cat == 'voleur' else 8 |
||||
|
return de_a_20_faces + self.exp * coefficient_classe |
||||
|
|
||||
|
def jet_defense(self): |
||||
|
de_a_20_faces = random.randint(1, 20) |
||||
|
coefficient_classe = 8 if self.cat == 'guerrier' else 7 if self.cat == 'magicien' else 9 if self.cat == 'voleur' else 10 |
||||
|
return de_a_20_faces + self.exp * coefficient_classe |
||||
|
|
||||
|
def change_pdv(self, nb_pdv): |
||||
|
self.pdv += nb_pdv |
||||
|
|
||||
|
def change_exp(self, nb_exp): |
||||
|
if nb_exp > 0: |
||||
|
self.exp += nb_exp |
||||
|
|
||||
|
def affiche_caracteristiques(self): |
||||
|
print(f"Nom: {self.nom}") |
||||
|
print(f"Catégorie: {self.cat}") |
||||
|
print(f"Points de vie: {self.pdv}") |
||||
|
print(f"Expérience: {self.exp}") |
||||
|
|
||||
|
def affiche_inventaire(self): |
||||
|
print(f"Inventaire de {self.nom}: {', '.join(self.inventaire)}") |
||||
|
|
||||
|
|
||||
|
def creer_table_personnage(): |
||||
|
conn = sqlite3.connect('personnages.db') |
||||
|
c = conn.cursor() |
||||
|
|
||||
|
c.execute('''CREATE TABLE IF NOT EXISTS personnages |
||||
|
(nom TEXT, pdv INTEGER, exp INTEGER, cat TEXT, inventaire TEXT)''') |
||||
|
|
||||
|
conn.commit() |
||||
|
conn.close() |
||||
|
|
||||
|
|
||||
|
def ajouter_personnage(personnage): |
||||
|
conn = sqlite3.connect('personnages.db') |
||||
|
c = conn.cursor() |
||||
|
|
||||
|
c.execute("INSERT INTO personnages VALUES (?, ?, ?, ?, ?)", |
||||
|
(personnage.nom, personnage.pdv, personnage.exp, personnage.cat, ', '.join(personnage.inventaire))) |
||||
|
|
||||
|
conn.commit() |
||||
|
conn.close() |
||||
|
|
||||
|
|
||||
|
creer_table_personnage() |
||||
|
|
||||
|
personnage1 = Personnage("Guerrier1", "guerrier") |
||||
|
personnage2 = Personnage("Magicien1", "magicien") |
||||
|
|
||||
|
ajouter_personnage(personnage1) |
||||
|
ajouter_personnage(personnage2) |
||||
|
|
||||
|
while personnage1.pdv > 0 and personnage2.pdv > 0: |
||||
|
attaquant = personnage1 if random.random() < 0.5 else personnage2 |
||||
|
defenseur = personnage2 if attaquant == personnage1 else personnage1 |
||||
|
|
||||
|
print(f"{attaquant.nom} attaque {defenseur.nom}!") |
||||
|
jet_attaque = attaquant.jet_attaque() |
||||
|
jet_defense = defenseur.jet_defense() |
||||
|
|
||||
|
if jet_attaque > jet_defense: |
||||
|
degats = random.randint(1, 8) |
||||
|
defenseur.change_pdv(-degats) |
||||
|
print(f"{defenseur.nom} perd {degats} points de vie.") |
||||
|
else: |
||||
|
degats = random.randint(1, 4) |
||||
|
attaquant.change_pdv(-degats) |
||||
|
print(f"{attaquant.nom} perd {degats} points de vie.") |
||||
|
|
||||
|
personnage1.affiche_caracteristiques() |
||||
|
personnage2.affiche_caracteristiques() |
||||
|
print() |
||||
|
|
||||
|
gagnant = personnage1 if personnage1.pdv > 0 else personnage2 |
||||
|
gagnant.change_exp(1) |
||||
|
print(f"{gagnant.nom} a gagné le combat et reçoit 1 point d'expérience!") |
Loading…
Reference in new issue