2021-06-25 14:42:35 +02:00
|
|
|
class GameLoop():
|
|
|
|
def __init__(self):
|
|
|
|
self.reinit()
|
|
|
|
|
|
|
|
def reinit(self):
|
|
|
|
# Réinitialise la boucle de jeu
|
|
|
|
self.objects = {}
|
|
|
|
self.maxid = 0
|
|
|
|
|
|
|
|
def summon(self,obj):
|
|
|
|
# Ajoute un objet à la boucle de jeu
|
|
|
|
obj.id = self.maxid # Donne un identifiant unique à l'objet
|
|
|
|
self.objects[self.maxid] = obj # Ajoute l'objet à la boucle
|
|
|
|
self.maxid+=1 # Change le prochain id disponible
|
|
|
|
|
|
|
|
return obj # Retourne l'objet stocké, au cas ou !!!!
|
|
|
|
|
|
|
|
def findname(self,name):
|
|
|
|
# Retournes tout les objets portants ce nom
|
|
|
|
result = []
|
|
|
|
for i in self.objects.values():
|
|
|
|
if type(i).__name__==name: # Si le nom de la classe correspond au nom donné, je retourne la classe
|
|
|
|
result.append(i)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def delid(self,id):
|
|
|
|
# Supprime un objet à partir de son id
|
2021-11-27 19:18:25 +01:00
|
|
|
if id in self.objects.keys():
|
|
|
|
del(self.objects[id])
|
2021-06-25 14:42:35 +02:00
|
|
|
|
|
|
|
def delname(self,name):
|
|
|
|
# Supprimes les objet à partir d'un nom
|
|
|
|
for i in self.findname(name):
|
|
|
|
self.delid(i.id)
|
|
|
|
|
|
|
|
def step(self,game):
|
|
|
|
# Addoucie le secouement de la caméra
|
|
|
|
for i in ["x","y"]:
|
|
|
|
game.globals["scamera"+i]-=game.dt*5
|
|
|
|
game.globals["scamera"+i]= max(0,game.globals["scamera"+i])
|
|
|
|
|
|
|
|
objs = list(self.objects.values())
|
|
|
|
for i in objs:
|
2021-11-25 13:41:38 +01:00
|
|
|
if not game.globals["pause"] or not i.ispaused:
|
|
|
|
i.step()
|
2021-06-25 14:42:35 +02:00
|
|
|
|
|
|
|
def draw(self,game):
|
|
|
|
#Je secoue ma caméra
|
|
|
|
vx = game.globals["scamerax"]
|
|
|
|
vy = game.globals["scameray"]
|
|
|
|
rx = game.lib.random()*2*vx-vx
|
|
|
|
ry = game.lib.random()*2*vy-vy
|
|
|
|
game.globals["camerax"]+=rx
|
|
|
|
game.globals["cameray"]+=ry
|
|
|
|
# Je dessine
|
|
|
|
values = list(self.objects.values())
|
|
|
|
tri = lambda x: x.depth # Donne la profondeur de l'objet
|
|
|
|
values.sort(key=tri)
|
|
|
|
for i in values:
|
2021-06-25 16:13:52 +02:00
|
|
|
i.draw() # Lancer le script d'affichage de chaques objets
|
2021-06-25 14:42:35 +02:00
|
|
|
# Je stabilise la caméra
|
|
|
|
game.globals["camerax"]-=rx
|
|
|
|
game.globals["cameray"]-=ry
|
2021-07-02 21:06:34 +02:00
|
|
|
|
|
|
|
# Get the right ratio
|
|
|
|
widthratio = game.DISPLAY_WIDTH/game.globals["cameraw"]
|
|
|
|
heightratio = game.DISPLAY_HEIGHT/game.globals["camerah"]
|
|
|
|
game.globals["tempsubsurface"].blit(game.window,[0,0])
|
|
|
|
temp = game.pygame.transform.scale(game.globals["tempsubsurface"],(game.DISPLAY_WIDTH,game.DISPLAY_HEIGHT))
|
|
|
|
game.realwindow.blit(temp,[0,0])
|