GHESQUIERE Mathieu
11 months ago
1 changed files with 115 additions and 0 deletions
@ -0,0 +1,115 @@ |
|||||
|
import sqlite3 |
||||
|
import random |
||||
|
|
||||
|
conn = sqlite3.connect('personnages.db') |
||||
|
cursor = conn.cursor() |
||||
|
|
||||
|
# Créa de la table des perso |
||||
|
cursor.execute(''' |
||||
|
CREATE TABLE IF NOT EXISTS personnage ( |
||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT, |
||||
|
nom TEXT, |
||||
|
pdv INTEGER, |
||||
|
exp INTEGER, |
||||
|
cat TEXT, |
||||
|
inventaire TEXT |
||||
|
) |
||||
|
''') |
||||
|
|
||||
|
# Classe Personnage avec interaction a la BDD |
||||
|
class Personnage: |
||||
|
def __init__(self, nom, cat): |
||||
|
self.nom = nom |
||||
|
self.pdv = 20 |
||||
|
self.exp = 1 |
||||
|
self.cat = cat |
||||
|
self.inventaire = [] |
||||
|
|
||||
|
# Ajout du perso a la BDD |
||||
|
cursor.execute(''' |
||||
|
INSERT INTO personnage (nom, pdv, exp, cat, inventaire) |
||||
|
VALUES (?, ?, ?, ?, ?) |
||||
|
''', (self.nom, self.pdv, self.exp, self.cat, ', '.join(self.inventaire))) |
||||
|
conn.commit() |
||||
|
|
||||
|
def jet_attaque(self): |
||||
|
coefficient = 10 # Coeff de base |
||||
|
if self.cat == "guerrier": |
||||
|
coefficient = 10 |
||||
|
elif self.cat == "magicien": |
||||
|
coefficient = 10 |
||||
|
elif self.cat == "voleur": |
||||
|
coefficient = 3 |
||||
|
elif self.cat == "elfe": |
||||
|
coefficient = 8 |
||||
|
|
||||
|
return random.randint(1, 20) + self.exp * coefficient |
||||
|
|
||||
|
def jet_defense(self): |
||||
|
coefficient = 8 # Coeff de base |
||||
|
if self.cat == "guerrier": |
||||
|
coefficient = 8 |
||||
|
elif self.cat == "magicien": |
||||
|
coefficient = 7 |
||||
|
elif self.cat == "voleur": |
||||
|
coefficient = 9 |
||||
|
elif self.cat == "elfe": |
||||
|
coefficient = 10 |
||||
|
|
||||
|
return random.randint(1, 20) + self.exp * coefficient |
||||
|
|
||||
|
def change_pdv(self, ab_pdv): |
||||
|
self.pdv += ab_pdv |
||||
|
cursor.execute(''' |
||||
|
UPDATE personnage |
||||
|
SET pdv = ? |
||||
|
WHERE nom = ? |
||||
|
''', (self.pdv, self.nom)) |
||||
|
conn.commit() |
||||
|
|
||||
|
def change_exp(self, ab_exp): |
||||
|
self.exp += ab_exp |
||||
|
cursor.execute(''' |
||||
|
UPDATE personnage |
||||
|
SET exp = ? |
||||
|
WHERE nom = ? |
||||
|
''', (self.exp, self.nom)) |
||||
|
conn.commit() |
||||
|
|
||||
|
def affiche_caracteristiques(self): |
||||
|
cursor.execute(''' |
||||
|
SELECT * FROM personnage |
||||
|
WHERE nom = ? |
||||
|
''', (self.nom,)) |
||||
|
result = cursor.fetchone() |
||||
|
print(f"Nom: {result[1]}") |
||||
|
print(f"Catégorie: {result[4]}") |
||||
|
print(f"Points de vie: {result[2]}") |
||||
|
print(f"Expérience: {result[3]}") |
||||
|
|
||||
|
def affiche_inventaire(self): |
||||
|
cursor.execute(''' |
||||
|
SELECT inventaire FROM personnage |
||||
|
WHERE nom = ? |
||||
|
''', (self.nom,)) |
||||
|
result = cursor.fetchone() |
||||
|
print(f"Inventaire de {self.nom}: {result[0]}") |
||||
|
|
||||
|
# Fermeture de la connexion |
||||
|
def close_db(): |
||||
|
conn.close() |
||||
|
|
||||
|
# Creation des perso |
||||
|
personnage1 = Personnage("legolas", "elfe") |
||||
|
personnage2 = Personnage("Leonidas", "guerrier") |
||||
|
|
||||
|
# ... (votre simulation de combat) |
||||
|
|
||||
|
# Ajout de l'exp au survivant a la fin de combat |
||||
|
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.") |
||||
|
|
||||
|
# Fermeture de la BDD |
||||
|
close_db() |
Loading…
Reference in new issue