Python Forum

Full Version: [pyGame] More Text in one Window, example needed !
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey Game Developers...

I try to create more tekst fonts in one game window, in one script...
But it don't works...

This is my script:
from datetime import datetime

import pygame
import sys

pygame.init()

size = (640, 480)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Clock")

WHITE = (255, 255, 255)
GREEN = (0, 255, 0)

done = False
clock = pygame.time.Clock()

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                done = True



    
    screen.fill(WHITE)

    now = datetime.now()
    
    font1 = pygame.font.SysFont('Calibri', 25, True, False)
    text1 = font.render('Sec: ' + str(now.second), True, GREEN)
    screen.blit(text1, [320, 240])
    
    font2 = pygame.font.SysFont('Calibri', 25, True, False)
    text2 = font.render('Min: ' + str(now.minute), True, GREEN)
    screen.blit(text2, [320, 230])

    font3 = pygame.font.SysFont('Calibri', 25, True, False)
    text3 = font.render('Hou: ' + str(now.hour), True, GREEN)
    screen.blit(text3, [320, 220])
    
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
sys.exit()
Pygame don't run my game perfect, my screen is black... please whats wrong with my code ?...
Can anyone give my an example code to lost this problem ?...

Thanks, Jamie.
If i run your script i get this error
Error:
Traceback (most recent call last): File "test5.py", line 34, in <module> text1 = font.render('Sec: ' + str(now.second), True, GREEN) NameError: name 'font' is not defined
You seemed to copy and paste each line and forgot to change font{NUM}
    font1 = pygame.font.SysFont('Calibri', 25, True, False)
    text1 = font1.render('Sec: ' + str(now.second), True, GREEN)
    screen.blit(text1, [320, 240])
     
    font2 = pygame.font.SysFont('Calibri', 25, True, False)
    text2 = font2.render('Min: ' + str(now.minute), True, GREEN)
    screen.blit(text2, [320, 230])
 
    font3 = pygame.font.SysFont('Calibri', 25, True, False)
    text3 = font3.render('Hou: ' + str(now.hour), True, GREEN)
    screen.blit(text3, [320, 220])