display text in pygame
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
def set_text(string, coordx, coordy, fontSize): #Function to set text
font = pygame.font.Font('freesansbold.ttf', fontSize)
#(0, 0, 0) is black, to make black text
text = font.render(string, True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (coordx, coordy)
return (text, textRect)
window.fill((255, 255, 255)) #Fills the whole window with white
#Places "Text in Pygame!" with an x,y coord of 250, 250 and 60 font size
totalText = set_text("Text in Pygame!", 250, 250, 60)
window.blit(totalText[0], totalText[1])
pygame.display.update()
4.25
4
def writeText(string, coordx, coordy, fontSize):
#set the font to write with
font = pygame.font.Font('freesansbold.ttf', fontSize)
#(0, 0, 0) is black, to make black text
text = font.render(string, True, (0, 0, 0))
#get the rect of the text
textRect = text.get_rect()
#set the position of the text
textRect.center = (coordx, coordy)
#add text to window
window.blit(text, textRect)
#update window
pygame.display.update()
Thank you!
4
0
4.67
3
"""system font"""
font = pygame.font.SysFont("Segoe UI", 35)
"""font from .ttf file"""
font = pygame.font.Font("path/to/font.ttf", 35)
textsurface = font.render("text", False, color) # "text", antialias, color
surface.blit(textsurface, (x, y))
Thank you!
3
0
3
2
font = pygame.font.SysFont(None, 24)
img = font.render('hello', True, BLUE)
screen.blit(img, (20, 20))
Thank you!
2
0
Are there any code examples left?
New code examples in category Python
-
Python 2023-04-11 03:04:20
-
Python 2022-03-27 22:40:04 pycharm no module named
-
Python 2022-03-27 22:25:05 assign multiple variablesin one line
-
Python 2022-03-27 22:20:02 levenshtein distance
-
Python 2022-03-27 21:35:09 get text from url python last slash
-
Python 2022-03-27 21:30:30 df concatenate df
-
Python 2022-03-27 21:25:09 python odd or even
-
Python 2022-03-27 21:15:32 python include function from another file
-
Python 2022-03-27 21:10:01 color module python
-
Python 2022-03-27 21:00:27 python tkinter cursor types