pygame-如何读取文件并将文本变白到屏幕上

时间:2018-11-18 21:36:43

标签: python-3.x pygame

所以最近我一直在读取文件,并且面临的一个挑战是先读取文件,然后将其屏幕屏蔽。将pygame中的所有文本都清空。

import pygame
import sys
pygame.init()

screenSizeX = 1080
screenSizeY = 720
screenSize = (screenSizeX,screenSizeY)

screen = pygame.display.set_mode(screenSize,0)
pygame.display.set_caption("Display Text")

WHITE = (255,255,255)
GREEN = (0,255,0)
BLUE = (0,0,255)
RED = (255,0,0)
YELLOW = (255,255,0)
BLACK = (0,0,0)
x = 100
y = 100


file = input("Enter the name of the file you want to display(no quotes) ")
file = str(file)

inputedFile = open(file, "r")

def textOutput(line):
    fontTitle = pygame.font.SysFont("Arial", 72)
    textTitle = fontTitle.render(line, True, GREEN)
    screen.blit(textTitle, (x,y))

pygame.display.update()
for line in inputedFile:
    text = line

go = True
while go:
    for event in pygame.event.get():
        if event.type ==pygame.QUIT:
            go = False
        screen.fill(WHITE)
        fontTitle = pygame.font.SysFont("Arial", 72)
        textTitle = fontTitle.render(text, True, GREEN)
        screen.blit(textTitle, (x,y))

        pygame.display.update()

inputedFile.close()

pygame.quit()
sys.exit()

所以这种作品。它将显示您输入的文件的最后一行以供阅读。所以我想知道是否有一种方法可以使它显示文本文件中的每一行。它还在行后显示一个矩形,我认为这与文本行末尾的\ n有关。

2 个答案:

答案 0 :(得分:0)

在此循环中

for line in inputedFile:
    text = line

实际上,您实际上一直在用最后一行覆盖文本。您实际上会想要添加新行。像这样:

text = []
for line in inputedFile:
    text.append(line)
text = '\n'.join(text)

您还可以尝试使用here中所述的read函数。

答案 1 :(得分:0)

遍历文本行,证明每一行并不难。 下面介绍的multilineRender()函数可以做到这一点。它首先生成每条线的位图,并确定最大线宽。一旦知道此宽度,就可以通过调整x坐标轻松地左右对齐。

然后将每个位图依次放到pygame屏幕上。显然,随着我们的前进,y坐标会随着每个位图的高度而增加。

import sys
import pygame
from pygame.locals import *

mline_text="The Scroobious Pip went out one day\nWhen the grass was green, and the sky was grey.\nThen all the beasts in the world came round\nWhen the Scroobious Pip sat down on the ground.\n"

WIDTH = 600
HEIGHT= 500
WHITE = 255,255,255
BLUE  = 0,0,200

###
### Draw a multi-line block of text to the screen at (x,y)
### With optional justification
###
def multilineRender(screen, text, x,y, the_font, colour=(128,128,128), justification="left"):
    justification = justification[0].upper()
    text = text.strip().replace('\r','').split('\n')
    max_width = 0
    text_bitmaps = []
    # Convert all the text into bitmaps, calculate the justification width
    for t in text:
        text_bitmap = the_font.render(t, True, colour)
        text_width  = text_bitmap.get_width()
        text_bitmaps.append((text_width, text_bitmap))
        if (max_width < text_width):
            max_width = text_width
    # Paint all the text bitmaps to the screen with justification
    for (width, bitmap) in text_bitmaps:
        xpos = x
        width_diff = max_width - width
        if (justification == 'R'):  # right-justify
            xpos = x + width_diff
        elif (justification == 'C'): # centre-justify
            xpos = x + (width_diff // 2)
        screen.blit(bitmap, (xpos, y) )
        y += bitmap.get_height()

#start pygame, create a font for the text
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.font.init
myfont = pygame.font.Font(None,14)
screen.fill(BLUE)
# three copies of the same text, different justificaiton
multilineRender(screen, mline_text, 20, 20, myfont, WHITE)
multilineRender(screen, mline_text, 20,100, myfont, WHITE, justification="right")
multilineRender(screen, mline_text, 20,180, myfont, WHITE, justification="centre")
pygame.display.update()

while (True):
    event = pygame.event.wait()
    if event.type == QUIT:
        pygame.quit()
        sys.exit()

pygame screenshot