pygame - 使用for循环输出名称和分数到屏幕

时间:2016-03-12 14:35:01

标签: python pygame

我在pygame中取得了高分;想检查,如何输出名称和分数到屏幕。 现在我能够从文件中获取内容并在屏幕上输出。但是,数据全部一起显示在一行中。 我试着写#34; \ n"但它没有用。 我是pygame的新手,有人可以帮助我吗?

我的代码

f = open("score.txt",'r')
for line in f:
    column = line.split("\t")
    names = column[0]
    scores = int(column[2])
    scoreArray.append(scores)
    nameArray.append(names)
    data = list(zip(scoreArray,nameArray))
l = heapsort(data)


for i in l:
//output on screen
    score = smallfont.render("\n "+ str(i[0]), True, Color.black)
    nameRect = diskSurf.get_rect()
    nameRect.midtop = (width / 2, height / 2)
    screen.blit(score, nameRect)

    score = smallfont.render("\n "+ str(i[1]), True, Color.black)
    nameRect = diskSurf.get_rect()
    nameRect.midtop = (width / 2, height / 2)
    screen.blit(score, nameRect)

    print i[1] , "\t", i[0] // in the command line

我想要的输出是

Name            Score 
abc               2
cde               5
ffd               10

但我现在的输出在同一行的另一个上面是1

编辑代码:

for i in l, range (0,520, 10):
    screen.blit(smallfont.render(str(i[0]), True, Color.gray),(200,190))
    screen.blit(smallfont.render(str(i[1]), True, Color.gray),(500,190))
    print i[1] , "\t", i[0]

此代码打印出如下值:

Name            Score 
(2,'abc')

1 个答案:

答案 0 :(得分:0)

免责声明:我没有测试过这个!

with open("score.txt",'r') as f:
    name_scores = []
    for line in f:
        row = line.split("\t")
        player_name = row[0]
        score = int(row[2])
        name_scores.append((name, score))

name_scores.sort(key=lambda name_score: name_score[1], reverse=True)

for i in xrange(len(name_scores)):
    player_name, score = name_scores[i]
    x_name = 200
    x_score = 500
    y = 10 * i

    name_pos = (x_name, y)
    score_pos = (x_score, y)

    name_sprite = smallfont.render(player_name, True, Color.gray)
    score_sprite = smallfont.render(score, True, Color.gray)

    screen.blit(name_sprite, name_pos)
    screen.blit(score_sprite, score_pos)