Browse Source

sql attributs

master
Kalyax 2 years ago
parent
commit
bbdc12c5ba
  1. 1
      .gitignore
  2. 9
      game/core.py
  3. 83
      game/personnage.py
  4. 2
      graphics/engine.py
  5. 9
      graphics/layers.py
  6. BIN
      jeu.db
  7. 27
      main.py
  8. 18
      util/sqlite.py

1
.gitignore

@ -1 +1,2 @@
__pycache__ __pycache__
jeu.db

9
game/core.py

@ -10,16 +10,17 @@ class Game:
self.ennemy = None self.ennemy = None
def late_init(self): def late_init(self):
self.personnage = Personnage(" ", "Guerrier", "player") self.personnage = Personnage("a", "Guerrier", "player")
self.ennemy = self.rand_ennemy() self.ennemy = self.rand_ennemy()
def rand_ennemy(self): def rand_ennemy(self):
classes = ["Guerrier", "Magicien", "Voleur", "Elfe"] classes = ["Guerrier", "Magicien", "Voleur", "Elfe"]
noms = ["Bill Gates", "Dark Vador", "Gargamel", "G-Man"] noms = ["Bill Gates", "Dark Vador", "Gargamel", "Arthur"]
return Personnage(noms[randint(0, 3)], classes[randint(0, 3)], "OPPONENT") return Personnage(noms[randint(0, 3)], classes[randint(0, 3)], "OPPONENT")
def init_personnage(self, nom, class_type): def init_personnage(self, nom, class_type):
self.personnage = Personnage(nom, class_type, "PLAYER") self.personnage = Personnage(nom, class_type, "PLAYER")
self.ennemy = self.rand_ennemy()
def attack(self, attacker, victim): def attack(self, attacker, victim):
atk_jet = attacker.jet_attaque() atk_jet = attacker.jet_attaque()
@ -34,7 +35,10 @@ class Game:
if self.personnage.get_pdv() == 0: if self.personnage.get_pdv() == 0:
for i in range(50): for i in range(50):
PopUp(1, "Vous avez perdu! Relancez le jeu pour continuer...", True) PopUp(1, "Vous avez perdu! Relancez le jeu pour continuer...", True)
self.ennemy.db_erase()
self.personnage.db_erase()
if self.ennemy.get_pdv() == 0: if self.ennemy.get_pdv() == 0:
self.ennemy.db_erase()
self.ennemy = self.rand_ennemy() self.ennemy = self.rand_ennemy()
potions = "" potions = ""
@ -46,7 +50,6 @@ class Game:
if len(inv) > 3: if len(inv) > 3:
maxpot = 6-len(inv) maxpot = 6-len(inv)
for i in range(randint(0, maxpot)): for i in range(randint(0, maxpot)):
random = randint(5, 25)
inv.append(Material.POTION) inv.append(Material.POTION)
potions += f"- Potion de 10HP\n" potions += f"- Potion de 10HP\n"

83
game/personnage.py

