From: Skullheadx Date: Fri, 14 Apr 2023 00:04:47 +0000 (-0400) Subject: Text X-Git-Url: http://git.skullheadx.com/projects/dotfiles.html?a=commitdiff_plain;h=5c6bfd5301bb67f416c36fe48f0758e0b1f13806;p=PygameGUIEngine.git Text --- diff --git a/main.py b/main.py index 6bd6bd2..d454a3c 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,7 @@ import pygame +from color import Color from rectangle import Rectangle +from text import Text pygame.init() @@ -8,10 +10,10 @@ pygame.display.set_caption("My Game") clock = pygame.time.Clock() delta = 0 -# Testing Rectangle -r = Rectangle(100, 100, 100, 100, (255, 0, 0)) -r2 = Rectangle(200, 200, 100, 100, (0, 255, 0), (0, 0, 255), 5, 10) -r3 = Rectangle(300, 300, 100, 100, (0, 0, 255), (255, 0, 0), 5, 10) +# Testing Text +a = Text(100, 100, "Hello World", "Arial", 20, color=Color.RED) +b = Text(100, 200, "Word hunt", "Imprint Shadow", 20) +c = Rectangle(100, 300, 100, 100, (255, 0, 0)) is_running = True while is_running: @@ -21,10 +23,11 @@ while is_running: screen.fill((255, 255, 255)) - r.draw(screen) - r2.draw(screen) - r3.move(delta * 10, delta * 10) - r3.draw(screen) + a.draw(screen) + b.move(delta * 10, delta * 10) + b.set_text("Word hunt " + str(round(clock.get_fps())) + " FPS") + b.draw(screen) + c.draw(screen) pygame.display.update() delta = clock.tick(60) / 1000 # Seconds since last frame diff --git a/text.py b/text.py new file mode 100644 index 0000000..32f3384 --- /dev/null +++ b/text.py @@ -0,0 +1,43 @@ +import pygame +from color import Color +from rectangle import Rectangle + + +class Text: + + def __init__(self, x, y, text, font, font_size, color=Color.BLACK): + self.x = x + self.y = y + self.text = text + self.font_size = font_size + self.font = pygame.font.SysFont(font, self.font_size) + self.color = color + + self.text_surface = self.font.render(self.text, True, self.color) + + def update(self): + self.text_surface = self.font.render(self.text, True, self.color) + + def set_position(self, x, y): + self.x = x + self.y = y + + def move(self, dx, dy): + self.x += dx + self.y += dy + + def set_text(self, text): + self.text = text + self.update() + + def set_font(self, font, font_size): + self.font_size = font_size + self.font = pygame.font.SysFont(font, font_size) + self.update() + + def set_color(self, color): + self.color = color + self.update() + + def draw(self, screen): + screen.blit(self.text_surface, (self.x, self.y))