174 lines
4.8 KiB
Python
174 lines
4.8 KiB
Python
#!~/.pyenv/versions/3.11.6/bin/python
|
|
#
|
|
# Copyright (c) 2024 Cutieguwu | Olivia Brooks
|
|
#
|
|
# -*- coding:utf-8 -*-
|
|
# @Title: Levels in a game.
|
|
# @Author: Cutieguwu | Olivia Brooks
|
|
# @Email: owen.brooks77@gmail.com | obroo2@ocdsb.ca
|
|
# @Description: Classes descibing different levels in a pygame game.
|
|
#
|
|
# @Script: level.py
|
|
# @Date Created: 16 Apr, 2024
|
|
# @Last Modified: 09 May, 2024
|
|
# @Last Modified by: Cutieguwu | Olivia Brooks
|
|
# ----------------------------------------------------------
|
|
|
|
from icecream import ic
|
|
from lib.section import Section, Blocks
|
|
from lib.scene import Scene
|
|
|
|
# Note: opt for importing IceCream in each library instead of using install() in case it is unavailable.
|
|
# Taken from IceCream devs github at https://github.com/gruns/icecream
|
|
try:
|
|
from icecream import ic
|
|
except ImportError: # Graceful fallback if IceCream isn't installed.
|
|
ic = lambda *a: None if not a else (a[0] if len(a) == 1 else a) # Whatever the heck this filler function does.
|
|
|
|
|
|
class Level():
|
|
"""
|
|
Creates and manages the level scape.
|
|
"""
|
|
|
|
def __init__(self, parent):
|
|
self.PARENT = parent
|
|
self.WINDOW = self.PARENT.WINDOW
|
|
self.SCALE = self.PARENT.SCALE
|
|
self.SECTION = Section(self.PARENT)
|
|
self.SCENE = Scene()
|
|
|
|
self.layout = Example() # Should be Main(), but Example for testing.
|
|
|
|
self.dx = 0
|
|
self.dy = 0
|
|
|
|
self.get_layout_size()
|
|
|
|
def draw(self):
|
|
"""
|
|
Draws the level on the screen.
|
|
"""
|
|
|
|
if self.layout.TYPE == "scene":
|
|
self.SCENE.draw()
|
|
elif self.layout.TYPE == "level":
|
|
self.SECTION.draw(self.layout.LAYOUT)
|
|
|
|
def load_layout(self, layout:object):
|
|
"""
|
|
Load a new layout configuration.
|
|
"""
|
|
|
|
self.layout = layout
|
|
self.draw()
|
|
self.get_layout_size()
|
|
|
|
def get_layout_size(self):
|
|
"""
|
|
Fetches the size of the active layout
|
|
"""
|
|
width = 0
|
|
height = 0
|
|
|
|
for coords, block in self.layout.LAYOUT.items():
|
|
try:
|
|
if coords[2] > width:
|
|
width = coords[2]
|
|
|
|
if coords[3] > height:
|
|
height = coords[3]
|
|
|
|
except IndexError:
|
|
if coords[0] > width:
|
|
width = coords[0]
|
|
|
|
if coords[1] > height:
|
|
height = coords[1]
|
|
|
|
self.width = width
|
|
self.height = height
|
|
|
|
def update_deltas_relative_to_player(self):
|
|
"""
|
|
Called with a scale update.
|
|
Recalculates self.dx and self.dy so that player is centered properly.
|
|
"""
|
|
|
|
borderWidthX = (self.SCALE.gameX // 2) + 1
|
|
borderWidthY = (self.SCALE.gameY // 2) + 1
|
|
|
|
if self.PARENT.PLAYER.x < borderWidthX:
|
|
# Player is in left border
|
|
self.dx = 0
|
|
|
|
elif self.PARENT.PLAYER.x > self.width - borderWidthX:
|
|
# Player is in right border
|
|
self.dx = self.width - self.SCALE.gameX
|
|
|
|
else:
|
|
# Player is within x boundaries
|
|
self.dx = self.PARENT.PLAYER.x - borderWidthX
|
|
|
|
|
|
if self.PARENT.PLAYER.y < borderWidthY:
|
|
# Player is in the top border
|
|
self.dy = 0
|
|
|
|
elif self.PARENT.PLAYER.y > self.height - borderWidthY:
|
|
# Player is in bottom border.
|
|
self.dy = self.height - self.SCALE.gameY
|
|
|
|
else:
|
|
# Player is within y boundaries.
|
|
self.dy = self.PARENT.PLAYER.y - borderWidthY
|
|
|
|
self.dx = int(self.dx)
|
|
self.dy = int(self.dy)
|
|
|
|
"""
|
|
NAME :str
|
|
HAS_PLAYER :bool
|
|
TYPE :str = "scene", "menu", "level"
|
|
|
|
scene # Does not scale with gameScale or uiScale
|
|
# Each obj in LAYOUT is a different frame, not block sections.
|
|
# Does not have a player, so HAS_PLAYER is not checked, and can be removed from definition.
|
|
|
|
menu # Does not scale with gameScale
|
|
|
|
LAYOUT :dict = {(x, y, rangeX, rangeY): obj}
|
|
BGFILL :tuple = (r, g, b)
|
|
:pygame.surface.Surface
|
|
"""
|
|
|
|
class Intro_Load():
|
|
def __init__(self):
|
|
|
|
self.TYPE = "scene"
|
|
self.LAYOUT = {}
|
|
|
|
class Main():
|
|
def __init__(self):
|
|
|
|
self.TYPE = "menu"
|
|
self.HAS_PLAYER = False
|
|
self.LAYOUT = {}
|
|
|
|
class Settings():
|
|
def __init__(self):
|
|
|
|
self.TYPE = "menu"
|
|
self.HAS_PLAYER = False
|
|
self.LAYOUT = {}
|
|
|
|
class Example():
|
|
def __init__(self):
|
|
|
|
self.TYPE = "level"
|
|
self.HAS_PLAYER = True
|
|
self.LAYOUT = {(0, 0, 32, 32): Blocks.block_blue(),
|
|
(2, 1): Blocks.block_rainbow(layer=1)
|
|
}
|
|
self.STARTPOSITIONS = {"center": (0, 0), "left": (-10, 0), "right": (10, 0), "up": (0, 10), "down": (0, -10)}
|
|
self.BGFILL = (0, 0, 0) |