问题描述
我的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()