在pygame中获取用户在屏幕上的输入以及如何存储它

时间:2016-09-03 18:18:46

标签: python-3.x pygame pickle pyinstaller

我有一个小的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)

感谢所有回复的人。

1 个答案:

答案 0 :(得分:0)

看起来你想在你的游戏中拥有GUI。阅读此http://www.pygame.org/wiki/gui,它应该对您有帮助。

例如,

OcempGUI

Links

Home Page:  http://ocemp.sourceforge.net/gui.html
Source:     http://sourceforge.net/project/showfiles.php?group_id=100329&package_id=149654
Installation http://ocemp.sourceforge.net/manual/installation.html

关于高分的文本文件,您可以将其与可执行文件一起包含在捆绑包中,请参阅https://pythonhosted.org/PyInstaller/spec-files.html#adding-files-to-the-bundle

Adding Data Files
To have data files included in the bundle, provide a list that describes the files as the value of the datas= argument to Analysis. The list of data files is a list of tuples. Each tuple has two values, both of which must be strings:

The first string specifies the file or files as they are in this system now.
The second specifies the name of the folder to contain the files at run-time.
For example, to add a single README file to the top level of a one-folder app, you could modify the spec file as follows:

a = Analysis(...
     datas=[ ('src/README.txt', '.') ],
     ...
     )
You have made the datas= argument a one-item list. The item is a tuple in which the first string says the existing file is src/README.txt. That file will be looked up (relative to the location of the spec file) and copied into the top level of the bundled app.