Skip to content
Snippets Groups Projects
Commit dbb42321 authored by Leon Grothus's avatar Leon Grothus
Browse files

added background_manager

parent 0ebbabaa
No related branches found
No related tags found
No related merge requests found
Showing
with 82 additions and 12 deletions
assets/images/pinball_background.jpg

60.2 KiB

assets/images/pinball_background/pinball_background_00.jpg

76.9 KiB

assets/images/pinball_background/pinball_background_01.jpg

79.1 KiB

assets/images/pinball_background/pinball_background_02.jpg

79.5 KiB

assets/images/pinball_background/pinball_background_03.jpg

81.6 KiB

assets/images/pinball_background/pinball_background_04.jpg

82 KiB

assets/images/pinball_background/pinball_background_05.jpg

84.3 KiB

assets/images/pinball_background/pinball_background_06.jpg

84.8 KiB

assets/images/pinball_background/pinball_background_07.jpg

87.2 KiB

assets/images/pinball_background/pinball_background_08.jpg

87.2 KiB

assets/images/pinball_background/pinball_background_09.jpg

76.9 KiB

assets/images/pinball_background/pinball_background_10.jpg

87.2 KiB

assets/images/pinball_background/pinball_background_11.jpg

76.9 KiB

assets/images/pinball_background/pinball_background_12.jpg

87.2 KiB

data/data.png

172 B

{"asf": 1.0, "master_volume": 50, "music_volume": 50, "sfx_volume": 50, "user_name": ""}
\ No newline at end of file
{"asf": 1.5, "master_volume": 50, "music_volume": 50, "sfx_volume": 50, "user_name": "Player"}
\ No newline at end of file
import os
from pathlib import Path
import pygame
import time
class BackgroundManager:
"""
A class to manage the background images in a game.
Attributes:
images (list[Path]): A list of paths to the image files.
delay (float): The delay in seconds between image changes.
screen (pygame.Surface): The screen to draw the image on.
current_image (int): The index of the current image.
last_update (float): The time in seconds since the last image change.
Methods:
__init__(self, images: list[Path], delay: float): Initializes the BackgroundManager.
update(self, delta_time): Updates the current image if the delay has passed.
draw(self, screen): Draws the current image to the given screen.
"""
def __init__(self, image_dir: Path, delay: float, screen: pygame.Surface) -> None:
"""
Initializes the BackgroundManager with a list of images and a delay.
Arguments:
image_dir (Path): The directory containing the images.
delay (float): The delay in seconds between image changes. If -1, the image will not change.
screen (pygame.Surface): The screen to draw the image on.
"""
# Load the images and scale them to the screen size
image_files = sorted([image for image in os.listdir(image_dir) if image.endswith('.jpg')])
self.images = []
for image_file in image_files:
image_path = os.path.join(image_dir, image_file)
pygame_image = pygame.image.load(image_path).convert()
self.images.append(pygame.transform.scale(pygame_image, (screen.get_width(), screen.get_height())))
self.delay = delay
self.screen = screen
self.current_image = 0
self.last_update = 0
def update(self, delta_time) -> None:
"""
Updates the current image if the delay has passed. If the delay is -1, the image will not change.
Arguments:
delta_time (float): The time in seconds since the last update.
"""
self.screen.blit(self.images[self.current_image], (0, 0))
if self.delay == -1:
return
self.last_update += delta_time
if self.last_update > self.delay:
self.current_image = (self.current_image + 1) % len(self.images)
self.last_update = 0
def update_scale(self) -> None:
"""
Updates the scale of the images.
"""
self.images = [pygame.transform.scale(image, (int(image.get_width()), int(image.get_height()))) for image in self.images]
\ No newline at end of file
from pathlib import Path
from pygame import Surface
from source.api.management.background_manager import BackgroundManager
from source.api.scene.scene import Scene
from constants import ASSETS_PATH
from source.game.scenes.main_menu import MainMenu
......@@ -31,10 +32,13 @@ class SceneManager:
"""
self.screen: Surface = screen
default_background = BackgroundManager(Path(ASSETS_PATH / Path("images/main_background")), -1, screen)
pinball_background = BackgroundManager(Path(ASSETS_PATH / Path("images/pinball_background")), 1, screen)
self.scenes: dict = {
"main_menu": MainMenu(self.screen, self, Path(ASSETS_PATH / Path("images/main_background.jpg"))),
"main_pinball": MainPinball(self.screen, self, Path(ASSETS_PATH / Path("images/pinball_background.jpg"))),
"options_menu": OptionsMenu(self.screen, self, Path(ASSETS_PATH / Path("images/main_background.jpg"))),
"main_menu": MainMenu(self.screen, self, default_background),
"main_pinball": MainPinball(self.screen, self, pinball_background),
"options_menu": OptionsMenu(self.screen, self, default_background),
}
self.active_scene = self.scenes[default]
self.active_scene.awake()
......
from abc import ABC
from pathlib import Path
import pygame
from pygame.event import Event
from source.api.management.background_manager import BackgroundManager
class BaseDisplay(ABC):
"""
Base class for all scenes. A scene is a collection of GameObjects. A scene can be serialized and deserialized. A scene can be initialized
......@@ -17,7 +18,7 @@ class BaseDisplay(ABC):
update(self, delta_time: float, events: list[Event])
unload(self)
"""
def __init__(self, screen: pygame.Surface, scene_manager, background_path: Path) -> None:
def __init__(self, screen: pygame.Surface, scene_manager, background_manager: BackgroundManager) -> None:
"""
Inits BaseDisplay with screen and scene_manager
......@@ -25,21 +26,22 @@ class BaseDisplay(ABC):
screen: pygame.Surface, the screen to draw on
scene_manager: SceneManager, the scene manager
"""
self.background = pygame.image.load(background_path).convert() # Load background image
self.background_manager = background_manager
self.screen: pygame.Surface = screen
self.scene_manager = scene_manager
### Methods to be extended by the user ###
def awake(self) -> None:
"""
Awake is called when the scene is initialized
Awake is called when the scene is initialized.
Returns:
None
"""
self.scaled_background = pygame.transform.scale(self.background, (self.screen.get_width(), self.screen.get_height())) # scales background image to fit screen size
self.background_manager.update_scale()
pass
def update(self, delta_time: float, events: list[Event]) -> None:
"""
......@@ -52,7 +54,7 @@ class BaseDisplay(ABC):
Returns:
None
"""
self.screen.blit(self.scaled_background, pygame.Vector2()) # redraws background image
self.background_manager.update(delta_time)
def unload(self) -> None:
"""
......@@ -61,6 +63,5 @@ class BaseDisplay(ABC):
Returns:
None
"""
pass
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment