在Python中创建离线游戏排行榜

2024-05-21 01:21:40 发布

您现在位置:Python中文网/ 问答频道 /正文

对于一个学校项目,我正在创建一个有评分系统的游戏,我想创建某种排行榜。完成后,老师们会把它上传到一个共享服务器上,在那里其他学生可以下载游戏的副本,但不幸的是,学生们无法保存到该服务器上;如果我们可以,排行榜将是小菜一碟。最多只需要记录几百个分数,所有的电脑都可以上网。在

我对服务器和主机不太了解,也不知道java、html或web开发中常用的任何其他语言,所以其他相关问题并没有真正的帮助。我的游戏将得分信息打印到一个文本文件中,从那里我不知道如何将它放到每个人都可以访问的在线位置。在

有没有一种方法可以只用python完成这样的任务?在

这里我有更新排行榜文件的代码(假设它只是一个文本文件),一旦我有了分数。这将假设我在同一个地方有排行榜和分数文件的副本。在

这是我模拟排行榜的格式(排行榜s.txt)公司名称:

Leaderboards

1) JOE  10001
2) ANA  10000
3) JAK  8400
4) AAA  4000
5) ABC  3999

这是日志文件将打印的内容-缩写和分数(日志.txt)公司名称:

^{pr2}$

代码(适用于Python2.7和3.3):

def extract_log_info(log_file = "log.txt"):
    with open(log_file, 'r') as log_info:
        new_name, new_score = [i.strip('\n') for i in log_info.readlines()[:2]]

    new_score = int(new_score)
    return new_name, new_score

def update_leaderboards(new_name, new_score, lb_file = "Leaderboards.txt"):
    cur_index = None
    with open(lb_file, 'r') as lb_info:
        lb_lines = lb_info.readlines()
        lb_lines_cp = list(lb_lines) # Make a copy for iterating over
        for line in lb_lines_cp:
            if 'Leaderboards' in line or line == '\n':
                continue

            # Now we're at the numbers
            position, name, score = [ i for i in line.split() ]

            if new_score > int(score):
                cur_index = lb_lines.index(line)
                cur_place = int(position.strip(')'))
                break

        # If you have reached the bottom of the leaderboard, and there
        # are no scores lower than yours
        if cur_index is None:
            # last_place essentially gets the number of entries thus far
            last_place = int(lb_lines[-1].split()[0].strip(')'))
            entry = "{}) {}\t{}\n".format((last_place+1), new_name, new_score)
            lb_lines.append(entry)
        else: # You've found a score you've beaten
            entry = "{}) {}\t{}\n".format(cur_place, new_name, new_score)
            lb_lines.insert(cur_index, entry)

            lb_lines_cp = list(lb_lines) # Make a copy for iterating over
            for line in lb_lines_cp[cur_index+1:]:
                position, entry_info = line.split(')', 1)
                new_entry_info = str(int(position)+1) + ')' + entry_info
                lb_lines[lb_lines.index(line)] = new_entry_info

    with open(lb_file, 'w') as lb_file_o:
        lb_file_o.writelines(lb_lines)


if __name__ == '__main__':
    name, score = extract_log_info()
    update_leaderboards(name, score)

更多信息:

  • 分数不到100万
  • 理想情况下,解决方案应该是一些游戏外部的代码,这样我就可以制作一个可执行文件,让用户在完成后运行
  • 我知道这听起来不太安全-也不是-但没关系,它不需要防黑客攻击

Tags: nameinfolognewforindexline分数
1条回答
网友
1楼 · 发布于 2024-05-21 01:21:40

最简单的方法可能是只使用mongodb或其他东西(mongodb是一个nosql类型的数据库,允许您轻松保存字典数据…)

您可以使用https://mongolab.com的免费帐户(taht应该给您足够的空间)。在

(您还需要pymongoeasy_install pymongo)。在

然后你可以简单地在那里保存记录

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)))

如果你想测试系统的话,这些证书实际上是有效的(请记住,我添加了leeroy几次)。在

相关问题 更多 >