Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用pygame制作分数列表_Python_Sql_Pygame - Fatal编程技术网

Python 如何使用pygame制作分数列表

Python 如何使用pygame制作分数列表,python,sql,pygame,Python,Sql,Pygame,我完成了pygame教程 但是如果我想让用户输入他们的昵称,然后输出分数列表(排序) 我该怎么办?快速浏览字体 以下是程序创建字体对象的方式: smallfont = pygame.font.SysFont("comicsansms", 25) #(font type, font size) medfont = pygame.font.SysFont("comicsansms", 50) largefont = pygame.font.SysFont("comicsansms", 80) 然后

我完成了pygame教程

但是如果我想让用户输入他们的昵称,然后输出分数列表(排序)

我该怎么办?

快速浏览字体 以下是程序创建字体对象的方式:

smallfont = pygame.font.SysFont("comicsansms", 25) #(font type, font size)
medfont = pygame.font.SysFont("comicsansms", 50)
largefont = pygame.font.SysFont("comicsansms", 80)
然后需要使用字体对象创建曲面。程序中有这样一个例子:

text = smallfont.render("Score: "+str(score), True, black)
然后是这样的布告:

gameDisplay.blit(text, [0,0])
保存分数 要将数组保存到文件,需要导入pickle。例如,请尝试以下代码:

import pickle, os

if os.path.isfile("/home/username/Pickling_Program/scores.txt"): #insert filename here
    f = open("/home/username/Pickling_Program/scores.txt", "r")
    scores = pickle.load(f)
    print scores
    f.close()
else:
    print "No scores saved."

scores = []
for a in range(10):
    scores.append(["", 0])

for a in range(10):
    scores[a][0] = raw_input("Type name:")
    scores[a][1] = raw_input("Type score:")

f = open("/home/username/Pickling_Program/scores.txt", "w")
pickle.dump(scores, f)
f.close()
我还不能测试这个程序,但它应该可以工作。在评论中提及任何错误,以便我可以修复它们

我不会告诉你具体要采取什么步骤来做这件事,因为我不仅没有时间,而且你也不会学到任何东西。要做到这一点,您需要做的是获取玩家获得的分数,加载分数文件,并查看它是否大于最低值。如果是,则将其添加到分数列表中,删除最低分数,再次对列表排序,然后保存。然后,您将获取分数并将其显示在屏幕上。请参考以下网站,以便您可以执行您正在尝试执行的操作,以及更多操作:

“给一个人一条鱼,他就可以吃一天。教一个人钓鱼,他就可以吃一辈子


希望这有帮助!

PyGame没有获取用户输入的元素/小部件。PyGame有一些(旧的)GUI模块,您可以找到它(),或者您必须创建自己的输入小部件。您必须呈现文本。