Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x pygame-如何读取文件并在屏幕上显示文本_Python 3.x_Pygame - Fatal编程技术网

Python 3.x pygame-如何读取文件并在屏幕上显示文本

Python 3.x pygame-如何读取文件并在屏幕上显示文本,python-3.x,pygame,Python 3.x,Pygame,所以我最近一直在读文件,一个挑战是读一个文件,然后在pygame中screen.blit所有的文本 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

所以我最近一直在读文件,一个挑战是读一个文件,然后在pygame中screen.blit所有的文本

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有关。

在这个循环中

for line in inputedFile:
    text = line
实际上,您始终使用最后一行覆盖文本。实际上,您需要附加新行。像这样:

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

您还可以尝试使用
read
功能,如前所述。

阅读一行行文字,并证明每行文字的正确性并不难。 下面介绍的
multiliender()
函数可以实现这一点。它首先生成每行的位图,并确定最大线宽。一旦知道该宽度,就可以通过调整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 从pygame.locals导入* mline_text=“有一天,当草是绿色的,天空是灰色的时候,这只凶猛的小家伙出去了。\n然后,当这只凶猛的小家伙坐在地上时,世界上所有的野兽都围了过来。\n” 宽度=600 高度=500 白色=255255 蓝色=0,0200 ### ###在(x,y)处向屏幕绘制多行文字块 ###有选择性的理由 ### def Multiliender(屏幕、文本、x、y、_字体、颜色=(128128)、对正=“左”): 对齐=对齐[0]。上限() text=text.strip().replace('\r','').split('\n') 最大宽度=0 text_位图=[] #将所有文本转换为位图,计算对齐宽度 对于文本中的t: text\u bitmap=字体渲染(t、真、彩色) text\u width=文本\u位图。获取\u width() 文本\位图。追加((文本\宽度,文本\位图)) 如果(最大宽度<文本宽度): 最大宽度=文本宽度 #使用对齐方式将所有文本位图绘制到屏幕上 对于文本位图中的(宽度、位图): xpos=x 宽度差异=最大宽度-宽度 如果(对正=='R'):#右对正 xpos=x+宽度 elif(对正='C'):#中心对正 xpos=x+(宽度相差//2) blit(位图,(xpos,y)) y+=位图。获取_高度() #开始pygame,为文本创建字体 pygame.init() screen=pygame.display.set_模式((600500)) pygame.font.init myfont=pygame.font.font(无,14) 屏幕填充(蓝色) #同一文本的三份副本,不同的理由 多参考(屏幕、多行文字、20、20、myfont、白色) 多引用程序(屏幕,多行文字,20100,myfont,白色,对正=“右”) 多参考(屏幕,多行文字,20180,myfont,白色,对正=“中心”) pygame.display.update() 虽然(正确): event=pygame.event.wait() 如果event.type==退出: pygame.quit() sys.exit()

当我尝试您建议的方法时,它确实允许我在屏幕上显示所有行,但每行后面仍然有一些小框。当你在屏幕上显示文本时,有没有一种方法可以让文本在不同的行上显示。blit it it?我建议你阅读更高级的文本输出,比如漂亮地打印多行。这是一个好问题。通常pygame代码会创建一种字体,然后使用该字体创建位图,然后将位图绘制到屏幕上。但是不能将
~font.render()
与多行字符串一起使用,它只处理单行,因此也没有对正。如何将每行转换为单个字符串?如果我这样做的话,我就可以单独blit每一行了。显然,不用使用常量
mline\u text
,一个简单的
mline\u text=open(“my\u file.txt”,“rt”).readlines()
就可以了。