Python Forum
[pyGame] Render Text from Variable, examples needed !
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[pyGame] Render Text from Variable, examples needed !
#1
Hey Game Developers...

How can i render tekst from as example an variable like an calculation, string or as example an
file open ?... I try this to learn working with pygame, i want to learn program my own software,
now i get an sum, calculated in python 3.6.2 and i try to render the calculation in pygame tekst...

This is my script, it don't works:
import pygame
import sys
 
pygame.init()
 
WHITE = (255, 255, 255)
RED = (0, 255, 0)
BLACK = (0, 0, 0)
 
size = (640, 480)
screen = pygame.display.set_mode(size)
 
pygame.display.set_caption("JCads Develop")
 
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)
    pygame.draw.line(screen, RED, [0, 0], [100, 100], 5)

    num1 = 36
    num2 = 84
    call = num1 * num2


    font = pygame.font.SysFont('Calibri', 25, True, False)
    text = font.render(call, True, BLACK)
    screen.blit(text, [250, 250])
    pygame.display.flip()
 
    clock.tick(60)
pygame.quit()
sys.exit()
Can anyone tell me how i can render tekst from variables like this sum or as example file.open,
strings, etc ?... this get i need for my future software, this is an simple test...

Can anyone give my an little many example codes ?...

Thanks, Jamie.




EDIT:

This is an in .txt writted script thad i want to try:
import pygame
import sys

pygame.init()

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

zise = (640, 480)
screen = pygame.display.set_mode(zise)

pygame.display.set_caption("JCads Develop")

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

    fle = open("myFile.txt", "r")

    fname = fle.name()
    fclos = fle.closed()

    screen.fill(WHITE)

    fle_text_basic = pygame.font.SysFont("calibri", 1, True, False)
    fle_text_name = pygame.font.SysFont("calibri", 1, False, False)
    fle_text_clos = pygame.font.SysFont("calibri", 1, False, False)

    fle_text_basic.render("FILE READING: ", True, BLACK)
    fle_text_name.render("Name: ".format(fname), True, BLACK)
    fle_text_clos.render("Closed: ".format(fclos), True, BLACK)

    screen.blit(fle_text_basic, (320, 240))
    screen.blit(fle_text_name, (320, 230))
    screen.blit(fle_text_clos, (320, 220))
    pygame.display.flip()

    clock.tick(60)

pygame.quit()
sys.exit()
Reply
#2
how much text is written in the file? If there is a lot, it will draw off the screen if you write it all at once. Here is an example of a credit screen. Note you should avoid using SysFont and use one that you put with the script instead. Also this was already written, so instead of reading it from text_list you would read it from a file. Credits just takes a list so if you read a file in via f.readlines(), you can use that in Credits parameters.
import pygame as pg
  
pg.init()
  
text_list = '''I'm Henry the eighth, I am
Henry the eighth, I am, I am
I got married to the widow next door
She's been married seven times before
  
And every one was an Henry (Henry)
She wouldn't have a Willy or a Sam (No Sam)
I'm her eighth old man, I'm Henry
Henry the eighth I am
  
Second verse, same as the first
  
I'm Henry the eighth, I am
Henry the eighth, I am, I am
I got married to the widow next door
She's been married seven times before
  
And every one was an Henry (Henry)
She wouldn't have a Willy or a Sam (No Sam)
I'm her eighth old man, I'm Henry
Henry the eighth I am
  
I'm Henry the eighth, I am
Henry the eighth, I am, I am
I got married to the widow next door
She's been married seven times before
  
And every one was an Henry (Henry)
She wouldn't have a Willy or a Sam (No Sam)
I'm her eighth old man, I'm Henry
Henry the eighth I am
  
H-E-N-R-Y
Henry (Henry)
Henry (Henry)
Henry the eighth I am, I am
Henry the eighth I am
Yeah!
'''.split('\n')
  
class Credits:
    def __init__(self, screen_rect, lst):
        self.srect = screen_rect
        self.lst = lst
        self.size = 16
        self.color = (255,0,0)
        self.buff_centery = self.srect.height/2 + 5
        self.buff_lines = 50
        self.timer = 0.0
        self.delay = 0
        self.make_surfaces()
  
  
    def make_text(self,message):
        font = pg.font.SysFont('Arial', self.size)
        text = font.render(message,True,self.color)
        rect = text.get_rect(center = (self.srect.centerx, self.srect.centery + self.buff_centery) )
        return text,rect
  
    def make_surfaces(self):
        self.text = []
        for i, line in enumerate(self.lst):
            l = self.make_text(line)
            l[1].y += i*self.buff_lines
            self.text.append(l)
  
    def update(self):
        if pg.time.get_ticks()-self.timer > self.delay:
            self.timer = pg.time.get_ticks()
            for text, rect in self.text:
                rect.y -= 1
  
    def render(self, surf):
        for text, rect in self.text:
            surf.blit(text, rect)
  
screen = pg.display.set_mode((800,600))
screen_rect = screen.get_rect()
clock = pg.time.Clock()
done = False
cred = Credits(screen_rect, text_list)
  
while not done:
    for event in pg.event.get(): 
        if event.type == pg.QUIT:
            done = True
    screen.fill((0,0,0))
    cred.update()
    cred.render(screen)
    pg.display.update()
    clock.tick(60)
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to render TAB in pygame lightframe109 1 2,973 Sep-03-2020, 09:49 PM
Last Post: metulburr
  [pyGame] More Text in one Window, example needed ! JamieVanCadsand 1 3,507 Oct-03-2017, 03:42 PM
Last Post: metulburr
  [pyGame] How do i change text with keyboard ?... JamieVanCadsand 1 4,498 Oct-03-2017, 10:05 AM
Last Post: metulburr
  [pyGame] Text with Input, help needed ! JamieVanCadsand 1 6,304 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