diff --git a/assets/images/objects/spring.png b/assets/images/objects/spring.png new file mode 100644 index 0000000000000000000000000000000000000000..68293d2ff1ffbf695d0d8a8ea3084082252e5ce1 Binary files /dev/null and b/assets/images/objects/spring.png differ diff --git a/data/constants.py b/data/constants.py index 583a86e504e3ef8d6cc0b200fbbced4d65fc3b65..492fb0a4d86ba565db62a27f566d4437601790d2 100644 --- a/data/constants.py +++ b/data/constants.py @@ -15,7 +15,7 @@ PADDLE_COLLISION_DAMPING = .66 # Factor to scale the velocity with when the padd FRAMERATE = 60 # Frames per second -PTPF = 8 # Physics ticks per frame +PTPF = 10 # Physics ticks per frame DELTA_TIME = (1 / FRAMERATE) # Delta time for updates PROJECT_PATH: Path = Path(__file__).parents[1] # Path to the project directory diff --git a/data/data.png b/data/data.png index af087751117087b6c797315bb33c3d10aa53c351..1fd415f9451e7d5dbe6868ea6da0a6d8f47fefa2 100644 Binary files a/data/data.png and b/data/data.png differ diff --git a/data/options.json b/data/options.json index 3c9ca101c5bc0a06bff94f585fd2f2722556bb9f..86a52f53ce4ca70c5989903c4206efda42ab4425 100644 --- a/data/options.json +++ b/data/options.json @@ -1 +1 @@ -{"asf": 1.0, "master_volume": 50, "music_volume": 50, "sfx_volume": 50, "user_name": "Player"} \ 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 diff --git a/source/api/components/textur_renderer.py b/source/api/components/textur_renderer.py new file mode 100644 index 0000000000000000000000000000000000000000..8919a0d53dfe01456a8e957a4f910de8b373a53f --- /dev/null +++ b/source/api/components/textur_renderer.py @@ -0,0 +1,111 @@ +from pathlib import Path +import pygame +from source.api.components.component import Component + +from source.api.components.mesh import CircleMesh, Mesh, PolygonMesh + +class TexturRenderer(Component): + """ + The Renderer class is responsible for rendering the visual representation of an entity in the game scene. + + Attributes: + visible (bool): Whether the renderer is visible or not. + mesh (Mesh): The mesh component of the parent entity. + mesh_type (type): The type of the mesh component of the parent entity. + + Methods: + __init__(self, visible: bool = True) + on_init(self) + on_late_update(self, delta_time: float) + get_mesh(self) + serialize(self) -> dict + deserialize(self, data: dict) -> 'Renderer' + """ + + def __init__(self, textur_path: Path, width: float, height: float, visible: bool = True, alpha: int = 200) -> None: + """ + Initializes the renderer component. + + Arguments: + visible (bool, optional): Whether the renderer is visible or not. Defaults to True. + """ + + super().__init__() + + self.width = width + self.height = height + self.textur_path = textur_path + self.texture = pygame.image.load(textur_path).convert_alpha() + self.texture = pygame.transform.scale(self.texture, (int(width), int(height))) + self.texture.set_alpha(alpha) + self.visible: bool = visible + self.screen = pygame.display.get_surface() + + def on_init(self) -> None: + """ + Called when the renderer component is initialized. + + Returns: + None + """ + self.rotate(self.parent.transform.rot.get_value()) + self.parent.transform.rot.subscribe(self.rotate) + return super().on_init() + + def on_late_update(self, delta_time: float) -> None: + """ + Called when the renderer component is updated. + + Arguments: + delta_time (float): The time since the last frame. + + Returns: + None + """ + self.screen.blit(self.rotated_texture, self.rotated_rect.topleft) + return super().on_update(delta_time) + + + def rotate(self, angle: float) -> None: + """ + Rotates the renderer component by the given angle. + + Arguments: + angle (float): The angle to rotate the renderer by. + + Returns: + None + """ + self.rotated_texture = pygame.transform.rotate(self.texture, -angle) + self.rotated_rect = self.rotated_texture.get_rect(center = self.texture.get_rect(center = (self.parent.transform.pos.x, self.parent.transform.pos.y)).center) + + def serialize(self) -> dict: + """ + Serializes the renderer component data into a dictionary. + + Returns: + dict: The serialized renderer data. + """ + return { + "textur_path": self.textur_path, + "width": self.width, + "height": self.height, + "visible": self.visible + } + + def deserialize(self, data: dict) -> 'TexturRenderer': + """ + Deserializes the renderer component data from a dictionary. + + Arguments: + data (dict): The serialized renderer data. + + Returns: + Renderer: The deserialized renderer component. + """ + self.textur_path = data["textur_path"] + self.width = data["width"] + self.height = data["height"] + self.visible = data["visible"] + self.texture = pygame.image.load(self.textur_path).convert_alpha() + return self \ No newline at end of file diff --git a/source/game/objects/spring.py b/source/game/objects/spring.py index c50eb839f53eaf59285f82384235406e082cefdb..3b4b7e90cc7b13b7f129d8171dac630d79a04d7c 100644 --- a/source/game/objects/spring.py +++ b/source/game/objects/spring.py @@ -5,6 +5,7 @@ from source.api.components.change_score import ChangeScore from source.api.components.collider import PolygonCollider from source.api.components.mesh import PolygonMesh from source.api.components.renderer import Renderer +from source.api.components.textur_renderer import TexturRenderer from source.api.management.options_manager import OptionsManager from source.api.objects.game_object import GameObject from data.constants import ASSETS_PATH @@ -40,6 +41,8 @@ class Spring(GameObject): super().__init__(pos, 0, scene) self.spring_sound: pygame.mixer.Sound = pygame.mixer.Sound(ASSETS_PATH / Path("sounds/spring.wav")) + textur_path = ASSETS_PATH / Path("images/objects/spring.png") + rel_points = [ Vector2(-width/2, -height/2), Vector2(width/2, -height/2), @@ -50,7 +53,7 @@ class Spring(GameObject): self.add_components( PolygonMesh(color, rel_points), PolygonCollider(is_trigger=True), - Renderer(), + TexturRenderer(textur_path, width, height), self.change_score ) diff --git a/source/game/scenes/main_pinball.py b/source/game/scenes/main_pinball.py index 0e83b522ab5eb48fcc9c695513bda2c220a3b8e3..cf2ced6964f3fffb03ccfb9c3c4022c12a47b7f0 100644 --- a/source/game/scenes/main_pinball.py +++ b/source/game/scenes/main_pinball.py @@ -221,20 +221,20 @@ class MainPinball(Scene): # springs # left - self.add_gameobject(Spring(self, V2(23, 700)*asf, width=12*asf, height=50 * + self.add_gameobject(Spring(self, V2(23, 700)*asf, width=10*asf, height=50 * asf, color=Color(150, 150, 150), add_to_score=25)) - self.add_gameobject(Spring(self, V2(70, 650)*asf, width=12*asf, height=50 * + self.add_gameobject(Spring(self, V2(70, 650)*asf, width=10*asf, height=50 * asf, color=Color(150, 150, 150), add_to_score=25)) # right - self.add_gameobject(Spring(self, V2(580, 700)*asf, width=12*asf, height=50 * + self.add_gameobject(Spring(self, V2(580, 700)*asf, width=10*asf, height=50 * asf, color=Color(150, 150, 150), add_to_score=25)) - self.add_gameobject(Spring(self, V2(532, 650)*asf, width=12*asf, height=50 * + self.add_gameobject(Spring(self, V2(532, 650)*asf, width=10*asf, height=50 * asf, color=Color(150, 150, 150), add_to_score=25)) # center - self.add_gameobject(Spring(self, V2(300, 800)*asf, width=12*asf, height=50 * + self.add_gameobject(Spring(self, V2(300, 800)*asf, width=10*asf, height=50 * asf, color=Color(150, 150, 150), add_to_score=25)) # tube - self.add_gameobject(Spring(self, V2(70, 493)*asf, width=12*asf, height=50*asf, + self.add_gameobject(Spring(self, V2(70, 493)*asf, width=10*asf, height=50*asf, color=Color(150, 150, 150), add_to_score=25, rotation=-35)) # target