2021-09-10 19:22:30 +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
|
|
|
|
del(self.objects[id])
|
|
|
|
|
|
|
|
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-09-19 12:20:43 +02:00
|
|
|
if i.handlepause and not game.globals["pause"] or not i.handlepause:
|
|
|
|
i.step()
|
2021-09-10 19:22:30 +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:
|
|
|
|
i.draw() # Lancer le script d'affichage de chaques objets
|
|
|
|
# Je stabilise la caméra
|
|
|
|
game.globals["camerax"]-=rx
|
|
|
|
game.globals["cameray"]-=ry
|
|
|
|
|
|
|
|
# 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])
|
2021-09-13 11:33:40 +02:00
|
|
|
for i in values:
|
|
|
|
i.afterdraw() # Draw on the "real screen"
|
2021-09-19 20:12:35 +02:00
|
|
|
|
|
|
|
# Draw the game window on screen
|
|
|
|
game.screen.fill([0,0,0])
|
|
|
|
|
|
|
|
w,h = game.screenw,game.screenh
|
|
|
|
height_on_width = round(game.screenw/16*9)
|
|
|
|
if height_on_width > h:
|
|
|
|
w = round(game.screenh/9*16)
|
|
|
|
else:
|
|
|
|
h = height_on_width
|
|
|
|
game.screenoffx = (game.screenw-w)/2
|
|
|
|
game.screenoffy = (game.screenh-h)/2
|
|
|
|
game.resizescreenw = w
|
|
|
|
game.resizescreenh = h
|
|
|
|
game.screen.blit(game.pygame.transform.scale(game.realwindow,[w,h]),[game.screenoffx,game.screenoffy])
|