【问题标题】:Pygame: Local Scoreboard After Death With InputPygame:输入死亡后的本地记分牌
【发布时间】:2013-12-20 23:38:44
【问题描述】:

在我正在开发的游戏中,我正在进入游戏的后期阶段。我现在想要添加一个本地记分牌,玩家在该记分牌上输入名称,然后将其添加到记分牌中,其中显示“得分:,时间:,杀戮:”。我必须用自己的类制作一个新文件吗?链接到任何可以帮助这个记分牌的地方也将被接受。我真正需要知道的是代码说明如何添加输入然后将其保存到本地系统。谢谢你。如果您想查看代码以帮助我获得分数,请给我留言。再次感谢。

【问题讨论】:

  • 需要更多信息。你想让玩家在 GUI 内的文本框中输入他们的名字吗?您希望文本输入的方式和位置。
  • 是的,我希望他们输入一个名字,这样他们就可以炫耀了。对于文本,我希望它在顶部有标题,所以“名字杀死时间分数”然后是下面的所有信息。 (我将能够显示文本(我希望))它只是为我需要知道的玩家保存信息和文本框。任何帮助表示赞赏。

标签: python python-2.7 pygame


【解决方案1】:

如果你的记分牌真的很高级,那么你可以使用 SQLite,但对于大多数记分牌用例而言,pickle.dump() 和 pickle.load() 就足够了(如果我理解正确的话)http://docs.python.org/2/library/pickle.html

【讨论】:

  • 谢谢。 “高级”记分牌会包含哪些内容?
【解决方案2】:

这就是我的做法。有人可能会说它笨拙,但它对我来说可以无缝运行。

基本上,它从文本文件中读取数据并将数据转换为要编辑、排序和写回文件的列表。

scoreboard = open("scoreboard.txt", 'r')    # Open file

scores = scoreboard.read()  # Put the whole file into a string
scores = [i.split() for i in scores.split('\n') if i]   # Convert string to list of lists
scores.append(['JAY', 1027, 120, 42])   # Add new name and scores to list
scores = sorted(scores, key=lambda tup: tup[1])[::-1]   # Sort the lists according to the second item in each (SCORE)

scoreboard = open("scoreboard.txt", 'w') # Open the file again to wipe it
scoreboard.write(str(scores).replace('], [', '\n').translate(None, ',[]\'\"')) # Write sanatized data to list

scoreboard.close() # Done

玩弄它!

【讨论】:

  • 谢谢。我会看看这是怎么回事:)
  • 一个问题。我如何让 pygame '读取'然后显示信息
  • 您可以只使用创建的变量scores;它包含所有数据,然后使用 pygame 的字体模块。不过,pygame 的一个烦恼是它一次只能渲染一行文本 font.render(),这意味着您必须迭代多行文本。例如; for score in scores: line = font.render("%s\t%s\t%s\t%s" % (score[0], score[1], score[2], score[3]), 1, (255, 255, 255)); window.blit(line, (0, 0)) ... 什么的。
猜你喜欢
  • 2014-11-27
  • 1970-01-01
  • 1970-01-01
  • 2018-11-19
  • 2023-03-14
  • 2014-02-16
  • 2018-02-22
  • 2012-04-11
  • 1970-01-01
相关资源
最近更新 更多