微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

文字未显示在Pygame中?我尝试了其他方法进行修复,但没有任何帮助

如何解决文字未显示在Pygame中?我尝试了其他方法进行修复,但没有任何帮助

我的pygame脚本运行正常并产生了背景,但是我的q1Text文本未在屏幕上呈现。有什么帮助吗?我尝试了各种修复程序,但似乎根本没有用。

    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,d):
        self.a=a
        self.b=b
        self.c=c
        self.d=d
        self.q=q

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

def gameLoop():
    global font
    font=pygame.font.SysFont('goudystout',False)
    global q1
    q1=question('What is the formula for speed','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,'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()

解决方法

您正在创建文本表面,但是仍然需要在主屏幕上将其变白。

尝试以下代码:

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()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。