Merge remote-tracking branch 'origin/main' into main

main
Yan Wittmann 2023-03-25 18:14:56 +01:00
commit 3e622e272b
3 changed files with 32 additions and 14 deletions

Binary file not shown.

View File

@ -12,7 +12,7 @@ from sprite.Sprite import Sprite
from sprite.StaticSprite import StaticSprite from sprite.StaticSprite import StaticSprite
from ui_elements.TextLabel import TextLabel from ui_elements.TextLabel import TextLabel
what_to_run = 'physics' what_to_run = 'textlabel'
def apply_frame_rate(number: float): def apply_frame_rate(number: float):
@ -84,11 +84,13 @@ elif what_to_run == 'textlabel':
screen_transform = PositionScale((0, 0), (4, 4)) screen_transform = PositionScale((0, 0), (4, 4))
pygame.init() pygame.init()
screen = pygame.display.set_mode((300, 300)) screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption("PM GAME") pygame.display.set_caption("PM GAME")
clock = pygame.time.Clock() clock = pygame.time.Clock()
test = TextLabel("Das ist ein Text", 100, 100, 50, Font('data/font/MilkyNice.otf', 50)) test1 = TextLabel("Das ist ein Test", 400, 0, 50, alignment="left")
test2 = TextLabel("Das ist ein Test", 400, 10, 50, alignment="center")
test3 = TextLabel("Das ist ein Test", 400, 20, 50, alignment="right")
while True: while True:
clock.tick(5) clock.tick(5)
@ -97,9 +99,12 @@ elif what_to_run == 'textlabel':
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
pygame.quit() pygame.quit()
screen.fill((0, 0, 0)) screen.fill((0, 0, 110))
test1.draw(screen, screen_transform)
test2.draw(screen, screen_transform)
test3.draw(screen, screen_transform)
test.draw(screen, screen_transform)
pygame.display.update() pygame.display.update()

View File

@ -1,12 +1,12 @@
import pygame import pygame
from pygame import Surface from pygame import Surface, Rect
from pygame.font import Font from pygame.font import Font
from sprite.PositionScale import PositionScale from sprite.PositionScale import PositionScale
class TextLabel: class TextLabel:
def __init__(self, text: str, x_position: float, y_position: float, font_size: float, font: Font, def __init__(self, text: str, x_position: float, y_position: float, font_size: int,
alignment: str = "left"): alignment: str = "left"):
self.text = text self.text = text
self.x_position = x_position self.x_position = x_position
@ -15,20 +15,33 @@ class TextLabel:
self.current_width = 0 self.current_width = 0
self.current_height = 0 self.current_height = 0
self.font_size = font_size self.font_size = font_size
self.font = font self.font = Font('data/font/MilkyNice.ttf', self.font_size)
self.position_scale = PositionScale() self.position_scale = PositionScale()
def draw(self, screen: Surface, screen_transform: PositionScale): def draw(self, screen: Surface, screen_transform: PositionScale):
rendered_font = self.font.render(str(self.text), True, (255, 255, 255)) rendered_font = self.font.render(str(self.text), True, pygame.Color('white'))
self.current_width = rendered_font.get_width() self.current_width = rendered_font.get_width()
self.current_height = rendered_font.get_height() self.current_height = rendered_font.get_height()
if self.alignment == "right": if self.alignment == "right":
screen.blit(rendered_font, (self.x_position - self.current_width / 2, self.y_position)) screen.blit(rendered_font, (self.x_position - self.current_width, self.y_position))
elif self.alignment == "right": elif self.alignment == "left":
screen.blit(rendered_font, (self.x_position, self.y_position)) screen.blit(rendered_font, (self.x_position, self.y_position))
elif self.alignment == "center": elif self.alignment == "center":
screen.blit(rendered_font, (self.x_position - self.current_width, self.y_position)) screen.blit(rendered_font, (self.x_position - self.current_width / 2, self.y_position))
target_scale = (screen_transform.scale[0] * self.position_scale.scale[0],
screen_transform.scale[1] * self.position_scale.scale[1])
target_position = ((self.position_scale.position[0] + screen_transform.position[0]) * target_scale[0],
(self.position_scale.position[1] + screen_transform.position[1]) * target_scale[1])
target_size = (int(target_scale[0] * self.x_position),
int(target_scale[1] * self.y_position))
target_image = pygame.transform.scale(rendered_font, target_size)
def set_text(self, new_text: str): def set_text(self, new_text: str):
self.text = new_text self.text = new_text