Browse Source

sql attributs

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

3
.gitignore

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

9
game/core.py

@ -10,16 +10,17 @@ class Game:
self.ennemy = None
def late_init(self):
self.personnage = Personnage(" ", "Guerrier", "player")
self.personnage = Personnage("a", "Guerrier", "player")
self.ennemy = self.rand_ennemy()
def rand_ennemy(self):
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")
def init_personnage(self, nom, class_type):
self.personnage = Personnage(nom, class_type, "PLAYER")
self.ennemy = self.rand_ennemy()
def attack(self, attacker, victim):
atk_jet = attacker.jet_attaque()
@ -34,7 +35,10 @@ class Game:
if self.personnage.get_pdv() == 0:
for i in range(50):
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:
self.ennemy.db_erase()
self.ennemy = self.rand_ennemy()
potions = ""
@ -46,7 +50,6 @@ class Game:
if len(inv) > 3:
maxpot = 6-len(inv)
for i in range(randint(0, maxpot)):
random = randint(5, 25)
inv.append(Material.POTION)
potions += f"- Potion de 10HP\n"

85
game/personnage.py

@ -1,7 +1,7 @@
from random import randint
from graphics.layers import Sprite
import util.sqlite as sqlite
from util.sqlite import Connection
class Material:
POTION = "POTION"
@ -10,38 +10,59 @@ class Material:
DAGUE = "DAGUE"
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:
def __init__(self, nom, cat, personnage_type):
#self.nom = nom
#self.__pdv = 20
#self.__exp = 1
#self.cat = cat
expcoef = None
item = None
if cat == "Guerrier":
#self.inventaire = [Material.EPEE, Material.POTION]
item = Material.EPEE
expcoef = 10
elif cat == "Magicien":
#self.inventaire = [Material.BATON, Material.POTION]
item = Material.BATON
expcoef = 10
elif cat == "Voleur":
#self.inventaire = [Material.DAGUE, Material.POTION]
item = Material.DAGUE
expcoef = 3
elif cat == "Elfe":
#self.inventaire = [Material.ARC, Material.POTION]
item = Material.ARC
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]
conn = Connection()
conn.db.execute(f"INSERT INTO personnages (nom, pdv, exp, cat, expcoef) VALUES (?,?,?,?,?);", (nom, 20, 1, cat, expcoef))
conn.commit()
conn.db.execute("SELECT last_insert_rowid();")
self._id = conn.db.fetchone()[0]
conn.close()
self.inventaire = Inventaire(self._id, item)
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):
sqlite.db.execute("SELECT ? FROM personnages WHERE id=?", (value, self._id))
return sqlite.db.fetchone()
conn = Connection()
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):
return self.__select_value("nom")
@ -55,27 +76,41 @@ class Personnage:
return self.__select_value("expcoef")
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):
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):
conn = Connection()
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:
raise ValueError("v attends un nombre positif")
conn.close()
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):
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):
damage = randint(1, 20)
self.change_exp(self.get_expcoef() * self.get_exp())
self.change_exp(self.get_expcoef())
return damage
def jet_defense(self):
damage = randint(1, 20)
self.change_exp(self.get_expcoef() * self.get_exp())
self.change_exp(self.get_expcoef())
return damage
def affiche_caracteristiques(self):
@ -87,4 +122,4 @@ class Personnage:
)
def affiche_inventaire(self):
return ["- "+item for item in self.inventaire]
return ["- "+item for item in self.inventaire]

2
graphics/engine.py

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

19
graphics/layers.py

@ -25,14 +25,17 @@ class GUI(Layer):
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)
x += len(button)+1
stats = Screen.instance.game.personnage.affiche_caracteristiques()
for i in range(5):
self.put_string(stats[i], 1, self.y-8+i, [Colors.RED2])
ennemy_stats = Screen.instance.game.ennemy.affiche_caracteristiques()
for i in range(5):
self.put_string(ennemy_stats[i], self.x-(self.x-2)//3, self.y-8+i, [Colors.BLUE2])
personnage = Screen.instance.game.personnage
if personnage is not None:
stats = personnage.affiche_caracteristiques()
for i in range(5):
self.put_string(stats[i], 1, self.y-8+i, [Colors.RED2])
ennemy = Screen.instance.game.ennemy
if ennemy is not None:
ennemy_stats = ennemy.affiche_caracteristiques()
for i in range(5):
self.put_string(ennemy_stats[i], self.x-(self.x-2)//3, self.y-8+i, [Colors.BLUE2])
else:
x = 1

BIN
jeu.db

Binary file not shown.

27
main.py

@ -5,22 +5,31 @@ import graphics.layers as layers
from graphics.engine import Screen
import graphics.key_listener as listener
import util.sqlite as sqlite
from util.sqlite import Connection
from time import sleep
if __name__ == "__main__":
sqlite.build_thread().start()
sqlite.db.execute(
conn = Connection()
conn.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
id INTEGER PRIMARY KEY AUTOINCREMENT,
nom VARCHAR(50) NOT NULL,
pdv INTEGER NOT NULL,
exp INTEGER NOT NULL,
cat VARCHAR(50) 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
game = Game()
screen = Screen(game)

18
util/sqlite.py

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

Loading…
Cancel
Save