TransWikia.com

Text is not showing up in Pygame? I tried some other ways to fix but nothing has helped

Stack Overflow Asked by nathannotnathan on September 28, 2020

My pygame script is working fine and produces a background, but my text of q1Text is not rendering on screen. Any help? I have tried various fixes but no have seem to work at all.

    import pygame
    
    import random
    
    pygame.init()
    
    #Title
    pygame.display.set_caption('Average Speed Game')
    
    #Screen 
    screenWidth=900
    screenHeight=500
    win=pygame.display.set_mode((screenWidth,screenHeight))
    
    #Framerate
    clock= pygame.time.Clock()
    
    #background image
    bg=pygame.image.load('C:\Users\18597\Desktop\My Python Scripts\speed\bg.png')
    
    class question(object):
        def __init__(self,q,a,b,c,d):
            self.a=a
            self.b=b
            self.c=c
            self.d=d
            self.q=q
    
    def redrawGameWin():
        win.blit(bg,(0,0))
        q1Text= font.render(q1.q,1,(255,255,255))
        pygame.display.update()
    
    def gameLoop():
        global font
        font=pygame.font.SysFont('goudystout',15,True,False)
        global q1
        q1=question('What is the formula for speed','Time/Distance','Distance/Time','Accleration/Time','bruh')
        run= True
        while run:
            clock.tick(27)
            
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                   run=False
    
            redrawGameWin()
    
    gameLoop()
    pygame.quit()

import pygame

import random

pygame.init()

#Title
pygame.display.set_caption('Average Speed Game')

#Screen 
screenWidth=900
screenHeight=500
win=pygame.display.set_mode((screenWidth,screenHeight))

#Framerate
clock= pygame.time.Clock()

#background image
bg=pygame.image.load('C:\Users\18597\Desktop\My Python Scripts\speed\bg.png')

class question(object):
    def __init__(self,q,a,b,c,d):
        self.a=a
        self.b=b
        self.c=c
        self.d=d
        self.q=q

def redrawGameWin():
    win.blit(bg,(0,0))
    q1Text= font.render(q1.q,1,(255,255,255))
    pygame.display.update()

def gameLoop():
    global font
    font=pygame.font.SysFont('goudystout',15,True,False)
    global q1
    q1=question('What is the formula for speed','Time/Distance','Distance/Time','Accleration/Time','bruh')
    run= True
    while run:
        clock.tick(27)
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
               run=False

        redrawGameWin()

gameLoop()
pygame.quit()

import pygame

import random

pygame.init()

#Title
pygame.display.set_caption('Average Speed Game')

#Screen 
screenWidth=900
screenHeight=500
win=pygame.display.set_mode((screenWidth,screenHeight))

#Framerate
clock= pygame.time.Clock()

#background image
bg=pygame.image.load('C:\Users\18597\Desktop\My Python Scripts\speed\bg.png')

class question(object):
    def __init__(self,q,a,b,c,d):
        self.a=a
        self.b=b
        self.c=c
        self.d=d
        self.q=q

def redrawGameWin():
    win.blit(bg,(0,0))
    q1Text= font.render(q1.q,1,(255,255,255))
    pygame.display.update()

def gameLoop():
    global font
    font=pygame.font.SysFont('goudystout',15,True,False)
    global q1
    q1=question('What is the formula for speed','Time/Distance','Distance/Time','Accleration/Time','bruh')
    run= True
    while run:
        clock.tick(27)
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
               run=False

        redrawGameWin()

gameLoop()
pygame.quit()

One Answer

You are creating the text surface, but you still need to blit it on the main screen.

Try this code:

def redrawGameWin():
    win.blit(bg,(0,0))
    q1Text= font.render(q1.q,1,(255,255,255))  # text surface
    textRect = q1Text.get_rect()
    textRect.center = ((900//2,500//2))  #center on screen
    win.blit(q1Text,textRect)  # draw on screen

    pygame.display.update()

Answered by Mike67 on September 28, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP