Skip to content

Commit

Permalink
Feb 20
Browse files Browse the repository at this point in the history
  • Loading branch information
opbo6311 authored and opbo6311 committed Feb 20, 2017
1 parent 36df492 commit a33002e
Show file tree
Hide file tree
Showing 38 changed files with 847 additions and 0 deletions.
17 changes: 17 additions & 0 deletions MyTestProject/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>MyTestProject</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.python.pydev.pythonNature</nature>
</natures>
</projectDescription>
8 changes: 8 additions & 0 deletions MyTestProject/.pydevproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?><pydev_project>
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/${PROJECT_DIR_NAME}/src</path>
</pydev_pathproperty>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 3.0</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
</pydev_project>
Empty file.
Binary file not shown.
Binary file not shown.
33 changes: 33 additions & 0 deletions MyTestProject/src/root/nested/RepeatedTimer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'''
Created on 9 févr. 2017
@author: opbo6311
'''

from threading import Timer

class RepeatedTimer(object):
def __init__(self, interval, function, *args, **kwargs):
self._timer = None
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs
self.is_running = False
self.start()

def _run(self):
self.is_running = False
self.start()
self.function(*self.args, **self.kwargs)

def start(self):
if not self.is_running:
self._timer = Timer(self.interval, self._run)
self._timer.start()
self.is_running = True

def stop(self):
self._timer.cancel()
self.is_running = False

Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
142 changes: 142 additions & 0 deletions MyTestProject/src/root/nested/cobaye.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
'''
Created on 30 nov. 2016
@author: opbo6311
'''
from root.nested.habitant import Habitant
from tkinter import *
from root.nested.terrain import *
from root.nested.constantes import Constantes
from math import sqrt

class Cobaye(Habitant):
"Un cobaye dans le monde virtuel"
nivenergie=Constantes.cobayeenergiedepart
distancenourriture=Constantes.infini

def __init__(self, ter, posx, posy):
"Constructeur de la classe Cobaye"
super().__init__(ter, posx, posy)
self.vitessemax=10
self.vision=40
self.forme = self.terrain.canvas.create_oval(self.positionx-5 , self.positiony-5, self.positionx+5, self.positiony+5, width=2, fill='blue')
self.type="Cobaye"
self.nivenergie=Constantes.cobayeenergiedepart
self.terrain.stockage.append(["Cobaye Start", self.positionx, self.positiony])
#self.terrain.stockagestr=self.terrain.stockagestr+"{{'catégorie':'cobaye','x':{0},'y':{1},'energie':{2}}}".format(self.positionx, self.positiony, self.nivenergie)
#print("stockagestr:", self.terrain.stockagestr)

def computemove(self):
"Calcul du prochain mouvement de ce cobaye"
# Trouver les prédateurs à proximité. Choisir le plus proche. Le fuir.
predposx, predposy = self.terrain.plusprocheselontype(self.positionx, self.positiony, "Predateur")
#print ("positions retournées move fuite cobaye", predposx, predposy)
vecteurdepx=0
vecteurdepy=0
self.nextpositionx=Constantes.nondefini
self.nextpositiony=Constantes.nondefini
if (predposx != Constantes.nondefini):
vecteurdepx=self.positionx-predposx
vecteurdepy=self.positiony-predposy
#print ("vecteurdep brut move fuite cobaye", vecteurdepx, vecteurdepy)
# Normalisation par rapport à la vitessemax
normevecteurdep=sqrt((vecteurdepx)**2 + (vecteurdepy)**2)
if normevecteurdep <= self.vision:
vecteurdepx=self.vitessemax*vecteurdepx/normevecteurdep
vecteurdepy=self.vitessemax*vecteurdepy/normevecteurdep
#print ("vecteurdep normalisé move fuite cobaye", vecteurdepx, vecteurdepy)
self.nextpositionx=self.positionx+vecteurdepx
if (self.nextpositionx<7):
self.nextpositionx=7
if (self.nextpositionx>self.terrain.dimensionx-7):
self.nextpositionx=self.terrain.dimensionx-7
self.nextpositiony=self.positiony+vecteurdepy
if (self.nextpositiony<7):
self.nextpositiony=7
if (self.nextpositiony>self.terrain.dimensiony-7):
self.nextpositiony=self.terrain.dimensiony-7
# Affichage du vecteur
self.formemove=self.terrain.canvas.create_line(self.positionx, self.positiony, self.nextpositionx, self.nextpositiony, arrow="last", width=2, fill='blue')
if (self.nextpositionx == Constantes.nondefini):
# Si la faim tiraille, il faut chercher à manger.
nourposx, nourposy = self.terrain.plusprocheselontype(self.positionx, self.positiony, "NourritureFixe")
#print ("positions retournées move nourriture cobaye", nourposx, nourposy)
if (nourposx != Constantes.nondefini):
# Aller dans la direction en question
vecteurdepx=self.positionx-nourposx
vecteurdepy=self.positiony-nourposy
#print ("vecteurdep brut move nourriture cobaye", vecteurdepx, vecteurdepy)
# Normalisation par rapport à la vitessemax
normevecteurdep=sqrt((vecteurdepx)**2 + (vecteurdepy)**2)
if normevecteurdep <= 20*self.vision:
if normevecteurdep>self.vitessemax:
vecteurdepx=-self.vitessemax*vecteurdepx/normevecteurdep
vecteurdepy=-self.vitessemax*vecteurdepy/normevecteurdep
else:
vecteurdepx=-vecteurdepx
vecteurdepy=-vecteurdepy
#print ("vecteurdep normalisé move nourriture cobaye", vecteurdepx, vecteurdepy)
self.nextpositionx=self.positionx+vecteurdepx
self.nextpositiony=self.positiony+vecteurdepy
# Affichage du vecteur
self.formemove=self.terrain.canvas.create_line(self.positionx, self.positiony, self.nextpositionx, self.nextpositiony, arrow="last", width=2, fill='blue')
#else:
# Se déplacer au hasard

