Browse Source

cette fois ça respecte les règles

master
Kalyax 2 years ago
parent
commit
496942e8d8
  1. 55
      game/core.py
  2. 128
      game/personnage.py
  3. 56
      graphics/layers.py
  4. 2
      main.py

55
game/core.py

@ -1,4 +1,4 @@
from game.personnage import Personnage, ClassType, Item, Material from game.personnage import *
from graphics.layers import Sprite, PopUp from graphics.layers import Sprite, PopUp
@ -10,54 +10,45 @@ class Game:
self.ennemy = None self.ennemy = None
def late_init(self): def late_init(self):
self.personnage = Personnage(" ", ClassType.GUERRIER, "player") self.personnage = Personnage(" ", "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", "G-Man"]
return Personnage(noms[randint(0, 3)], ClassType[classes[randint(0, 3)]], "opponant") 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, ClassType[class_type], "player") self.personnage = Personnage(nom, class_type, "PLAYER")
def attack(self, attacker, victim): def attack(self, attacker, victim):
atk_jet = attacker.jet_attaque() atk_jet = attacker.jet_attaque()
def_jet = victim.jet_defense() def_jet = victim.jet_defense()
if (atk_jet >= def_jet): if (atk_jet >= def_jet):
rand = randint(1, 8) rand = randint(1, 8)
victim.change_hp(rand + rand * ((attacker.class_type.atkP + attacker.arme.stats.atkP)//100)) victim.change_pdv(rand)
else: else:
rand = randint(1, 4) rand = randint(1, 4)
attacker.change_hp(rand + rand * ((victim.class_type.defP + victim.arme.stats.defP)//100)) attacker.change_pdv(rand)
if self.personnage.get_hp() == 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)
if self.ennemy.get_hp() == 0: if self.ennemy.get_pdv() == 0:
self.ennemy = self.rand_ennemy() self.ennemy = self.rand_ennemy()
potions = "" potions = ""
for i in range(randint(0, 3)): inv = self.personnage.inventaire
random = randint(5, 25) if len(inv) >= 6:
self.personnage.potions.append(Item(Material.POTION, random)) potions = f"- Pas de potion car inventaire plein\n"
potions += f"- Potion de {random}HP\n" else:
maxpot = 3
random = randint(0, 4) if len(inv) > 3:
arme = "" maxpot = 6-len(inv)
stat = randint(1, 4) for i in range(randint(0, maxpot)):
stats = self.personnage.arme.stats random = randint(5, 25)
if random == 0: inv.append(Material.POTION)
stats.hp += stat potions += f"- Potion de 10HP\n"
arme = f"+ {stat} HP"
elif random == 1: PopUp(1, f"Ennemi vaincu!\n{potions}")
stats.atkP += stat self.personnage.change_exp(1)
arme = f"+ {stat} ATK%"
elif random == 2:
stats.defP += stat
arme = f"+ {stat} DEF%"
elif random == 3:
stats.initiative += stat
arme = f"+ {stat} INITIATIVE"
PopUp(1, f"Ennemi vaincu!\n{potions}\n{arme}")

128
game/personnage.py

@ -4,105 +4,73 @@ from enum import Enum
from graphics.engine import Screen from graphics.engine import Screen
from graphics.layers import Sprite from graphics.layers import Sprite
class StatsSet: class Category:
#HP, ATK, DEF, INITIATIVE, XPCOEF def __init__(self, nom, __expcoef, inventaire):
def __init__(self, hp, atkP, defP, initiative, xpcoef): self.nom = nom
self.hp = hp self.pdv = __expcoef
self.atkP = atkP self.inventaire = inventaire
self.defP = defP
self.initiative = initiative
self.xpcoef = xpcoef
class ClassType(Enum): class Material:
GUERRIER = StatsSet(20, 10, 15, 5, 8) POTION = "POTION"
MAGICIEN = StatsSet(15, 15, 6, 5, 7) EPEE = "EPEE"
VOLEUR = StatsSet(9, 30, 9, 7, 9) BATON = "BATON"
ELFE = StatsSet(13, 22, 1, 5, 10) DAGUE = "DAGUE"
ARC = "ARC"
class Personnage: class Personnage:
def __init__(self, nom, class_type, place): def __init__(self, nom, cat, personnage_type):
self.nom = nom self.nom = nom
self.class_name = class_type.name self.__pdv = 20
self.class_type = class_type.value self.__exp = 1
self.cat = cat
self.__xp = 1 if cat == "Guerrier":
self.inventaire = [Material.EPEE, Material.POTION]
self.potions = [Item(Material.POTION, 10)] self.__expcoef = 10
self.arme = Item(*ClassItem[self.class_name].value) elif cat == "Magicien":
self.inventaire = [Material.BATON, Material.POTION]
self.__hp = self.class_type.hp + self.arme.stats.hp self.__expcoef = 10
elif cat == "Voleur":
Sprite(3, self.class_name, place) self.inventaire = [Material.DAGUE, Material.POTION]
self.__expcoef = 3
elif cat == "Elfe":
self.inventaire = [Material.ARC, Material.POTION]
self.__expcoef = 8
Sprite(3, cat, personnage_type)
def jet_attaque(self): def jet_attaque(self):
damage = randint(1, 20) damage = randint(1, 20)
self.change_exp(self.class_type.xpcoef * self.__xp) self.change_exp(self.__expcoef * self.__exp)
return damage + self.class_type.initiative + self.arme.stats.initiative return damage
def jet_defense(self): def jet_defense(self):
damage = randint(1, 20) damage = randint(1, 20)
self.change_exp(self.class_type.xpcoef * self.__xp) self.change_exp(self.__expcoef * self.__exp)
return damage + self.class_type.initiative + self.arme.stats.initiative return damage
def get_hp(self): def get_pdv(self):
return self.__hp return self.__pdv
def change_hp(self, nb_hp): def change_pdv(self, nb_pdv):
if self.__hp - nb_hp < 0: if self.__pdv - nb_pdv < 0:
self.__hp = 0 self.__pdv = 0
elif self.__hp - nb_hp > self.class_type.hp + self.arme.stats.hp:
self.__hp = self.class_type.hp + self.arme.stats.hp
else: else:
self.__hp -= nb_hp self.__pdv -= nb_pdv
def get_xp(self): def get_xp(self):
return self.__xp return self.__exp
def change_exp(self, nb_exp): def change_exp(self, nb_exp):
if nb_exp > 0: if nb_exp > 0:
self.__xp += nb_exp self.__exp += nb_exp
else: else:
raise ValueError("nb_exp attends un nombre positif") 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.nom}",
f"Type de classe: {self.class_name}", f"Type de classe: {self.cat}",
f"Vie: {self.__hp}", f"Vie: {self.__pdv}",
f"Expérience: {self.__xp}", f"Expérience: {self.__exp}",
"Stats (classe + inventaire):", f"XPCOEF: {self.__expcoef}"
f"- HP: {self.class_type.hp} + {self.arme.stats.hp}",
f"- ATK%: {self.class_type.atkP} + {self.arme.stats.atkP}",
f"- DEF%: {self.class_type.defP} + {self.arme.stats.defP}",
f"- INITIATIVE: {self.class_type.initiative} + {self.arme.stats.initiative}",
f"- XPCOEF: {self.class_type.xpcoef} + {self.arme.stats.xpcoef}"
) )
def reduced_stats(self): def affiche_inventaire(self):
return(f"Ennemi: {self.nom}", return ["- "+item for item in self.inventaire]
f"Type: {self.class_name}",
f"Vie: {self.__hp}"
)
class Item():
def __init__(self, material, stats, meta=None):
self.material = material
self.meta = material.value if meta == None else meta
self.stats = stats
class ItemMeta:
def __init__(self, name, description):
self.name = name
self.description = description
class Material(Enum):
POTION = ItemMeta("Potion", "TODO")
EPEE = ItemMeta("Épée", "TODO")
BATON = ItemMeta("Bâton", "TODO")
DAGUE = ItemMeta("Dague", "TODO")
ARC = ItemMeta("Arc", "TODO")
class ClassItem(Enum):
#HP, ATK, DEF, INITIATIVE, XPCOEF
GUERRIER = (Material.EPEE, StatsSet(3, 5, 2, 6, 0))
MAGICIEN = (Material.BATON, StatsSet(8, 1, 2, 3, 0))
VOLEUR = (Material.DAGUE, StatsSet(1, 9, 1, 1, 0))
ELFE = (Material.ARC, StatsSet(4, 2, 5, 3, 0))

