我用python创建了一个保龄球游戏,并且模板格式表现得很奇怪。
从图片上看,第一部分是正确的。但是,一旦我添加了一个用户的滚动,score变量就会向右移动。但是在接下来的游戏中它将保持不变。我对为什么会这样感到困惑。
这是我的参考代码(仅显示与格式相关的功能):
template = "{0:6}"
template_name = "{0:10}"
template_score = "{0:64}"
def next_turn():
global current_turn
global counter
global current_turn_name
global frame_counter
global frame_max_counter
if current_turn >= player_counter:
print(frame_counter, frame_max_counter)
if frame_counter >= frame_max_counter:
endgame()
print("Next frame!")
frame_counter += 1
current_turn = 0
counter += 1
for keys in players.keys():
if players[keys].order == current_turn + 1:
current_turn_name = keys
current_turn += 1
break
main_game()
def update_screen():
global template_score
global counter
print("Names 1 --- 2 --- 3 --- 4 --- 5 --- 6 --- 7 --- 8 --- 9 --- 10 --- score")
for names in players.keys():
print(template_name.format(names), end='')
for key, value in players[names].frames.items():
print(template.format(value), end='')
print(template_score.format(players[names].score))
print("\n")
template_score = "{0:" + str(70 - 6 * counter) + "}"
def main_game():
os.system("cls")
update_screen()
print(current_turn, player_counter)
roll = input("It is now {}'s turn! What is your roll?: ".format(current_turn_name))
players[current_turn_name].roll(roll)
因此,程序的结构是,当用户在此处输入掷骰时,分数会添加到他们的总成绩中,并且掷骰将以他们的名字保存。然后,我调用next_turn()定义谁是下一个播放器,或者是否是下一帧。分数变量在第一个掷球后移动,因此此行:if current_turn >= player_counter
无关紧要。
朝着正确方向迈出的任何一步都将受到赞赏