ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-10-14 02:58:24  阅读:215  来源: 互联网

标签:python leaderboard


对于一个学校项目,我正在创建一个具有计分系统的游戏,并且我想创建某种排行榜.完成后,教师会将其上传到共享服务器,其他学生可以在该服务器上下载游戏副本,但不幸的是,学生无法将其保存到该服务器上.如果可以的话,排行榜将是小菜一碟.最多只能记录几百个分数,并且所有计算机都可以访问互联网.

我对服务器或主机了解不多,也不了解Java,HTML或Web开发中常用的任何其他语言,因此其他相关问题并没有真正帮助.我的游戏将计分信息打印到一个文本文件中,从那里我不知道如何将其在线获取,每个人都可以访问.

有没有办法用python来完成这样的任务?

在这里,我有了代码,一旦获得分数,就可以更新排行榜文件(假设它只是文本文件).假设我在同一位置拥有排行榜和成绩文件的副本.

这是我的模拟排行榜(Leaderboards.txt)的格式:

Leaderboards

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

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

ABC
3999

代码(适用于python 2.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)

更多信息:

>分数将小于1000000
>理想情况下,解决方案只是游戏外部的一些代码,这样我就可以制作一个可执行文件,供用户在完成后运行
>我知道这听起来并不十分安全-并非如此-没关系,它不需要防黑客攻击

解决方法:

最简单的方法可能只是使用mongodb之类的东西(mongo DB是一个nosql类型的数据库,可让您轻松保存字典数据…)

您可以在https://mongolab.com使用免费帐户(应为您提供足够的空间).

(您将需要pymongo以及easy_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).

标签:python,leaderboard
来源: https://codeday.me/bug/20191014/1912076.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有