ChanceMeteor515
ChanceMeteor515

Reputation: 47

Python3.10, Text Doesn't Show On Pygame Window

I am trying to make a app where you enter in the minute value then a timer pops up on a window. The timer isnt appearing. I am using python 3.10. I'm new to python so please if you can fix it, please include the source code.

import pygame
import time
import sys

pygame.init()

q = True
WIDTH = 800
HEIGHT = 600

clock = pygame.time.Clock()

myFont = pygame.font.SysFont("monospace", 35)

def countdown(t):
    t *= 60
    while t:
        min, sec = divmod(t, 60)
        timer = '{:02d}:{:02d}'.format(min, sec)
#
        screen = pygame.display.set_mode((WIDTH, HEIGHT))
        pygame.display.set_caption('PyTimerTwo,  By: ChanceMeteor515')
        screen.fill((0, 0, 0))
        text = str(timer)
        label = myFont.render(text, 1, (255, 255, 255))
        screen.blit(label, (WIDTH - 200, HEIGHT - 40))
#
    print("\nTime's Up")
    time.sleep(5400)

t = input("Enter Time In Minutes:")
countdown(int(t))

while q:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_h:
                pygame.mouse.set_visible(0)
            elif event.key == pygame.K_ESCAPE:
                sys.exit()

    clock.tick(120)
    pygame.display.update()

Upvotes: 0

Views: 170

Answers (1)

Roni
Roni

Reputation: 649

In your countdown function, you forgot to update the display with pygame.display.update() and you also forgot to add an event loop, so you won't be able to close the window. If you add these, the timer will appear.

Modified Code:

import pygame
import time
import sys

pygame.init()

q = True
WIDTH = 800
HEIGHT = 600

clock = pygame.time.Clock()

myFont = pygame.font.SysFont("monospace", 35)

def countdown(t):
    t *= 60
    while t:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        min, sec = divmod(t, 60)
        timer = '{:02d}:{:02d}'.format(min, sec)
#
        screen = pygame.display.set_mode((WIDTH, HEIGHT))
        pygame.display.set_caption('PyTimerTwo,  By: ChanceMeteor515')
        screen.fill((0, 0, 0))
        text = str(timer)
        label = myFont.render(text, True, (255, 255, 255))
        screen.blit(label, (WIDTH - 200, HEIGHT - 40))

        pygame.display.update()
#
    print("\nTime's Up")
    time.sleep(5400)

t = input("Enter Time In Minutes:")
countdown(int(t))

while q:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_h:
                pygame.mouse.set_visible(False)
            elif event.key == pygame.K_ESCAPE:
                sys.exit()

    clock.tick(120)
    pygame.display.update()

Upvotes: 1

Related Questions