【问题标题】:Getting user input on screen in pygame and how to store it在pygame的屏幕上获取用户输入以及如何存储它
【发布时间】: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


    【解决方案1】:

    看起来您想在游戏中使用 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.
    

    【讨论】:

    • 我知道我可以将它与 exe 捆绑在一起,但我不知道如何使用 pyInstaller 这样做,因为它会自动捆绑我需要的文件,所以我不知道如何手动添加一。另外,我不知道如何安装这些“GUI 库”。你能说得更具体点吗?
    • 谢谢,这很有帮助,但是我为 OcempGUI 下载的 .gz 文件该怎么办?所有“提取”选项都显示为灰色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 2016-06-15
    • 2011-08-30
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 2018-06-19
    相关资源
    最近更新 更多