[英]How to display text on PyGame?
我正在嘗試創建一個游戲,我需要在不同的區域顯示文本,所以我嘗試創建一個類來顯示文本,但我需要調用 pygame.display.set_mode 才能對我的文本進行 blit 但我已經有了:
black = (0, 0, 0)
white = (255, 255, 255)
gold = (250, 175, 50)
blue = (50, 50, 250)
class Display:
def __init__(self, width, height, colour):
self.width = width
self.height = height
self.colour = colour
canvasW, canvasH = width, height
canvas = pygame.display.set_mode((canvasW, canvasH), pygame.NOFRAME)
canvas.fill(colour)
class Text:
def __init__(self, text, size, colour, bg, font, bold, italic, x, y, width, height, col):
self.text = text
self.size = size
self.colour = colour
self.bg = bg
self.font = font
self.bold = bold
self.italic = italic
self.x = x
self.y = y
self.width = width
self.height = height
self.col = col
Display(width, height, col)
pygame.font.init()
myFont = pygame.font.SysFont(font, size, bold, italic)
textSurf = myFont.render(text, False, colour, bg)
Display.canvas.blit(textSurf, (x, y))
class DrawDisplay:
def __init__(self, width, height, col, intro):
self.width = width
self.height = height
self.col = col
self.intro = intro
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
Display(self.width, self.height, self.col)
# canvasW, canvasH = width, height
pygame.display.set_caption("TRAVELLER")
# canvas = pygame.display.set_mode((canvasW, canvasH), pygame.NOFRAME)
clock = pygame.time.Clock()
pygame.display.flip()
if intro:
# Text('TRAVELLER', 50, black, 'freesansbold.ttf')
Text('TRAVELLER', 50, black, white, 'freesansbold.ttf', True, False, 10, 100, width, height, col)
# Text('TRAVELLER', 48, white, 'freesansbold.ttf')
pygame.display.update()
clock.tick(120)
pygame.display.flip()
time.sleep(7.5)
pygame.quit()
else:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
canvas.fill(gold)
clock.tick(120)
pygame.display.flip()
class Load(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
width = 450
height = 250
DrawDisplay(width, height, gold, True)
class User(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
width = 1920
height = 1080
DrawDisplay(width, height, blue, False)
LoaderThread = Load()
UserThread = User()
LoaderThread.start()
LoaderThread.stop()
UserThread.start()
我不確定如何在不將文本連接到顯示類的情況下執行此操作,或者我不知道如何在需要時調用它。
創建Display
和Text
實例:
display = Display(self.width, self.height, self.col)
text1 = Text('TRAVELLER', 50, black, white, 'freesansbold.ttf', True, False, 10, 100, width, height, col)
類Display
需要一個方法來繪制背景並更新顯示:
class Display:
def __init__(self, width, height, colour):
self.width = width
self.height = height
self.colour = colour
canvasW, canvasH = width, height
self.canvas = pygame.display.set_mode((canvasW, canvasH), pygame.NOFRAME)
pygame.display.set_caption("TRAVELLER")
def drawbackground(self):
self.canvas.fill(self.colour)
def update(self):
pygame.display.flip()
類 text 在構造函數中構造文本表面。 它有一個draw
方法,可以將文本( self.textSurf
) blit
到顯示表面( canvas
),這是draw
方法的一個參數:
class Text:
def __init__(self, text, size, colour, bg, font, bold, italic, x, y, width, height, col):
self.text = text
self.size = size
self.colour = colour
self.bg = bg
self.font = font
self.bold = bold
self.italic = italic
self.x = x
self.y = y
self.width = width
self.height = height
self.col = col
myFont = pygame.font.SysFont(font, size, bold, italic)
self.textSurf = myFont.render(text, False, colour, bg)
def draw(self, canvas):
canvas.blit(self.textSurf, (self.x, self.y))
不要使用time.sleep
。 您有一個主應用程序循環,因此請使用它。 根據狀態self.intro
繪制場景。 如果按下某個鍵或經過一段時間,則跳過介紹。 使用pygame.time.get_ticks()
以毫秒為單位獲取時間。 例如:
class DrawDisplay:
def __init__(self, width, height, col, intro):
self.width = width
self.height = height
self.col = col
self.intro = intro
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
pygame.font.init()
display = Display(self.width, self.height, self.col)
text1 = Text('TRAVELLER', 50, black, white, 'freesansbold.ttf', True, False, 10, 100, width, height, col)
clock = pygame.time.Clock()
start_time = pygame.time.get_ticks()
run = True
while run:
clock.tick(120)
# handel events
current_time = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if self.intro and event.type == pygame.KEYDOWN:
run = False
# change game states
if self.intro and current_time > start_time + 7500: # 7.5 seconds
run = false
# draw dispaly
display.drawbackground()
# draw scene
if self.intro:
text1.draw(display.canvas) #
else:
# draw game objects
# [...]
pass
# update display
display.update()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.