Creating a leaderboard for offline game in Python

半城伤御伤魂 提交于 2019-12-02 07:17:10
Joran Beasley

The easiest is probably to just use mongodb or something (mongo DB is a nosql type database that allows you to save dictionary data easily...)

You can use the free account at https://mongolab.com (taht should give you plenty of space).

(You will need pymongo as well easy_install pymongo ).

then you can simply save records there

from pymongo import MongoClient
uri = "mongodb://test1:test1@ds051990.mongolab.com:51990/joran1"
my_db_cli = MongoClient(uri)
db = my_db_cli.joran1 #select the database ... 

my_scores = db.scores #this will be created if it doesnt exist!
#add a new score
my_scores.insert({"user_name":"Leeeeroy Jenkins","score":124,"time":"11/24/2014 13:43:22"})
my_scores.insert({"user_name":"bob smith","score":88,"time":"11/24/2014 13:43:22"})
from pymongo import DESCENDING
#get a list of high scores (from best to worst)
print (list(my_scores.find().sort("score",DESCENDING)))

Those credentials will actually work if you want to test the system (keep in mind I added leeroy a few times).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!