【发布时间】:2016-09-03 18:18:46
【问题描述】:
我有一个小型 python 街机游戏,我已使用 pyInstaller 将其转换为独立的 .exe。它适用于 pygame,但问题是我使用 pickle 来保存高分。我最初使用 cmd 进行用户输入,所以在 cmd 中它会说“请输入您的姓名”,而您输入的任何内容都将使用 pickle 存储在单独的文件中。两个问题:我不能在独立的 .exe 中使用 cmd (而且它看起来很丑),当我用 pickle 将它存储在一个单独的文件中时,我认为它没有包含在独立的文件中。我说“思考”是因为代码永远不会超过用户输入部分。
如何让用户输入显示在屏幕上(以我自己的字体和位置)而不是 cmd?
和
如何将用户输入文件(与 pickle 一起存储)包含在 .exe 中?
这是我目前拥有的(全部在主循环中):
if lives == 0:
username = input("Please type your name: ")
highscore = {username: points}
try:
with open("C:/Python35/highscore.txt", "rb") as highscoreBest:
highscoreBest = pickle.load(highscoreBest)
except EOFError:
with open("C:/Python35/highscore.txt", "wb") as highscoreBest:
pickle.dump(highscore, highscoreBest)
for (k, v), (k2, v2) in zip(highscore.items(), highscoreBest.items()):
if v >= v2:
with open("C:/Python35/highscore.txt", "wb") as highscoreBest:
pickle.dump(highscore, highscoreBest)
with open("C:/Python35/highscore.txt", "rb") as highscoreBest:
highscoreBest = pickle.load(highscoreBest)
for key, value in highscoreBest.items():
print("%s has the highscore of %s!" % (key, value))
highscoreText = highscorefont.render("Highscore %s" % (value), 1, textcolor)
gameOverText = font.render("GAME OVER", 1, textcolor)
scoreText = font.render("Score %s" % (points), 1, textcolor)
while 1:
screen.blit(gameOverText, (200, 400))
screen.blit(scoreText, (225, 450))
screen.blit(highscoreText, (235,500))
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
pygame.display.flip()
time.sleep(1)
感谢所有回复的人。
【问题讨论】:
标签: python-3.x pygame pickle pyinstaller