how to make pygame display random numbers? Of course, the goal is pursued by another, but I would like to understand this example.

import pygame from pygame.locals import * pygame.init() getSize = pygame.display.Info() screen = pygame.display.set_mode((getSize.current_w, getSize.current_h))#, pygame.FULLSCREEN) w = getSize.current_w h = getSize.current_h pygame.display.set_caption(''); fontSize = 20 myFont = pygame.font.SysFont('Calibri', fontSize) fontColor = (255,255,255) bgColor = (0,0,0) text = 'blablabla' fontImage = myFont.render(str(text), 9, (fontColor)) mainLoop = True while mainLoop: for event in pygame.event.get(): if event.type == QUIT: mainLoop = False screen.fill(bgColor) screen.blit(fontImage, (w / 2 - 50, h / 2)) pygame.display.update() pygame.quit() 

let's say so - how to connect these two pieces of code? I can not understand

 import random import time a = random.randrange(1, 100) while a > 0: print(a) time.sleep(1) a = random.randrange(1, 100) 
  • Just like any other text. - Enikeyschik pm

1 answer 1

First, initialize pygame.font, and then render

 pygame.font.init() #1 labelFont = pygame.font.SysFont('Monaco', 30) rendersurface = labelFont.render('your text or number', False, (0, 0, 0)) screen.blit(labelFont, (0, 0)) 

Shove the last two actions into a loop and substitute a random number instead of text.

R1: If you have already called

 pygame.init() 

it is not necessary to add the first line.