python开发的游戏生成_在Python中为离线游戏创建排行榜

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

我对服务器或主机了解不多,也不了解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('

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

':

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 = "{}) {}{}

".format((last_place+1), new_name, new_score)

lb_lines.append(entry)

else: # You've found a score you've beaten

entry = "{}) {}{}

".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

>理想情况下,解决方案只是游戏外部的一些代码,这样我就可以制作一个可执行文件,供用户在完成后运行

>我知道这听起来并不十分安全-并非如此-没关系,它不需要防黑客攻击

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值