【发布时间】:2019-05-16 10:10:30
【问题描述】:
我正在制作一个可以由 1 到 3 名玩家玩的游戏,在游戏开始之前,会询问用户有多少人在玩。我正在努力的部分是在屏幕上显示正确数量的玩家。我想出的想法如下。我创建了一个名为draw_text_2 的def,它使用文本的大小和颜色以及x 和y 位置在表面的正确位置打印玩家的统计数据。之后我将每个玩家的绘制文本的参数放在一个列表中。
player_1 = [100,0,0,0], player_2 = [100,0,0,0], player_3 = [100,0,0,0]
draw_text_2(surface, text, size, color, x, y):
Draw text to surface
surface - Pygame surface to draw to
text - string text to draw
size - font size
color - color of text
x - x position of text on surface
y - y position of text on surface
player_display = [
[win,'Player 1 '+'Health: ' + str(player_1[0])+' Damage: ' + str(player_1[1])+' Armour: '+str(player_1[2])+' postition: '+str(player_1[3]),10,(0,0,0),10,20]
[win,'Player 2 '+'Health: ' + str(player_2[0])+' Damage: ' + str(player_2[1])+' Armour: '+str(player_2[2])+' postition: '+str(player_2[3]),10,(0,0,0),10,120]
[win,'Player 3 '+'Health: ' + str(player_3[0])+' Damage: ' + str(player_3[1])+' Armour: '+str(player_3[2])+' postition: '+str(player_3[3]),10,(0,0,0),10,220]
]
所以当用户输入 2 number of players for example 时,程序将从player_display 中取出draw_text_2 的玩家参数并将它们放入draw_text_2 中,因为它在一个while循环中将继续显示,因为在 for 循环完成后 num_players_playing will 再次变为 2。不幸的是,这不起作用,因为它给了我TypeError 和list indices must be integers or slices, not tuple。问题是我怎样才能防止这个错误来制作这个方法,如果TypeError 无法解决,还有其他方法吗?
while True:
num_players_playing = 2
while num_players_playing > 0:
for i in player_display:
draw_text_2(i[x])
num_players_playing -= 1
【问题讨论】:
-
如果对您有帮助,请投票。
标签: python-3.x parameters pygame typeerror