56
graphics/layers.py

@ -27,13 +27,11 @@ class GUI(Layer):
x += len(button)+1 x += len(button)+1
stats = Screen.instance.game.personnage.affiche_caracteristiques() stats = Screen.instance.game.personnage.affiche_caracteristiques()
for i in range(4): 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])
for i in range(6):
self.put_string(stats[i+4], (self.x-2)//3, self.y-8+i, [Colors.RED2])
ennemy_stats = Screen.instance.game.ennemy.reduced_stats() ennemy_stats = Screen.instance.game.ennemy.affiche_caracteristiques()
for i in range(3): 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])
else: else:
@ -43,23 +41,10 @@ class GUI(Layer):
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
if self.__current == 0: self.put_string("Votre inventaire:", 1, self.y-8, [Colors.RED2])
self.put_string(f"ENTREE pour retourner en arrière", 1, self.y-8, [Colors.RED2]) stats = Screen.instance.game.personnage.affiche_inventaire()
elif self.__current == 1: for i in range(len(stats)):
potions = Screen.instance.game.personnage.potions self.put_string(stats[i], 1, self.y-7+i, [Colors.RED2])
self.put_string(f"Nombre total de potions: {len(potions)}", 1, self.y-8, [Colors.RED2])
if len(potions) > 0:
self.put_string(f"Rénération de la prochaine potion: {potions[0].stats}", 1, self.y-7, [Colors.RED2])
self.put_string(f"ENTEE pour utiliser", 1, self.y-5, [Colors.RED2])
elif self.__current == 2:
arme = Screen.instance.game.personnage.arme
self.put_string(f"Nom: {arme.meta.name}", 1, self.y-8, [Colors.RED2])
self.put_string(f"Stats:", 1, self.y-7, [Colors.RED2])
self.put_string(f"- HP: {arme.stats.hp}", 1, self.y-6, [Colors.RED2])
self.put_string(f"- ATK%: {arme.stats.atkP}", 1, self.y-5, [Colors.RED2])
self.put_string(f"- DEF%: {arme.stats.defP}", 1, self.y-4, [Colors.RED2])
self.put_string(f"- INITIATIVE: {arme.stats.initiative}", 1, self.y-3, [Colors.RED2])
self.put_string(f"- XPCOEF: {arme.stats.xpcoef}", 1, self.y-2, [Colors.RED2])
return self return self
@ -76,15 +61,18 @@ class GUI(Layer):
Screen.instance.game.attack(attacker, victim) Screen.instance.game.attack(attacker, victim)
elif self.__current == 1: elif self.__current == 1:
self.__inventaire = True self.__inventaire = True
self.__buttons = ["Retour", "Potion", "Arme"] self.__buttons = ["Retour", "Utiliser une potion"]
else: else:
if self.__current == 0: if self.__current == 0:
self.__inventaire = False self.__inventaire = False
self.__buttons = ["Attaquer", "Inventaire"] self.__buttons = ["Attaquer", "Inventaire"]
elif self.__current == 1 and len(Screen.instance.game.personnage.potions) > 0: elif self.__current == 1:
personnage = Screen.instance.game.personnage perso = Screen.instance.game.personnage
personnage.change_hp(-1*personnage.potions[0].stats) for item in range(len(perso.inventaire)):
del personnage.potions[0] if perso.inventaire[item] == "POTION":
perso.change_pdv(-10)
del perso.inventaire[item]
break
@ -92,7 +80,7 @@ class StartPopUp(Layer):
"""Calque du menu de lancement""" """Calque du menu de lancement"""
def __init__(self, z_index): def __init__(self, z_index):
super().__init__(z_index, "popup") super().__init__(z_index, "popup")
self.__classes = ["GUERRIER", "MAGICIEN", "VOLEUR", "ELFE"] self.__classes = ["Guerrier", "Magicien", "Voleur", "Elfe"]
self.__choosen_class = 0 self.__choosen_class = 0
self.__username = "" self.__username = ""
self.__missing_un = False self.__missing_un = False
@ -109,7 +97,7 @@ class StartPopUp(Layer):
self.put_string("Classe perso.: (Flèches)", self.x//3 + 1, self.y//6 + 4, [Colors.WHITEBG, Colors.BLACK]) self.put_string("Classe perso.: (Flèches)", self.x//3 + 1, self.y//6 + 4, [Colors.WHITEBG, Colors.BLACK])
y = 0 y = 0
for user_class in self.__classes: for user_class in self.__classes:
colors = colors = [Colors.REDBG, Colors.BLACK] if y == self.__choosen_class else [Colors.GREYBG, Colors.WHITE] colors = [Colors.REDBG, Colors.BLACK] if y == self.__choosen_class else [Colors.GREYBG, Colors.WHITE]
self.put_string(user_class, self.x//3 + 2, self.y//6 + 5 + y, colors) self.put_string(user_class, self.x//3 + 2, self.y//6 + 5 + y, colors)
y += 1 y += 1
@ -170,17 +158,15 @@ class PopUp(Layer):
Screen.instance.del_layer("popup") Screen.instance.del_layer("popup")
class Sprite(Layer): class Sprite(Layer):
def __init__(self, z_index, sprite_name, place): def __init__(self, z_index, sprite_name, personnage_type):
if place != "player" and place != "opponant": super().__init__(z_index, personnage_type)
raise ValueError("wrong sprite name") self.personnage_type = personnage_type
super().__init__(z_index, place)
self.place = place
self.sprite_name = sprite_name self.sprite_name = sprite_name
def draw(self): def draw(self):
super().draw() super().draw()
with open(f"game/sprites/{self.sprite_name}.txt", "r") as sprite: with open(f"game/sprites/{self.sprite_name}.txt", "r") as sprite:
pos = (1, 1) if self.place == "player" else (Screen.instance.x*2//3+1, 1) pos = (1, 1) if self.personnage_type == "PLAYER" else (Screen.instance.x*2//3+1, 1)
self.put_string(sprite.read(), *pos) self.put_string(sprite.read(), *pos)
def key_handler(self, key): def key_handler(self, key):

2
main.py

@ -1,4 +1,4 @@
from game.personnage import Personnage, ClassType from game.personnage import *
from game.core import Game from game.core import Game
import graphics.layers as layers import graphics.layers as layers

Loading…
Cancel
Save