Tawnymender12


Code/Games

Home Code/Games About me

20 20 20 rule timer

a python executable timer for the 20 20 20 rule

download


The code:


        import pygame, sys

        pygame.init()

        window_size = [200, 100]
        screen = pygame.display.set_mode(window_size)
        pygame.display.set_caption("20 20 20 rule")
        icon = pygame.image.load("data/icon.png")
        pygame.display.set_icon(icon)
        clock = pygame.time.Clock()

        def write(font_path: str, font_size, text: str, antialias: bool, color: str, surface, pos=(0, 0)):
            font = pygame.font.Font(font_path, font_size)
            rendered_text = font.render(text, antialias, color)
            rendered_text_rect = rendered_text.get_rect(center = (pos))
            surface.blit(rendered_text, rendered_text_rect)

        def mins():
            time = 1200

            pygame.mixer.music.load('data/alarm_sound.mp3')

            while 1:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit()
                        sys.exit()

                screen.fill("black")
                if len(str(int(divmod((time), 60)[1]))) > 1: 
                    write(None, 50, f'{int(divmod((time), 60)[0])}:{int(divmod((time), 60)[1])}', True, "cyan", screen, (window_size[0] / 2, window_size[1] / 2))
                else: 
                    write(None, 50, f'{int(divmod((time), 60)[0])}:0{int(divmod((time), 60)[1])}', True, "cyan", screen, (window_size[0] / 2, window_size[1] / 2))
                if time > 0: time -= 1

                if time <= 0:
                    pygame.mixer.music.play()
                    secs()

                pygame.display.flip()
                clock.tick(1)

        def secs():
            time = 20

            while 1:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit()
                        sys.exit()

                screen.fill("black")
                write(None, 50, f'{time}', True, "cyan", screen, (window_size[0] / 2, window_size[1] / 2))
                if time > 0: time -= 1

                if time <= 0:
                    mins()

                pygame.display.flip()
                clock.tick(1)

        mins()