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

implemented point feedback

parent dbb42321
No related branches found
No related tags found
No related merge requests found
from pygame import Vector2
from pygame import Color, Surface, Vector2
import pygame
from constants import DEFAULT_FONT
from source.api.components.component import Component
from source.api.components.mesh import CircleMesh, Mesh, PolygonMesh
from source.api.management.options_manager import OptionsManager
from source.api.objects.game_object import GameObject
from pygame.freetype import Font
class TextObject:
"""
This class should only be used as a parameter for the ChangeScore class.
Arguments:
alpha (int): The transparency value of the text object.
pos (Vector2): The position of the text object.
Attributes:
alpha (int): The transparency value of the text object.
pos (Vector2): The position of the text object.
"""
def __init__(self, alpha: int, pos: Vector2) -> None:
"""
Initialize the ChangeScore component.
Args:
alpha (int): The alpha value.
pos (Vector2): The position vector.
"""
self.alpha = alpha
self.pos = pos
class ChangeScore(Component):
......@@ -8,39 +38,99 @@ class ChangeScore(Component):
A class to represent a ChangeScore. A ChangeScore is a component that changes the score by a given amount when it is hit.
Attributes:
add_to_score: int, score to add
add_to_score (int): The score to add when a collision occurs.
speed (float): The speed at which the score text moves upwards.
alpha_decrease (int): The amount by which the alpha of the score text decreases each frame.
font (pygame.font.Font): The font used to render the score text.
text_surface (pygame.Surface): The pre-rendered text surface.
text_objects (list[TextObject]): The list of text objects currently being displayed.
Methods:
__init__(self, add_to_score: int = 10)
__init__(self, add_to_score: int = 10, speed: float = 1.0, alpha_decrease: int = 1)
on_collision(self, other: GameObject, point: Vector2, normal: Vector2)
on_update(self, delta_time: float) -> None
serialize(self) -> dict
deserialize(self, data: dict) -> 'ChangeScore'
"""
def __init__(self, add_to_score: int = 10) -> None:
def __init__(self, add_to_score: int = 10, show_text: bool = False, text_size: float = 1, speed: float = 1, alpha_decrease: int = 10, text_color: Color = Color(255, 255, 255)) -> None:
"""
Inits ChangeScore with add_to_score
Inits ChangeScore with add_to_score, speed, and alpha_decrease.
Arguments:
add_to_score: int, score to add
add_to_score (int): The score to add when a collision occurs.
speed (float): The speed at which the score text moves upwards.
alpha_decrease (int): The amount by which the alpha of the score text decreases each frame.
"""
self.add_to_score = add_to_score
super().__init__()
self.add_to_score = add_to_score
self.speed = speed
self.alpha_decrease = alpha_decrease
self.show_text = show_text
if not self.show_text:
return
self.font = Font(DEFAULT_FONT, 20*OptionsManager().asf * text_size)
text = str(self.add_to_score)
self.text_size = self.font.get_rect(text).size
self.text_surface = pygame.Surface(self.text_size, pygame.SRCALPHA)
self.font.render_to(self.text_surface, (0, 0), text, text_color)
self.text_objects: list[TextObject] = []
def on_init(self) -> None:
"""
Called when the renderer component is initialized.
Returns:
None
"""
self.get_mesh()
if self.mesh_type == PolygonMesh:
self.cms = (sum(x for x, _ in self.mesh.points) / len(self.mesh.points),
sum(y for _, y in self.mesh.points) / len(self.mesh.points))
if self.mesh_type == CircleMesh:
self.cms = None
return super().on_init()
def on_collision(self, other: GameObject, point: Vector2, normal: Vector2):
"""
Adds add_to_score to the score
Adds add_to_score to the score and creates a new text object.
Arguments:
other: GameObject, the other object
point: Vector2, the point of collision
normal: Vector2, the normal of the collision
other (GameObject): The other object.
point (Vector2): The point of collision.
normal (Vector2): The normal of the collision.
"""
self.parent.scene.score += self.add_to_score
if not self.show_text:
return super().on_collision(other, point, normal)
text_object = TextObject(255, Vector2(self.cms if self.cms else self.parent.transform.pos))
self.text_objects.append(text_object)
return super().on_collision(other, point, normal)
def on_late_update(self, delta_time: float) -> None:
"""
Moves the text objects upwards and decreases their alpha.
Arguments:
delta_time (float): The time since the last frame.
Returns:
None
"""
if not self.show_text:
return super().on_late_update(delta_time)
for text_object in self.text_objects:
text_object.pos.y -= self.speed
text_object.alpha -= self.alpha_decrease
self.text_surface.set_alpha(text_object.alpha)
self.parent.scene.screen.blit(self.text_surface, text_object.pos - Vector2(self.text_size) / 2)
self.text_objects = [text_object for text_object in self.text_objects if text_object.alpha > 0]
return super().on_late_update(delta_time)
def serialize(self) -> dict:
"""
Serializes the ChangeScore
......@@ -64,4 +154,19 @@ class ChangeScore(Component):
ChangeScore: the modified ChangeScore instance
"""
self.add_to_score = data["add_to_score"]
return self
\ No newline at end of file
return self
def get_mesh(self) -> None:
"""
Gets the mesh of the parent.
Returns:
None
"""
mesh = self.parent.get_component_by_class(Mesh)
if not mesh:
raise Exception(f"No Mesh found on {self.parent}")
self.mesh = mesh
self.mesh_type = type(mesh)
......@@ -7,10 +7,9 @@ from source.api.ui.button_style import ButtonStyle
from source.api.ui.ui_element_base import UIElementBase
from constants import ASSETS_PATH, DEFAULT_BUTTON_STYLE, DEFAULT_FONT
class TextObject:
"""
A class to represent a text object. Used by the Panel class as data structure.
This class should only be used as a parameter for the Panel class.
Attributes:
text (str): The text to display.
......
from pathlib import Path
from pygame import Color, Vector2
import pygame
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
......@@ -36,8 +37,6 @@ class Spring(GameObject):
"""
super().__init__(pos, 0, scene)
self.add_to_score = add_to_score
self.spring_sound: pygame.mixer.Sound = pygame.mixer.Sound(ASSETS_PATH / Path("sounds/spring.wav"))
rel_points = [
......@@ -46,10 +45,12 @@ class Spring(GameObject):
Vector2(width/2, height/2),
Vector2(-width/2, height/2)
]
self.change_score = ChangeScore(add_to_score, True, 1.5, 2)
self.add_components(
PolygonMesh(color, rel_points),
PolygonCollider(is_trigger=True),
Renderer()
Renderer(),
self.change_score
)
self.transform.rotate(rotation)
......@@ -65,6 +66,6 @@ class Spring(GameObject):
None
"""
self.scene.score += self.add_to_score
self.change_score.on_collision(self, Vector2(), Vector2())
self.sound_manager.play_sfx(self.spring_sound)
return super().on_trigger_enter(other)
\ No newline at end of file
......@@ -198,20 +198,20 @@ class MainPinball(Scene):
# bumpers
self.add_gameobject(CircleWall(self, V2(320, 420)*asf, 40*asf, color=Color(255, 0, 0), hit_sound=bumper_sound01
).add_components(Bumper(bumper_strength), ChangeScore(200), ScaleRenderer(scale_duration, scale_strength)))
).add_components(Bumper(bumper_strength), ScaleRenderer(scale_duration, scale_strength), ChangeScore(200, True, 2, 2)))
self.add_gameobject(CircleWall(self, V2(388, 292)*asf, 35*asf, color=Color(240, 212, 88), hit_sound=bumper_sound01
).add_components(Bumper(bumper_strength), ChangeScore(100), ScaleRenderer(scale_duration, scale_strength)))
).add_components(Bumper(bumper_strength), ScaleRenderer(scale_duration, scale_strength), ChangeScore(100, True, 2, 2)))
self.add_gameobject(CircleWall(self, V2(250, 282)*asf, 30*asf, color=Color(100, 201, 231), hit_sound=bumper_sound01
).add_components(Bumper(bumper_strength), ChangeScore(50), ScaleRenderer(scale_duration, scale_strength)))
).add_components(Bumper(bumper_strength), ScaleRenderer(scale_duration, scale_strength), ChangeScore(50, True, 2, 2)))
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
bumper_strength), ScaleRenderer(scale_duration, scale_strength, True), ChangeScore(150, True, 2, 2), SimpleMovement(V2(250, 700)*asf, V2(350, 700)*asf, .75)))
# life 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), Color(2, 132, 207)]
self.add_gameobject(CircleWall(self, V2(25, 760)*asf, 15*asf, ).add_components(
Bumper((bumper_strength[0]*2, bumper_strength[1]*2)), ChangeScore(50), ScaleRenderer(scale_duration, scale_strength), LifeTimer(colors, 6)))
Bumper((bumper_strength[0]*2, bumper_strength[1]*2)), ScaleRenderer(scale_duration, scale_strength), ChangeScore(50, True, 2, 2), LifeTimer(colors, 6)))
self.add_gameobject(CircleWall(self, V2(578, 760)*asf, 15*asf).add_components(
Bumper((bumper_strength[0]*2, bumper_strength[1]*2)), ChangeScore(50), ScaleRenderer(scale_duration, scale_strength), LifeTimer(colors, 6)))
Bumper((bumper_strength[0]*2, bumper_strength[1]*2)), ScaleRenderer(scale_duration, scale_strength), ChangeScore(50, True, 2, 2), LifeTimer(colors, 6)))
# teleporter
rel_points = list(map(lambda x: utils.ceil_vector(x*asf),
......
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