@ -1,7 +1,7 @@
from random import randint from random import randint
from graphics.layers import Sprite from graphics.layers import Sprite
import util.sqlite as sqlite from util.sqlite import Connection
class Material: class Material:
POTION = "POTION" POTION = "POTION"
@ -10,38 +10,59 @@ class Material:
DAGUE = "DAGUE" DAGUE = "DAGUE"
ARC = "ARC" ARC = "ARC"
#TODO: inventaire mysql
class Inventaire:
def __init__(self, id, item):
self._id = id
def append(self, item):
conn = Connection()
conn.db.execute("INSERT INTO inventaire (personnage_id, nom) VALUES (?, ?);", (self._id, item))
conn.db.execute("SELECT last_insert_rowid();")
self._id = conn.db.fetchone()[0]
conn.close()
class Personnage: class Personnage:
def __init__(self, nom, cat, personnage_type): def __init__(self, nom, cat, personnage_type):
#self.nom = nom
#self.__pdv = 20
#self.__exp = 1
#self.cat = cat
expcoef = None expcoef = None
item = None
if cat == "Guerrier": if cat == "Guerrier":
#self.inventaire = [Material.EPEE, Material.POTION] item = Material.EPEE
expcoef = 10 expcoef = 10
elif cat == "Magicien": elif cat == "Magicien":
#self.inventaire = [Material.BATON, Material.POTION] item = Material.BATON
expcoef = 10 expcoef = 10
elif cat == "Voleur": elif cat == "Voleur":
#self.inventaire = [Material.DAGUE, Material.POTION] item = Material.DAGUE
expcoef = 3 expcoef = 3
elif cat == "Elfe": elif cat == "Elfe":
#self.inventaire = [Material.ARC, Material.POTION] item = Material.ARC
expcoef = 8 expcoef = 8
sqlite.db.execute( conn = Connection()
"""INSERT INTO personnages (nom, pdv, exp, cat, expcoef) VALUES (?, ?, ?, ?, ?);""", conn.db.execute(f"INSERT INTO personnages (nom, pdv, exp, cat, expcoef) VALUES (?,?,?,?,?);", (nom, 20, 1, cat, expcoef))
(nom, 20, 1, cat, expcoef) conn.commit()
) conn.db.execute("SELECT last_insert_rowid();")
sqlite.db.execute("SELECT last_insert_rowid();") self._id = conn.db.fetchone()[0]
self._id = sqlite.db.fetchone()[0] conn.close()
self.inventaire = Inventaire(self._id, item)
Sprite(3, cat, personnage_type) Sprite(3, cat, personnage_type)
def db_erase(self):
conn = Connection()
conn.db.execute("DELETE FROM personnages WHERE id=?;", (self._id,))
conn.close()
def __select_value(self, value): def __select_value(self, value):
sqlite.db.execute("SELECT ? FROM personnages WHERE id=?", (value, self._id)) conn = Connection()
return sqlite.db.fetchone() conn.db.execute("SELECT "+value+" FROM personnages WHERE id=?;", (self._id,))
value = conn.db.fetchone()
conn.close()
return value[0] if value is not None else None
def get_nom(self): def get_nom(self):
return self.__select_value("nom") return self.__select_value("nom")
@ -55,27 +76,41 @@ class Personnage:
return self.__select_value("expcoef") return self.__select_value("expcoef")
def set_nom(self, v): def set_nom(self, v):
Connection.db.execute(f"UPDATE personnages SET nom=? WHERE id={self.id}", v) conn = Connection()
conn.db.execute("UPDATE personnages SET nom=? WHERE id=?;", (v, self._id))
conn.close()
def change_pdv(self, 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}") conn = Connection()
conn.db.execute("UPDATE personnages SET pdv = CASE WHEN pdv - ? <= 0 THEN 0 ELSE pdv - ? END WHERE id=?;", (v, v, self._id))
conn.close()
def change_exp(self, v): def change_exp(self, v):
conn = Connection()
if v > 0: if v > 0:
Connection.db.execute(f"UPDATE personnages SET exp=exp+? WHERE id={self.id}", v) conn.db.execute("UPDATE personnages SET exp=exp+? WHERE id=?;", (v, self._id))
else: else:
raise ValueError("v attends un nombre positif") raise ValueError("v attends un nombre positif")
conn.close()
def set_cat(self, v): def set_cat(self, v):
Connection.db.execute(f"UPDATE personnages SET cat=? WHERE id={self.id}", v) conn = Connection()
conn.db.execute("UPDATE personnages SET cat=? WHERE id=?;", (v, self._id))
conn.close()
def set_expcoef(self, v): def set_expcoef(self, v):
Connection.db.execute(f"UPDATE personnages SET expcoef=? WHERE id={self.id}", v) conn = Connection()
conn.db.execute("UPDATE personnages SET expcoef=? WHERE id=?;", (v, self._id))
conn.close()
def jet_attaque(self): def jet_attaque(self):
damage = randint(1, 20) damage = randint(1, 20)
self.change_exp(self.get_expcoef() * self.get_exp()) self.change_exp(self.get_expcoef())
return damage return damage
def jet_defense(self): def jet_defense(self):
damage = randint(1, 20) damage = randint(1, 20)
self.change_exp(self.get_expcoef() * self.get_exp()) self.change_exp(self.get_expcoef())
return damage return damage
def affiche_caracteristiques(self): def affiche_caracteristiques(self):

