import pygame import pygame.freetype import threading from time import sleep class Alert(): def __init__(self, text, display): self.text = text font = pygame.freetype.Font(None, 24) font_fg = (255, 255, 255) font_bg = (255, 255, 255, 120) text_image = font.render(text, font_fg)[0] self.background = pygame.Surface((text_image.get_width() + 12, text_image.get_height() + 12), pygame.SRCALPHA) self.background.fill(font_bg) self.rect = self.background.get_rect() text_rect = text_image.get_rect(center = self.rect.center) self.background.blit(text_image, text_rect) self.rect.topleft = (display.get_width() / 2 - self.background.get_width() / 2, display.get_height() - self.background.get_height() - 8) def draw(self, surface): surface.blit(self.background, self.rect) def start_hide_thread(self, alerts, delay): def hide_delay(delay): sleep(delay) try: alerts.pop(alerts.index(self)) except ValueError: pass p = threading.Thread(target=hide_delay, args=(delay,)) p.daemon = True p.start()