【发布时间】:2015-10-24 08:13:19
【问题描述】:
我正在创建一个打字游戏,我希望有人输入显示的单词,事情就是这样。我不知道该怎么做。这些词来自一个 .txt 文件,每个词都在它自己的行上。随机数生成器生成一个数字,对文本文件进行枚举,获取对应槽中的单词给随机数。然后该词显示在屏幕上。希望这是有道理的。有办法吗?如果这还不够,将给出更多代码。这是使用的代码:
def text_objects(text, color, size):
if size == "small":
textSurf = smallFont.render(text, True, color)
elif size == "medium":
textSurf = medFont.render(text, True, color)
elif size == "large":
textSurf = largeFont.render(text, True, color)
elif size == "verySmall":
textSurf = vSmallFont.render(text, True, color)
return textSurf, textSurf.get_rect()
def messageToScreen(msg, color, y_displace = 0, size = "small"):
textSurface, textRect = text_objects(msg, color, size)
textRect.center = (display_width/2), (display_height/2) + y_displace
gameDisplay.blit(textSurface, textRect)
def randWord():
rand = random.randint(0,996)
fp = open("1.txt")
for i, line in enumerate(fp):
if i == rand:
line = line.strip()
messageToScreen(line,black,-200,size = "large")
for char in line:
global chars
chars = []
chars.append(char)
print(str(chars))
fp.close()
【问题讨论】:
标签: python keyboard pygame key