Python Forum
[pyGame] More Text in one Window, example needed !
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[pyGame] More Text in one Window, example needed !
#1
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.
Reply
#2
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])
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Cannot display anything inside pygame window the_gullwing 5 1,113 Dec-07-2023, 02:44 AM
Last Post: Benixon
  [PyGame] drawing images onto pygame window djwilson0495 1 3,483 Feb-22-2021, 05:39 PM
Last Post: nilamo
  pygame get window position vskarica 3 5,920 Oct-18-2020, 05:14 PM
Last Post: nilamo
  Unable to exit Pygame window Hesper 2 3,871 Mar-30-2020, 08:59 AM
Last Post: Hesper
  [pyGame] How do i change text with keyboard ?... JamieVanCadsand 1 4,532 Oct-03-2017, 10:05 AM
Last Post: metulburr
  [pyGame] Render Text from Variable, examples needed ! JamieVanCadsand 1 5,929 Oct-03-2017, 09:54 AM
Last Post: metulburr
  [pyGame] Text with Input, help needed ! JamieVanCadsand 1 6,329 Oct-02-2017, 06:53 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020