Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import random
- import numpy as np
- ########################################################################################################
- #PARAMETRES GENERAUX
- i = 150
- j = 150
- Munitions_max = 20 # Quantité maximale que peut avoir un humain dans son sac
- Médicaments_max = 10 # Quantité maximale que peut avoir un humain dans son sac
- Nourriture_max = 20 # Quantité maximale que peut avoir un humain dans son sac
- Odorat_Z = 10 # Zone d'odorat des zombies
- Odorat_H = 10 # Zone d'alerte des humains
- ########################################################################################################
- #HUMAIN
- class Humain:
- """
- Classe définissant un humain, qui peux se déplacer, fuir les zombies
- """
- num_humain=0
- def __init__(self):
- self.nom = Humain.num_humain
- Humain.num_humain += 1
- self.compteur_temps=0
- self.pdv=random.randint(40,100)
- self.faim=100
- self.compteur_faim=0
- self.sac={"Munitions":4,"Médicaments":0,"Nourriture":0} # Mun max : 30
- def __str__(self):
- return "Humain " + str(self.nom)
- def __repr__(self):
- return self.__str__()
- def sentir_zombie(self,carte):
- """
- Rend les coordonnées du zombie le plus proche
- """
- i_humain,j_humain = np.where(carte == self) # Donne l'emplacement de l'humain
- i_humain,j_humain = int(i_humain),int(j_humain)
- self.place_matrice = i_humain,j_humain
- liste_zombie = where_classe(Zombie,carte)
- distance = 1000 # Distance supposée du zombie le plus proche
- i_zombie = None
- j_zombie = None
- self.Zombie_le_plus_proche = (i_zombie, j_zombie)
- self.liste_zombie = []
- for e in range(len(liste_zombie)):
- i_zombieTempo, j_zombieTempo = liste_zombie[e]
- if i_zombieTempo in range((i_humain-Odorat_H),(i_humain+Odorat_H+1))\
- and j_zombieTempo in range((j_humain-Odorat_H),(j_humain+Odorat_H+1)): # L'humain est dans un voisinage +/- 10 du zombie
- self.liste_zombie +=[(i_zombieTempo,j_zombieTempo)]
- if min(math.sqrt((i_zombieTempo - i_humain)**2+(j_zombieTempo - j_humain)**2),distance)\
- ==(math.sqrt((i_zombieTempo-i_humain)**2+(j_zombieTempo-j_humain)**2)): # Compare la distance H-Z la plus petite trouvée
- i_zombie = i_zombieTempo # Garde en mémoire les coordonnées du zombie le plus proche
- j_zombie = j_zombieTempo
- distance = (math.sqrt((i_zombie - i_humain)**2 + (j_zombie - j_humain)**2))
- self.Zombie_le_plus_proche = (i_zombieTempo,j_zombieTempo)
- def Pdv(self,carte):
- """
- Etat de la vie de l'humain
- """
- i_humain, j_humain = self.place_matrice
- if self.faim == 0:
- if self.compteur_temps < 50:
- self.compteur_temps += 1
- else:
- self.pdv -= 1
- self.compteur_temps = 0
- if self.pdv<=80 and self.sac["Médicaments"]>1:
- self.sac["Médicaments"]-=1
- self.pdv+=20
- if self.pdv==0:
- carte[i_humain,j_humain]=Zombie()
- def Faim(self):
- """
- Etat de la faim de l'humain
- """
- if self.sac["Nourriture"]>=1 and self.faim<=80:
- self.sac["Nourriture"]-=1
- self.faim+=20
- if self.faim==0 :
- if self.compteur_faim < 20:
- self.compteur_faim += 1
- else:
- self.pdv -=1
- self.compteur_faim=0
- def shoot(self,carte):
- if len(self.liste_zombie)<=2 and len(self.liste_zombie)>0 :
- if self.sac["Munitions"]>=1:
- x=random.randint(0,1)
- print(x)
- if x==1:
- carte[self.Zombie_le_plus_proche]=Vide()
- self.sac["Munitions"]=self.sac["Munitions"]-1
- print(self.sac)
- print(carte[self.Zombie_le_plus_proche])
- else:
- self.sac["Munitions"]=self.sac["Munitions"]-1
- print(self.sac)
- print(carte[self.Zombie_le_plus_proche])
- def ravitaillement(self,carte):
- """
- Permet à l'humain de se ravitailler grâce à une porte si il y en a une près de lui
- """
- i_humain, j_humain = self.place_matrice
- for x in range(-1,2): #Vérifie qu'il y a une porte autour de l'humain
- if isinstance(carte[i_humain+x, j_humain+x], Batiment) and carte[i_humain+x, j_humain+x].type == "Porte":
- posx_porte = i_humain
- posy_porte = j_humain
- if not carte[posx_porte,posy_porte].stock_medicaments == 0 : # Si le stock n'est pas vide
- if not self.sac["Médicaments"]== Médicaments_max : # Si le sac n'est pas plein
- if carte[posx_porte,posy_porte].stock_medicaments >= (Médicaments_max - self.sac["Médicaments"]) : # Si stock >= Place dans le sac
- carte[posx_porte,posy_porte].stock_medicaments -= (Médicaments_max - self.sac["Médicaments"]) # Vide le stock de la quantité à remplir dans le sac
- self.sac["Médicaments"] = Médicaments_max # Rempli au max le sac
- if carte[posx_porte,posy_porte].stock_medicaments < (Médicaments_max - self.sac["Médicaments"]) : # Si stock < Place dans le sac
- self.sac["Médicaments"] += carte[posx_porte,posy_porte].stock_medicaments # Rempli le sac avec la taille du stock
- carte[posx_porte,posy_porte].stock_medicaments -= 0 #Médicaments_max-self.sac["Médicaments"] # Vide entièrement le stock
- if not carte[posx_porte,posy_porte].stock_munitions == 0 : # Même chose mais avec une autre ressource
- if not self.sac["Munitions"]== Munitions_max :
- if carte[posx_porte,posy_porte].stock_munitions >= (Munitions_max - self.sac["Munitions"]) :
- carte[posx_porte,posy_porte].stock_munitions -= (Munitions_max - self.sac["Munitions"])
- self.sac["Munitions"] = Munitions_max
- if carte[posx_porte,posy_porte].stock_munitions < (Munitions_max - self.sac["Munitions"]) :
- self.sac["Munitions"] += carte[posx_porte,posy_porte].stock_munitions
- carte[posx_porte,posy_porte].stock_munitions = 0 #Probleme: tu remet à 0 le nombres de munitions....
- if not carte[posx_porte,posy_porte].stock_nourriture == 0 : # Même chose mais avec une autre ressource
- if not self.sac["Nourriture"]== Nourriture_max :
- if carte[posx_porte,posy_porte].stock_nourriture >= (Nourriture_max - self.sac["Nourriture"]) :
- carte[posx_porte,posy_porte].stock_nourriture -= (Nourriture_max - self.sac["Nourriture"])
- self.sac["Nourriture"] = Nourriture_max
- if carte[posx_porte,posy_porte].stock_nourriture < (Nourriture_max - self.sac["Nourriture"]) :
- self.sac["Nourriture"] += carte[posx_porte,posy_porte].stock_nourriture
- carte[posx_porte,posy_porte].stock_nourriture = 0
- def move(self,carte,i_destination, j_destination):
- """
- Déplace un humain vers nouvelles coordonnées\
- destinaton_i, destination_j par un chemin possible
- """
- i_humain, j_humain = self.place_matrice_temporaire
- i_prox, j_prox = i_humain, j_humain
- i_boussole = 0 # Coordonnées de la direction
- j_boussole = 0
- if i_humain < i_destination : # Analyse du chemin idéal
- i_prox += 1
- i_boussole = 1
- elif i_humain > i_destination :
- i_prox -= 1
- i_boussole = -1
- if j_humain < j_destination :
- j_prox += 1
- j_boussole = 1
- elif j_humain > j_destination :
- j_prox -= 1
- j_boussole = -1
- if isinstance((carte[i_prox,j_prox]),Vide): # Déplacement prévu
- carte[i_prox,j_prox] = self
- #self.place_matrice=i_prox,j_prox
- carte[i_humain,j_humain] = Vide()
- print(carte[i_prox,j_prox],carte[i_humain,j_humain])
- elif isinstance(carte[i_prox - i_boussole ,j_prox],Vide): # Alternative si chemin obstrué
- carte[i_prox - i_boussole ,j_prox] = self
- #self.place_matrice=i_prox - i_boussole ,j_prox
- carte[i_humain,j_humain] = Vide()
- print(carte[i_prox - i_boussole ,j_prox],carte[i_humain,j_humain])
- elif isinstance(carte[i_prox,j_prox - j_boussole],Vide):
- carte[i_prox ,j_prox - j_boussole] = self
- #self.place_matrice=i_prox ,j_prox - j_boussole
- carte[i_humain,j_humain] = Vide()
- print(carte[i_prox ,j_prox - j_boussole],carte[i_humain,j_humain])
- def run(self,carte):
- """
- Lance la capacité de l'humain à se déplacer
- """
- i_humain, j_humain = self.place_matrice
- self.place_matrice_temporaire = self.place_matrice
- if len(self.liste_zombie) == 0: # Si il n'y a pas de zombie à l'horizon
- if self.sac["Munitions"] < 1: # Déplacement vers une porte quelconque
- i_porte, j_porte = trouve_batiment("Armurerie",i_humain,j_humain,carte) # Fonction qui rend toute les portes
- self.move(carte, i_porte, j_porte)
- elif self.faim < 30 and self.sac["Nourriture"] == 0 : # Déplacement vers n'importe quelle porte
- i_porte, j_porte = trouve_porte(carte) # Fonction qui rend toute les portes
- self.move(carte, i_porte, j_porte)
- elif self.pdv < 40 and self.sac["Médicaments"]==0:
- i_porte, j_porte = trouve_batiment("Hôpital",i_humain,j_humain,carte)
- self.move(carte,i_porte, j_porte)
- else:
- i_porte, j_porte = trouve_batiment("Maison",i_humain,j_humain,carte)
- self.move(carte,i_porte, j_porte)
- ########################################################################################################
- #FONCTIONS AUXILLIARES
- def where_classe(classe,carte):
- L=[]
- for k in range(0,len(carte)):
- for i in range(0,len(carte)):
- if isinstance(carte[k,i],classe):
- L+=[(k,i)]
- return L
- def trouve_porte(carte):
- """
- Rend la liste des coordonnées de l'ensemble des portes des batiments de la carte
- """
- LR = []
- for ligne in range (i):
- for colonne in range(j):
- if isinstance(carte[ligne,colonne],Batiment) and carte[ligne,colonne].type == "Porte":
- LR.append((ligne,colonne))
- return LR
- def liste_porte_Bat(carte,type_batiment):
- """
- Rend la liste des coordonnées de l'ensemble des portes des batiments convoités
- """
- liste_portes_desirees = []
- liste_toutes_portes = trouve_porte(carte)
- for i_porte,j_porte in liste_toutes_portes:
- for x in range(-1,2):
- if isinstance(carte[i_porte+x, j_porte+x], Batiment)\
- and carte[i_porte+x, j_porte+x].type == type_batiment:
- liste_portes_desirees.append((i_porte,j_porte))
- return liste_portes_desirees
- def trouve_batiment(type_batiment,i_humain,j_humain,carte):
- """
- Rend les coordonnées de la porte du batiment convoité le plus proche
- """
- L = liste_porte_Bat(carte, type_batiment)
- distance = 1000
- Bat_proche = None,None
- for k in range(len(L)):
- i_porte,j_porte = L[k]
- if min(math.sqrt((i_porte-i_humain)**2+(j_porte-j_humain)**2),distance)\
- ==(math.sqrt((i_porte-i_humain)**2+(j_porte-j_humain)**2)):
- distance = math.sqrt((i_porte-i_humain)**2+(j_porte-j_humain)**2)
- Bat_proche = i_porte,j_porte
- return Bat_proche
- #####################################################################################################
- #ZOMBIE
- class Zombie:
- """
- Classe définissant un zombie, qui peux se déplacer et cherche à tuer les humains
- """
- num_zombie=0
- def __init__(self):
- self.nom = Zombie.num_zombie # Attribue le nom à chaque zombie
- Zombie.num_zombie += 1 # Compteur pour différencier chaque zombie
- def __str__(self):
- return "Zombie " + str(self.nom)
- def __repr__(self):
- return self.__str__() # Affiche le zombie par son nom dans le tableau
- def sentir(self, carte):
- """
- Trouve les coordonnées de l'humain le plus proche
- """
- i_zombie,j_zombie = np.where(carte == self) # Donne l'emplacement du zombie observé
- i_zombie,j_zombie = int(i_zombie), int(j_zombie)
- liste_humain = where_classe(Humain ,carte) # Arrange cette liste en liste de tuples
- self.place_matrice = i_zombie ,j_zombie
- distance = 1000 # Distance supposée du plus proche humain
- i_humain_cible = None # Coordonnée i de l'humain le plus proche
- j_humain_cible = None # Coordonnée j de l'humain le plus proche
- for X in range(len(liste_humain)): # Parcours les positions d'humains
- i_humain, j_humain = liste_humain[X]
- if i_humain in range((i_zombie-Odorat_Z),(i_zombie+Odorat_Z+1))\
- and j_humain in range((j_zombie-Odorat_Z),(j_zombie+Odorat_Z+1)):
- if min(math.sqrt((i_humain - i_zombie)**2+(j_humain - j_zombie)**2),distance)\
- ==(math.sqrt((i_humain-i_zombie)**2+(j_humain-j_zombie)**2)): # Compare la distance H-Z avec la plus petite trouvée
- i_humain_cible = i_humain
- j_humain_cible = j_humain
- distance = (math.sqrt((i_humain - i_zombie)**2 + (j_humain - j_zombie)**2))
- self.coord_humain=(i_humain_cible ,j_humain_cible)
- def move(self, carte):
- if self.coord_humain==(None,None):
- compteur_boucle = 0 # Compteur de sortie de boucle
- while compteur_boucle == 0 :
- x = random.randint(-1,1) # Tirage du déplacement vertical
- y = random.randint(-1,1) # Tirage du déplacement horizontal
- i_zombie, j_zombie = self.place_matrice
- i1 = i_zombie + x
- j1 = j_zombie + y
- if (i1 in range(i)) and (j1 in range(j)): # Vérifie que le déplacement voulu ne sort pas de la map
- if isinstance(carte[i1,j1], Vide):
- carte[i1,j1] = self
- carte[i_zombie,j_zombie] = Vide()
- self.place_matrice=i1,j1
- compteur_boucle += 1
- else:
- A1,B1=self.coord_humain
- A,B=self.place_matrice
- A2,B2=0,0
- A1,B1,A2,B2=nouveau_coordonnées(A1,B1,A,B)
- if isinstance(carte[A2,B2], Vide):
- carte[A2,B2]=self
- carte[A,B]=Vide()
- if isinstance(carte[A2,B2], Humain):
- x=random.randint(0,1)
- if x==1:
- print("Un humain se fait dévorer!")
- carte[A2,B2]=self
- carte[A,B]=Vide()
- else:
- print("Un humain devient un zombie!")
- carte[A2,B2]=self
- carte[A,B]=Zombie()
- def nouveau_coordonnées(H1,H2,Z1,Z2):
- if H1<Z1:
- Z1-=1
- if H1>Z1:
- Z1+=1
- if H2<Z2:
- Z2-=1
- if H2>Z2:
- Z2+=1
- return (H1,H2,Z1,Z2)
- ########################################################################################################
- #BATIMENTS
- class Batiment:
- """
- Création de la classe Bâtiment qui comprend les différents types de batiments
- """
- def __init__(self, taille_batiment): # Définit le type des bâtiments selon leur taille
- if taille_batiment == 1:
- self.type = "Porte"
- elif taille_batiment in range(4,7):
- self.type = "Maison"
- elif taille_batiment == 7 :
- self.type = "Armurerie"
- elif taille_batiment in range(8,12):
- self.type = "Magasin"
- elif taille_batiment == 18:
- self.type = "Hôpital"
- if self.type == "Porte": #Stocks des portes
- self.stock_medicaments = 0
- self.stock_nourriture = 0
- self.stock_munitions = 0
- def __str__(self): # Fait en sorte que leur nom soit leur type
- return self.type
- def __repr__(self):
- return self.__str__()
- ########################################################################################################
- #VIDE
- class Vide:
- def __repr__(self):
- return "Vide"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement