2016-09-03 93 views
0

我有一個小型的蟒蛇街機遊戲,我已經使用pyInstaller轉換爲獨立的.exe。它可以在pygame下正常工作,但問題是我使用pickle來保存高分。我最初使用cmd作爲用戶輸入,因此在cmd中會說「請輸入你的名字」,無論你輸入什麼內容,都將使用pickle將其存儲在單獨的文件中。兩個問題:我不能在獨立的.exe中使用cmd(而且它看起來很醜),當我用pickle將它存儲在一個單獨的文件中時,我認爲它不包含在獨立文件中。我說「想」是因爲代碼永遠不會超過用戶輸入部分。獲取pygame屏幕上的用戶輸入以及如何存儲它

如何讓用戶輸入出現在屏幕上(在我自己的字體和位置中)而不是在cmd中?

如何可以包括用戶輸入文件(其存儲有泡菜)將被包括在該.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) 

感謝所有誰答覆。

回答

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. 
+0

我知道,我可以用EXE捆綁,但我不知道如何使用pyInstaller,因爲它會自動捆綁我需要的文件,所以我不知道如何手動添加文件。另外,我不知道如何安裝這些「GUI庫」。你可以再詳細一點嗎? –

+0

剛剛更新了我的建議 – sr3z

+0

謝謝,這非常有幫助,但我如何處理爲OcempGUI下載的'.gz'文件?所有「提取」選項都變灰。 –

相關問題