将帐户与一组数据相关联

时间:2015-12-11 18:19:32

标签: python tkinter pickle

DCprice = 100
ACprice = 10
FCprice = 500
TCprice = 5000
Gold = 0
Multiply = 1
autoclickers = 0

def SaveGame():
    with open('objs.pickle', 'wb') as f:
         pickle.dump([Gold, DCprice, Multiply, ACprice, FCprice, TCprice, Multiply, autoclickers], f)

def Load():
    with open('objs.pickle', 'rb') as f:
        return pickle.load(f)

这是我从游戏中保存数据的代码。 我想知道的是,我将如何使用每个数据来查找帐户。

([Gold,DCprice,Multiply,ACprice,FCprice,TCprice,Multiply,autoclickers],

将保存在一个帐户中,并拥有用户名和密码。

所以我的问题:我不知道怎么做,任何正确方向的暗示都会很好

3 个答案:

答案 0 :(得分:1)

创建一个Account对象。这正是类和对象的用途:

class Account(object):
    def __init__(self, username, password, dc=0, ac=0, fc=0, tc=0, gold=0, multiply=1):
        self.username = username
        self.password = password
        self.dcPrice = dc
        self.acPrice = ac
        self.tcPrice = tc
        self.gold = gold
        self.multiply = multiply

首次运行代码时,您可以创建几个帐户:

accounts = [Account("joe", "secret"), 
            Account("sally", "password", gold=100)]

如果您想将帐户0中的黄金数量设置为1000,您可以这样做:

accounts[0].gold = 1000

您可以对accounts数组进行pickle和unpickle,因为它由简单的数据类型组成。例如:

with open('objs.pickle', 'wb') as f:
    pickle.dump(accounts, f)

答案 1 :(得分:0)

您可以转储这样的数据结构:

data = {
  'username': username,
  'password': password,
  'account_info': [Gold, DCprice, Multiply, ACprice, FCprice, TCprice, autoclickers],
  }

或者,对于多个帐户:

data = [{
  'username': a.username,
  'password': a.password,
  'account_info': [a.gold, a.dcprice, a.multiply, a.acprice, a.fcprice, a.tcprice, a.autoclickers],
  } for a in accounts]

然后,只需转储并保存该对象:

pickle.dump(data, f)
data = pickle.load(f)

答案 2 :(得分:0)

作为替代方案,您可以将数据存储在数据库中,例如MySQL。您所要做的就是设置表并使用连接器将数据插入表中。例如,使用MySQL:

import mysql.connector
mysql.connector.connect(user='', password='', host='', database='')
INSERT INTO table_name values (Gold, DCprice, Multiply, ACprice, FCprice,
TCprice, Multiply, autoclickers)