def performmove(self):
"Réalisation du move déjà calculé"
# Le tour de mouvement coute de la nourriture
self.nivenergie=self.nivenergie-Constantes.coutenergiecobayepartour
self.terrain.stockage.append(["Cobaye Energie", self.nivenergie])
# Tester si mouvement nécessaire
if self.nextpositionx != Constantes.nondefini:
# Effacer la flèche.
self.terrain.canvas.delete(self.formemove)
self.formemove=Constantes.nondefini
# Bouger le cobaye.
self.positionx=self.nextpositionx
self.positiony=self.nextpositiony
self.nextpositionx=Constantes.nondefini
self.nextpositiony=Constantes.nondefini
self.terrain.canvas.coords(self.forme, self.positionx-5, self.positiony-5, self.positionx+5, self.positiony+5)
self.terrain.stockage.append(["Cobaye Move", self.positionx, self.positiony])

def aftermove(self):
"Réalisation des actions post move"
# Si la nourriture est atteinte, recharger le compteur de nourriture, et dire à la nourriture qu'elle est consommée.
# Savoir si une nourriture est au contact.
nourposx, nourposy = self.terrain.plusprocheselontype(self.positionx, self.positiony, "NourritureFixe")
#print ("positions retournées after move nourriture cobaye", nourposx, nourposy)
if (nourposx != Constantes.nondefini):
# mesurer la distance
vecteurx=self.positionx-nourposx
vecteury=self.positiony-nourposy
#print ("vecteurdep brut after move nourriture cobaye", vecteurx, vecteury)
# Calcul de la distance
self.distancenourriture=sqrt((vecteurx)**2 + (vecteury)**2)
#print("Distance after move nourriture cobaye", distancenourriture)
if (self.distancenourriture < 0.1):
self.nivenergie=self.nivenergie+Constantes.energienourriturefixe
lanourriture=self.terrain.trouverhabitant("NourritureFixe", nourposx, nourposy)
self.terrain.retirerhabitant(lanourriture)

# Si le cobaye n'a plus d'énergie, il meurt.
if (self.nivenergie == 0):
self.terrain.retirerhabitant(self)
print("Le cobaye est mort de faim")

# Si on est mort, sortir du plateau.

def disparaitre(self):
"Actions à réaliser avant de faire sortir cet habitant"
self.terrain.canvas.delete(self.forme)
self.terrain.stockage.append(["Cobaye Delete", self.positionx, self.positiony ])

def display(self, txtwidget):
"Donner à chaque objet (habitant, mais pas que...) l'opportunité d'afficher quelque chose sur le controleur"
txtwidget.insert(END, "Cobaye: distance à la nourriture: {}.\n".format(self.distancenourriture))
txtwidget.insert(END, "Cobaye: niveau d'énergie: {}.\n ".format(self.nivenergie))

def stocker(self, first=0):
"Stockage dans je json"
self.terrain.stockagestr=self.terrain.stockagestr+"{{'catégorie':'cobaye','x':{0},'y':{1},'energie':{2}}}".format(self.positionx, self.positiony, self.nivenergie)
52 changes: 52 additions & 0 deletions MyTestProject/src/root/nested/constantes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'''
Created on 12 déc. 2016
@author: opbo6311
'''

class Constantes(object):
"Les constantes pour tout l'environnement"
infini=99999999
nondefini=-1
cobayeenergiedepart=100
coutenergiecobayepartour=1
energienourriturefixe=50
dimensionresultx=750
dimensionresulty=100

def __init__(self):
"Constructeur de la classe des constantes"


""" Exemple de format de message
{
"start":{
"dimensions": { "x": 300, "y": 600}
},
"tours": [
{
"tour nb": 0,
"population":[
{"catégorie":"cobaye", "x": 305, "y": 700, "energie": 45},
{"catégorie":"predateur", "x": 301, "y": 600},
{"catégorie":"predateur", "x": 302, "y": 600},
{"catégorie":"predateur", "x": 303, "y": 600},
{"catégorie":"nourriturefixe", "x": 303, "y": 600}
]
},
{
"tour nb": 1,
"population":[
{"catégorie":"cobaye", "x": 305, "y": 700, "energie": 45},
{"catégorie":"predateur", "x": 301, "y": 600},
{"catégorie":"predateur", "x": 302, "y": 600},
{"catégorie":"predateur", "x": 303, "y": 600},
{"catégorie":"nourriturefixe", "x": 303, "y": 600}
]
}
],
"end":{
"survie": 100
}
}
"""
Loading

0 comments on commit a33002e

Please sign in to comment.