Pygame text isn't showing up using blit or sysfont

226 Views Asked by At

I've been trying to put text over a sprite (a dialogue box I imported) but no text comes up. The current code is me trying to blit the text onto the screen but it isn't working. The code works on a separate document, just not on this one. I currently have it creating the colour, then a box for the text to display in. After that, the box is made to fit its surroundings, a font is initialised through SysFont. Then my text is blit to the screen with a display.update to keep it constantly up the top of the layers. Even after hashing the code that creates the sprites and blits them, the text doesn't show up and instead the mouse coordinates and keyboard and mouse buttons are shown underneath the screen in a textbox. Anything helps as I'm new to coding.

import pygame
import os
import random
import sys
pygame.init()
pygame.font.init()

# Create Screen
FrameHeight = 3000
FrameWidth = 10000
screen = pygame.display.set_mode((FrameWidth, FrameHeight))

#Create Sprites
flag = True
background = pygame.image.load("fantasy-village-bg.jpg")
icon1 = pygame.image.load("Elder.png")
Dialogue = pygame.image.load("Dialogue-box.png")

def village():
#Repeating loop for beginning shot of the game
  while flag == True:

 #Background loops

   screen.fill((23, 234, 80))
   screen.blit(background, (0, 0))
  
 #Village elder loops
   screen.blit(icon1, (0, 0))
  
 #Dialogue box loops
 
   screen.blit(Dialogue, (-100, 400))
   pygame.display.flip()
  
 
village()
# PYGAME FRAME WINDOW and documentation for keylogging and mouse tracking
pygame.mouse.set_visible(0)
pygame.display.set_caption("Riftka: Adventure Awaits")

#Dealing with the event of a crash"""
crashed = False

while not crashed:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
        print(event)
    pygame.display.update()
    
    
#First set of dialogue 

black = (0,0,0)
WINDOW_WIDTH  = 500
WINDOW_HEIGHT = 500

def show_text( msg, color, x=WINDOW_WIDTH//2, y=WINDOW_WIDTH//2 ):
    global WINDOW
    text = font.render( msg, True, color)
    WINDOW.blit(text, ( x, y ) )

pygame.init()
WINDOW = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
pygame.display.set_caption("Text")

# Create the font (only needs to be done once)
font = pygame.font.SysFont(None, 25)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()

    show_text("We have been waiting for you traveller", blue)

    pygame.display.update()
1

There are 1 best solutions below

0
import random On

Images are layered in the order they're blit to the screen.

I've separated out your display text loop and added in some draw functions to show the layering

import pygame
pygame.init()

black = (0,0,0)
blue = pygame.Color("dodgerblue")
WINDOW_WIDTH  = 500
WINDOW_HEIGHT = 500

def show_text( window, font, msg, color,):
    text = font.render( msg, True, color)
    # center the text on the window
    text_rect = text.get_rect(center=window.get_rect().center)
    window.blit(text, text_rect)

pygame.init()
WINDOW = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
pygame.display.set_caption("Text")

# Create the font (only needs to be done once)
font = pygame.font.SysFont(None, 25)
clock = pygame.time.Clock()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    WINDOW.fill(black)
    pygame.draw.rect(WINDOW, pygame.Color("purple"), [250, 200, 200, 100])
    show_text(WINDOW, font, "We have been waiting for you traveller", blue)
    pygame.draw.rect(WINDOW, (pygame.Color("red")), [320, 220, 50, 60])
    pygame.display.update()
    clock.tick(30)  # limit FPS

This should look something like this: layered text