So I have this function that blits text to the screen:
def text_speech(font : str ,size : int,text : str,color,x,y, bold : bool):
SCREEN = width, height = 900, 600
font = pygame.font.Font(font,size)
font.set_bold(bold)
text = font.render(text, True, color)
textRect = text.get_rect()
textRect.center = (x,y)
screen.blit(text,textRect)
If I have
screen.fill((0,0,0))
text_speech('arialnarrow.ttf', 30, 'Hello', (255,255,255),
width/2, height/2, False)
It prints 'Hello' on a black screen with white text. If the player hovers over this text, how can I make a text bubble appear that looks like this:
I just want a text that has a green background and says 'Click to Confirm'. Anyone got any clue of to how to do this. I apologize, I'm a bit new to Pygame.
The 4th parameter of font.render
is the optional background color of the text:
color = (255, 255, 255)
background = (0, 255, 0)
text = font.render(text, True, color, background)
Another option is to render the text to a surface and to convert the surface format to a per pixel alpha format (convert_alpha
):
textSurf = font.render(text, True, color).convert_alpha()
textSize = textSurf.get_size()
Create a new surface with the double size:
bubbleSurf = pygame.Surface((textSize[0]*2., textSize[1]*2))
bubbleRect = bubbleSurf.get_rect()
Fill the surface with the background color of the bubble:
bubbleSurf.fill(background)
Blit the text onto the bubble surface:
bubbleSurf.blit(textSurf, textSurf.get_rect(center = bubbleRect.center))
Blit the bubble surface onto screen
:
bubbleRect.center = (x,y)
screen.blit(bubbleSurf, bubbleRect)
Full function text_speech
:
def text_speech(font : str ,size : int,text : str,color,background,x,y, bold : bool):
SCREEN = width, height = 900, 600
font = pygame.font.Font(font,size)
font.set_bold(bold)
textSurf = font.render(text, True, color).convert_alpha()
textSize = textSurf.get_size()
bubbleSurf = pygame.Surface((textSize[0]*2., textSize[1]*2))
bubbleRect = bubbleSurf.get_rect()
bubbleSurf.fill(background)
bubbleSurf.blit(textSurf, textSurf.get_rect(center = bubbleRect.center))
bubbleRect.center = (x,y)
screen.blit(bubbleSurf, bubbleRect)
text_speech('arialnarrow.ttf', 30, 'Hello', (255,255,255), (0,128,0),
width/2, height/2, False)
See the example:
import pygame
import pygame.font
pygame.init()
width, height = 400, 300
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
textRect = pygame.Rect(0, 0, 0, 0)
def text_speech(font : str, size : int, text : str,color,background,x,y, bold : bool, bubble: bool):
global textRect
font = pygame.font.SysFont(None, 40)
#font = pygame.font.Font(font,size)
font.set_bold(bold)
textSurf = font.render(text, True, color).convert_alpha()
if bubble:
textSize = textSurf.get_size()
bubbleSurf = pygame.Surface((textSize[0]*2., textSize[1]*2))
textRect = bubbleSurf.get_rect()
bubbleSurf.fill(background)
bubbleSurf.blit(textSurf, textSurf.get_rect(center = textRect.center))
textRect.center = (x,y)
screen.blit(bubbleSurf, textRect)
else:
textRect = textSurf.get_rect()
textRect.center = (x,y)
screen.blit(textSurf, textRect)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
hover = textRect.collidepoint(pygame.mouse.get_pos())
screen.fill((0,0,0))
text_speech('arialnarrow.ttf', 30, 'Hello', (255,255,255), (0, 128, 0),
(width/2), (height/2), False, hover)
pygame.display.flip()