Skip to content

Commit

Permalink
feat(worlds): ✨ Added a bgpic!!
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsunami014 (Max) authored and Tsunami014 (Max) committed Jan 3, 2025
1 parent cf74464 commit 9e4e1d2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
6 changes: 5 additions & 1 deletion BlazeSudio/Game/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ def draw(self):
realpos = self.Game.curScene.CamPos
realpos = [realpos[0] * scale, realpos[1] * scale]

win.fill(self.Game.currentLvL.bgColour)
bg = self.Game.currentLvL.bgPic
if bg is not None:
win.blit(pygame.transform.scale(bg, sze), (0, 0))
else:
win.fill(self.Game.currentLvL.bgColour)
sur = rend or pygame.Surface((0, 0))

bounds = self.Game.curScene.CamBounds
Expand Down
18 changes: 14 additions & 4 deletions BlazeSudio/ldtk/Pyldtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, jsoninf: Dict[str, str], fileloc: str = ''): ## It takes in J
t = Tileset(fileloc, i)
self.tilesets[t.uid] = t

self.levels: List[Ldtklevel] = [Ldtklevel(lvl, self.tilesets, self.defs) for lvl in self.ldtkData['levels']]
self.levels: List[Ldtklevel] = [Ldtklevel(lvl, self.tilesets, self.defs, fileloc) for lvl in self.ldtkData['levels']]

class Ldtk(LdtkJSON):
def __init__(self, ldtkfile: str): ## It takes in the file path
Expand Down Expand Up @@ -232,24 +232,29 @@ def get_tile(self, ui: bool = False) -> pygame.Surface:
return outsur

class Ldtklevel:
def __init__(self, data: Dict, tilesets: Dict[int, Tileset], defs: Dict):
def __init__(self, data: Dict, tilesets: Dict[int, Tileset], defs: Dict, fileloc: str):
"""
An LDtk Level object which contains multiple layers.
Args:
data (Dict): The data that has info about the layer in it.
tilesets (Dict[int, Tileset]): All the tilesets.
defs (Dict): The defs from the main LDtk file.
fileloc (str): The location of the LDtk file.
"""
self.defs: Dict = defs
self.data: Dict = data
self.tilesets: Dict[int, Tileset] = tilesets
self.fileLoc: str = fileloc

self.identifier: str = self.data['identifier']
self.iid: str = self.data['iid']
self.uid: int = self.data['uid']
self.worldPos: List[int] = [self.data['worldX'], self.data['worldY'], self.data['worldDepth']]
self.bgColour: str = self.data['bgColor'] or self.data['__bgColor']
self.bgPic: pygame.Surface|None = None
if self.data['bgRelPath'] is not None:
self.bgPic = pygame.image.load(os.path.abspath(os.path.join(fileloc,'../',self.data['bgRelPath'])))
self.fieldInstances: List[Dict] = self.data['fieldInstances'] # The specific level flags
self.sizePx: List[int] = [self.data['pxWid'], self.data['pxHei']]
self.neighbours: List[Dict] = self.data['__neighbours']
Expand All @@ -273,7 +278,12 @@ def Render(self, transparent_bg=False):
if transparent_bg:
end.fill((0, 0, 0, 0))
else:
end.fill(self.bgColour)
bg = self.bgPic
if bg is not None:
sf = max(self.sizePx[0] / bg.get_width(), self.sizePx[1] / bg.get_height())
end.blit(pygame.transform.scale(bg, (int(bg.get_width() * sf), int(bg.get_height() * sf))), (0, 0))
else:
end.fill(self.bgColour)
for i in self.layers:
end.blit(i.getImg(), (0, 0))
return end
Expand All @@ -289,7 +299,7 @@ def CollisionLayer(self, collisionFunc: Callable[[Tileset], pygame.Surface]) ->
Returns:
Ldtklevel: The new level with the collision data.
"""
newLevel = Ldtklevel(self.data, self.tilesets, self.defs)
newLevel = Ldtklevel(self.data, self.tilesets, self.defs, self.fileLoc)
for i in range(len(newLevel.layers)):
newLevel.layers[i] = newLevel.layers[i].CollisionLayer(collisionFunc)
return newLevel
Expand Down

0 comments on commit 9e4e1d2

Please sign in to comment.