Added parallax to backgrounds

This commit is contained in:
theo@manjaro 2021-11-20 21:23:05 +01:00
parent d48cf64816
commit c3c45854bb
10 changed files with 32 additions and 8 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 144 KiB

View File

@ -36,8 +36,9 @@ class Game():
mapdico = {}
mapdico["name"] = mapfolder.split(os.sep)[-1]
mapdico["cover"] = None
mapdico["background"] = None
mapdico["backgrounds"] = []
mapdico["data"] = None
mapdico["filler"] = None
mapdico["tilesets"] = {}
scanner = os.scandir(path=mapfolder)
for i in scanner: # Je check tout les fichiers du dossier
@ -46,10 +47,12 @@ class Game():
if name.endswith(".png"):
if name=="cover.png":
mapdico["cover"] = pygame.image.load(i.path)
elif name=="background.png":
mapdico["background"] = pygame.image.load(i.path)
elif name.startswith("background"):
mapdico["backgrounds"].append({"sprite":pygame.image.load(i.path).convert_alpha(),"name":i.path})
elif name=="filler.png":
mapdico["filler"] = pygame.image.load(i.path).convert_alpha()
else:
mapdico["tilesets"][i.name] = pygame.image.load(i.path)
mapdico["tilesets"][i.name] = pygame.image.load(i.path).convert_alpha()
except:
self.log("Erreur",mapfolder,name,"Fichier invalide")
if name=="map.json":
@ -59,6 +62,7 @@ class Game():
except:
self.log("Erreur",mapfolder,name)
if mapdico["data"]:
mapdico["backgrounds"].sort(key=lambda x: x["name"])
return mapdico
return None

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -16,9 +16,11 @@ class TilesetRenderer(BaseObject):
self.level = game.levels_lib[mapfoldername]
self.reinit(self.level)
self.bg = game.sprite_lib["fallbackground.png"]
if self.level["background"]:
self.bg = self.level["background"]
self.bgs = [game.sprite_lib["fallbackground.png"]]
if len(self.level["backgrounds"]):
self.bgs = self.level["backgrounds"]
self.bgoffset = self.layers[0]["surface"].get_height()/2-self.game.globals["camerah"]
def step(self):
# Spawning ennemies
@ -28,7 +30,25 @@ class TilesetRenderer(BaseObject):
self.game.gameloop.summon(e)
def draw(self):
self.game.window.blit(self.game.pygame.transform.scale(self.bg,(self.game.globals["cameraw"],self.game.globals["camerah"])),[0,0])
wratio = self.game.globals["cameraw"]/self.game.DISPLAY_WIDTH
hratio = self.game.globals["camerah"]/self.game.DISPLAY_HEIGHT
lastoffset = 0
for i,bg in enumerate(self.bgs):
ratio = 1-(len(self.bgs)-i)/len(self.bgs)
width, height = round(bg["sprite"].get_width()*wratio),round(bg["sprite"].get_height()*hratio)
sprite = self.game.pygame.transform.scale(bg["sprite"],(width,height))
nbs = self.game.globals["cameraw"]/width
lastoffset = self.rect[1]+(self.bgoffset-self.game.globals["cameray"])*ratio
for i in range(round(nbs+0.5)):
self.game.window.blit(sprite,[(-self.game.globals["camerax"]*ratio)%width+(i-1)*width,lastoffset])
# Draw filler
if self.level["filler"]:
filleroffset = lastoffset+height
self.game.window.blit(self.level["filler"],[0,filleroffset])
# Separator
sep = self.game.pygame.transform.scale(self.game.sprite_lib["backgroundseparator.png"],[self.game.globals["cameraw"],self.game.globals["camerah"]])
self.game.window.blit(sep,[0,0])
# Layers
for layer in self.layers:
self.game.window.blit(layer["surface"],(layer["offsets"][0]-self.game.globals["camerax"],layer["offsets"][1]-self.game.globals["cameray"]))