如何将进度保存到文本文件?

时间:2019-10-05 21:55:54

标签: python python-3.x tkinter

我正在尝试将进度保存为文本文件。我试过泡菜,但是没用。

这是代码;我想一开始问他们是否要加载或开始新游戏,以及在猜测过程中的任何时候是否要保存进度。

# Username and password system to get into the puzzle
Username = []
Password = []
while Username != "Username":
    Username = input("Please enter your username: ")
while Password != "Password":
    Password = input("Please enter your password: ")

#Import Modules
import tkinter as tkr
import string
import random
import os
import time
from collections import OrderedDict

while True:
    #Define Root From tkinter
    root = tkr.Tk()
    root.configure(background="white")
    root.configure(highlightbackground="grey")
    root.iconbitmap("PuzzleIcon.ico")

    #Inputs for title etc...
    Title = input("Please enter the Title of your puzzle: ")
    Phrase = input("Please enter the phrase you would like to be encoded: ").upper()
    giveaways = str(input("Please enter the letters you want to giveaway with no spaces: ")).upper()
    giveaways = giveaways + " "
    giveaways = "".join(OrderedDict.fromkeys(giveaways))
    root.title(Title)

    #Define alphabet and numbers
    Alphabet = list(string.ascii_uppercase)
    Numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
    random.shuffle(Numbers)

    # Specifies the numbers to put under the phrase
    AlphabetNumbersToDisplay = []
    Numbers.append(" ")
    Alphabet.append(" ")
    for letter in Phrase:
        n = 0
        found = False
        while n < len(Alphabet) and found == False:
            if letter == Alphabet[n]:
                AlphabetNumbersToDisplay.append(Numbers[n])
                found = True
            n = n + 1

    # Defines the tkinter window
    def tkinterDisplay():
        for i in range (0, len(Alphabet)):
            tkr.Label(root, text=Alphabet[i], fg="black", bg="white", width=3).grid(row=0, column=i)

        for i in range (0, len(Numbers)):
            if Alphabet[i] in giveaways:
                tkr.Label(root, text=Numbers[i], fg="black", bg="white", width=3).grid(row=1, column=i)
            else:
                tkr.Label(root, text= "__", fg="black", bg="white", width=3).grid(row=1, column=i)

        tkr.Label(root, text=" ", fg = "white",bg="white").grid(row=2)

        for i in range(0, len(Phrase)):
            if Phrase[i] in giveaways:
                tkr.Label(root, text=Phrase[i], fg= "black", bg="white", width = 3).grid(row = 3, column =i)
            else:
                tkr.Label(root, text="__", fg= "black", bg="white", width = 3).grid(row = 3, column =i)

        for i in range(0, len(AlphabetNumbersToDisplay)):
            tkr.Label(root, text=AlphabetNumbersToDisplay[i], fg= "black", bg="white", width = 3).grid(row = 4, column =i)

        winMessage = "Well done! You Won!".upper()

        for i in range(0, len(winMessage)):
            if len(giveaways) == 27:
                tkr.Label(root, text=" ", fg = "white",bg="white").grid(row=5)
                tkr.Label(root, text=winMessage[i], fg="red", bg="white", width = 3).grid(row = 6, column=i)

    # Guessing system for the program
    def Guessing():
        tkinterDisplay()
        while True:
            try:
                GuessNum = int(input("Please enter the number of your guess: "))
                break
            except ValueError:
                print("Your answer must be a number!")
        while True:
            try:
                GuessLetter = str(input("Please enter the letter you want to guess: ")).upper()
                break
            except ValueError:
                print("Your answer must be a letter!")

        foundMatch = False
        for i in range(0, len(Alphabet)):
            if Alphabet[i] == GuessLetter and Numbers[i] == GuessNum:
                global giveaways
                foundMatch = True
                giveaways = giveaways + Alphabet[i]
        if foundMatch == False:
            print("They do not match, try again!")
        elif foundMatch == True and len(giveaways) < 27:
            print("Well done! Keep going!")

    while len(giveaways) < 27:
        Guessing()
        tkinterDisplay()

    def Clear():
        os.system("cls")

    if len(giveaways) == 27:
        tkinterDisplay()
        print("Well done! You won! The phrase you guessed was: " + Phrase)
        Restart = input("Would you like to play again? (Y/N): ").upper()
        if Restart == "N":
            break
        elif Restart == "Y":
            print("Restarting Puzzle...")
            time.sleep(.1500)
            root.destroy()
            Clear()
            continue

    root.mainloop()

1 个答案:

答案 0 :(得分:0)

这是一个非常广泛的问题。再次构建“保存”选项可能与其余游戏一样工作。您需要考虑以下因素:

  • 需要什么数据来完全定义游戏在任何给定点的状态(这就是文本文件中的内容)
  • 在保存状态下重建游戏需要什么逻辑(您需要为此编写新代码)
  • 如何以及何时为用户提供加载/保存的机会(这会影响游戏体验)