Browse Source

connection redo

master
Kalyax 2 years ago
parent
commit
b87ac94d34
  1. 97
      game/personnage.py
  2. BIN
      jeu.db
  3. 13
      main.py
  4. 12
      util/sqlite.py

97
game/personnage.py

@ -1,11 +1,7 @@
from random import randint from random import randint
from graphics.layers import Sprite from graphics.layers import Sprite
class Category: import util.sqlite as sqlite
def __init__(self, nom, __expcoef, inventaire):
self.nom = nom
self.pdv = __expcoef
self.inventaire = inventaire
class Material: class Material:
POTION = "POTION" POTION = "POTION"
@ -16,57 +12,78 @@ class Material:
class Personnage: class Personnage:
def __init__(self, nom, cat, personnage_type): def __init__(self, nom, cat, personnage_type):
self.nom = nom #self.nom = nom
self.__pdv = 20 #self.__pdv = 20
self.__exp = 1 #self.__exp = 1
self.cat = cat #self.cat = cat
expcoef = None
if cat == "Guerrier": if cat == "Guerrier":
self.inventaire = [Material.EPEE, Material.POTION] #self.inventaire = [Material.EPEE, Material.POTION]
self.__expcoef = 10 expcoef = 10
elif cat == "Magicien": elif cat == "Magicien":
self.inventaire = [Material.BATON, Material.POTION] #self.inventaire = [Material.BATON, Material.POTION]
self.__expcoef = 10 expcoef = 10
elif cat == "Voleur": elif cat == "Voleur":
self.inventaire = [Material.DAGUE, Material.POTION] #self.inventaire = [Material.DAGUE, Material.POTION]
self.__expcoef = 3 expcoef = 3
elif cat == "Elfe": elif cat == "Elfe":
self.inventaire = [Material.ARC, Material.POTION] #self.inventaire = [Material.ARC, Material.POTION]
self.__expcoef = 8 expcoef = 8
sqlite.db.execute(
"""INSERT INTO personnages (nom, pdv, exp, cat, expcoef) VALUES (?, ?, ?, ?, ?);""",
(nom, 20, 1, cat, expcoef)
)
sqlite.db.execute("SELECT last_insert_rowid();")
self._id = sqlite.db.fetchone()[0]
Sprite(3, cat, personnage_type) Sprite(3, cat, personnage_type)
def __select_value(self, value):
sqlite.db.execute("SELECT ? FROM personnages WHERE id=?", (value, self._id))
return sqlite.db.fetchone()
def get_nom(self):
return self.__select_value("nom")
def get_pdv(self):
return self.__select_value("pdv")
def get_exp(self):
return self.__select_value("exp")
def get_cat(self):
return self.__select_value("cat")
def get_expcoef(self):
return self.__select_value("expcoef")
def set_nom(self, v):
Connection.db.execute(f"UPDATE personnages SET nom=? WHERE id={self.id}", v)
def change_pdv(self, v):
Connection.db.execute(f"UPDATE personnages SET pdv = CASE WHEN pdv - {v} < 0 THEN 0 ELSE {v} END WHERE id={self.id}")
def change_exp(self, v):
if v > 0:
Connection.db.execute(f"UPDATE personnages SET exp=exp+? WHERE id={self.id}", v)
else:
raise ValueError("v attends un nombre positif")
def set_cat(self, v):
Connection.db.execute(f"UPDATE personnages SET cat=? WHERE id={self.id}", v)
def set_expcoef(self, v):
Connection.db.execute(f"UPDATE personnages SET expcoef=? WHERE id={self.id}", v)
def jet_attaque(self): def jet_attaque(self):
damage = randint(1, 20) damage = randint(1, 20)
self.change_exp(self.__expcoef * self.__exp) self.change_exp(self.get_expcoef() * self.get_exp())
return damage return damage
def jet_defense(self): def jet_defense(self):
damage = randint(1, 20) damage = randint(1, 20)
self.change_exp(self.__expcoef * self.__exp) self.change_exp(self.get_expcoef() * self.get_exp())
return damage return damage
def get_pdv(self):
return self.__pdv
def change_pdv(self, nb_pdv):
if self.__pdv - nb_pdv < 0:
self.__pdv = 0
else:
self.__pdv -= nb_pdv
def get_xp(self):
return self.__exp
def change_exp(self, nb_exp):
if nb_exp > 0:
self.__exp += nb_exp
else:
raise ValueError("nb_exp attends un nombre positif")
def affiche_caracteristiques(self): def affiche_caracteristiques(self):
return (f"Nom: {self.nom}", return (f"Nom: {self.get_nom()}",
f"Type de classe: {self.cat}", f"Type de classe: {self.get_cat()}",
f"Vie: {self.__pdv}", f"Vie: {self.get_pdv()}",
f"Expérience: {self.__exp}", f"Expérience: {self.get_exp()}",
f"XPCOEF: {self.__expcoef}" f"XPCOEF: {self.get_expcoef()}"
) )
def affiche_inventaire(self): def affiche_inventaire(self):

BIN
jeu.db

Binary file not shown.

13
main.py

@ -5,9 +5,22 @@ import graphics.layers as layers
from graphics.engine import Screen from graphics.engine import Screen
import graphics.key_listener as listener import graphics.key_listener as listener
import util.sqlite as sqlite
from time import sleep from time import sleep
if __name__ == "__main__": if __name__ == "__main__":
sqlite.build_thread().start()
sqlite.db.execute(
"""CREATE TABLE IF NOT EXISTS personnages (
id INT IDENTITY(1,1) PRIMARY KEY,
nom VARCHAR(20) NOT NULL,
pdv INT NOT NULL,
exp INT NOT NULL,
cat VARCHAR(20) NOT NULL,
expcoef INT NOT NULL
)""")
#Initialise la partie graphique #Initialise la partie graphique
game = Game() game = Game()
screen = Screen(game) screen = Screen(game)

12
util/sqlite.py

@ -0,0 +1,12 @@
import sqlite3
from threading import Thread
conn = None
db = None
def __listener():
conn = sqlite3.connect("jeu.db")
db = conn.cursor()
def build_thread():
return Thread(target=__listener)
Loading…
Cancel
Save