2
graphics/engine.py

@ -17,7 +17,7 @@ class Screen:
self.__layers = {} self.__layers = {}
self.handlers = [] self.handlers = []
game.late_init() #game.late_init()
self.game = game self.game = game
def set_layer(self, layer): def set_layer(self, layer):

9
graphics/layers.py

@ -25,12 +25,15 @@ class GUI(Layer):
color = (Colors.REDBG, Colors.BLACK) if self.__buttons[self.__current] == button else (Colors.WHITEBG, Colors.BLACK) color = (Colors.REDBG, Colors.BLACK) if self.__buttons[self.__current] == button else (Colors.WHITEBG, Colors.BLACK)
self.put_string(button, x, self.y-10, color) self.put_string(button, x, self.y-10, color)
x += len(button)+1 x += len(button)+1
personnage = Screen.instance.game.personnage
stats = Screen.instance.game.personnage.affiche_caracteristiques() if personnage is not None:
stats = personnage.affiche_caracteristiques()
for i in range(5): for i in range(5):
self.put_string(stats[i], 1, self.y-8+i, [Colors.RED2]) self.put_string(stats[i], 1, self.y-8+i, [Colors.RED2])
ennemy_stats = Screen.instance.game.ennemy.affiche_caracteristiques() ennemy = Screen.instance.game.ennemy
if ennemy is not None:
ennemy_stats = ennemy.affiche_caracteristiques()
for i in range(5): for i in range(5):
self.put_string(ennemy_stats[i], self.x-(self.x-2)//3, self.y-8+i, [Colors.BLUE2]) self.put_string(ennemy_stats[i], self.x-(self.x-2)//3, self.y-8+i, [Colors.BLUE2])

BIN
jeu.db

Binary file not shown.

27
main.py

@ -5,22 +5,31 @@ 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 util.sqlite import Connection
from time import sleep from time import sleep
if __name__ == "__main__": if __name__ == "__main__":
sqlite.build_thread().start() conn = Connection()
sqlite.db.execute( conn.db.execute(
"""CREATE TABLE IF NOT EXISTS personnages ( """CREATE TABLE IF NOT EXISTS personnages (
id INT IDENTITY(1,1) PRIMARY KEY, id INTEGER PRIMARY KEY AUTOINCREMENT,
nom VARCHAR(20) NOT NULL, nom VARCHAR(50) NOT NULL,
pdv INT NOT NULL, pdv INTEGER NOT NULL,
exp INT NOT NULL, exp INTEGER NOT NULL,
cat VARCHAR(20) NOT NULL, cat VARCHAR(50) NOT NULL,
expcoef INT NOT NULL expcoef INTEGER NOT NULL
)""") )""")
conn.db.execute(
"""CREATE TABLE IF NOT EXISTS inventaire (
personnage_id INTEGER PRIMARY KEY,
slot INTEGER PRIMARY KEY AUTOINCREMENT,
nom VARCHAR(50) NOT NULL,
FOREIGN KEY(personnage_id) REFERENCES personnages(id)
)""")
conn.close()
#Initialise la partie graphique #Initialise la partie graphique
game = Game() game = Game()
screen = Screen(game) screen = Screen(game)

18
util/sqlite.py

@ -1,12 +1,14 @@
import sqlite3 import sqlite3
from threading import Thread
conn = None class Connection():
db = None
def __listener(): def __init__(self):
conn = sqlite3.connect("jeu.db") self._conn = sqlite3.connect("jeu.db")
db = conn.cursor() self.db = self._conn.cursor()
def build_thread(): def commit(self):
return Thread(target=__listener) self._conn.commit()
def close(self):
self.commit()
self._conn.close()

Loading…
Cancel
Save