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

added life_timer_objects

parent 61652105
No related branches found
No related tags found
No related merge requests found
from pathlib import Path
from pygame import Color, Vector2
import pygame
from api.components.component import Component
from api.components.mesh import Mesh
from api.objects.game_object import GameObject
from constants import ASSETS_PATH
class LifeTimer(Component):
def __init__(self, colors: list[Color], lives: int = 10, hit_sound = None):
super().__init__()
self.colors = colors
self.lives = lives
self.hit_sound: pygame.mixer.Sound = hit_sound # type: ignore
if not self.hit_sound:
self.hit_sound = pygame.mixer.Sound(ASSETS_PATH / Path("sounds/bumper01.wav"))
def on_init(self) -> None:
self.mesh: Mesh = self.parent.get_component_by_class(Mesh) # type: ignore
if not self.mesh:
raise Exception("No mesh component found")
return super().on_init()
def on_collision(self, other: GameObject, point: Vector2, normal: Vector2) -> None:
self.lives -= 1
if self.lives < 0:
self.parent.on_destroy()
self.mesh.color = self.colors[self.lives]
self.parent.sound_manager.play_sfx(self.hit_sound)
return super().on_collision(other, point, normal)
def serialize(self) -> dict:
return {
"colors": self.colors,
"lives": self.lives
}
def deserialize(self, data: dict) -> 'LifeTimer':
self.colors = data["colors"]
self.lives = data["lives"]
return self
\ No newline at end of file
......@@ -43,4 +43,4 @@ class Renderer(Component):
}
def deserialize(self, data: dict) -> 'Renderer':
return self
return self
\ No newline at end of file
{"scoreboard": {"Player": 9225}}
\ No newline at end of file
{"scoreboard": {"Player": 6975}}
\ No newline at end of file
......@@ -5,6 +5,7 @@ import pygame
from pygame.event import Event
from api.components.bumper import Bumper
from api.components.change_score import ChangeScore
from api.components.life_timer import LifeTimer
from api.components.scale_renderer import ScaleRenderer
from api.components.simple_movement import SimpleMovement
from api.components.tray import Tray
......@@ -158,6 +159,13 @@ class MainPinball(Scene):
).add_components(Bumper(bumper_strength), ChangeScore(50), ScaleRenderer(scale_duration, scale_strength)))
self.add_gameobject(CircleWall(self, V2(300, 700)*asf, 20*asf, color=Color(100, 201, 231), hit_sound=bumper_sound01).add_components(Bumper(
bumper_strength), ChangeScore(150), ScaleRenderer(scale_duration, scale_strength, True), SimpleMovement(V2(250, 700)*asf, V2(350, 700)*asf, .75)))
# live bumpers
colors: list[Color] = [Color(54, 54, 54), Color(63, 75, 77), Color(64, 92, 97), Color(59, 108, 117), Color(48, 130, 145), Color(28, 151, 173), Color(6, 165, 194)]
self.add_gameobject(CircleWall(self, V2(25, 760)*asf, 15*asf, color=Color(2, 132, 207)).add_components(
Bumper(bumper_strength), ChangeScore(50), ScaleRenderer(scale_duration, scale_strength), LifeTimer(colors, 7)))
self.add_gameobject(CircleWall(self, V2(575, 760)*asf, 15*asf, color=Color(2, 132, 207)).add_components(
Bumper(bumper_strength), ChangeScore(50), ScaleRenderer(scale_duration, scale_strength), LifeTimer(colors, 7)))
# teleporter
rel_points = list(map(lambda x: utils.ceil_vector(x*asf),
......@@ -166,15 +174,15 @@ class MainPinball(Scene):
# springs
# left
self.add_gameobject(Spring(self, V2(20, 750)*asf, width=15*asf, height=50*asf, color=Color(150, 150, 150), add_to_score=25))
self.add_gameobject(Spring(self, V2(70, 700)*asf, width=15*asf, height=50*asf, color=Color(150, 150, 150), add_to_score=25))
self.add_gameobject(Spring(self, V2(20, 700)*asf, width=12*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*asf, color=Color(150, 150, 150), add_to_score=25))
# right
self.add_gameobject(Spring(self, V2(580, 750)*asf, width=15*asf, height=50*asf, color=Color(150, 150, 150), add_to_score=25))
self.add_gameobject(Spring(self, V2(540, 700)*asf, width=15*asf, height=50*asf, color=Color(150, 150, 150), add_to_score=25))
self.add_gameobject(Spring(self, V2(580, 700)*asf, width=12*asf, height=50*asf, color=Color(150, 150, 150), add_to_score=25))
self.add_gameobject(Spring(self, V2(540, 650)*asf, width=12*asf, height=50*asf, color=Color(150, 150, 150), add_to_score=25))
# center
self.add_gameobject(Spring(self, V2(300, 800)*asf, width=15*asf, height=50*asf, color=Color(150, 150, 150), add_to_score=25))
self.add_gameobject(Spring(self, V2(300, 800)*asf, width=12*asf, height=50*asf, color=Color(150, 150, 150), add_to_score=25))
# tube
self.add_gameobject(Spring(self, V2(70, 493)*asf, width=15*asf, height=50*asf, color=Color(150, 150, 150), add_to_score=25, rotation=-35))
self.add_gameobject(Spring(self, V2(70, 493)*asf, width=12*asf, height=50*asf, color=Color(150, 150, 150), add_to_score=25, rotation=-35))
# text
self.score_text = Text(self.screen, (.01, .01), (0, 0), text=f"Score: {self.score}", font_size=50*asf)
......
{"acf": 1.5, "master_volume": 58, "music_volume": 26, "sfx_volume": 63}
\ No newline at end of file
{"acf": 1.5, "master_volume": 45, "music_volume": 26, "sfx_volume": 63}
\ No newline at end of file
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