我在处理游戏项目时遇到了一些麻烦,尝试在用户启动新游戏时创建用户创建提示,如果输入的用户名尚未退出则创建新用户,否则附加统计信息如果用户名已存在。这些统计数据是附加到用户统计数据的胜利,损失和抽奖。
所以我只能想到两个选项,首先是创建一个字典,其中用户名为key,嵌套列表为stat值,或者创建一个带有username功能的类和每个列表,如胜利清单,损失清单等。
非常感谢任何帮助或想法!
1月28日编辑:
根据代码请求,我会发布我所拥有的内容以及我想要完成的内容......
第1部分 - 创建用户名:
while True:
playerinput = input("Please type a number correlating with your menu choice, or 4 to return to main menu: ")
if playerinput == "1":
game_range = range(1, 10)
play_the_game(playerinput, game_range)
elif playerinput == "2":
game_range = range(1, 26)
play_the_game(playerinput, game_range)
elif playerinput == "3":
game_range = range(1, 28)
play_the_game(playerinput, game_range)
elif playerinput == "4":
exec(open("menuitems.py").read(), globals())
else:
print("Unknown input: ", playerinput, ", please try again")
此时开始一个新游戏(我已经离开菜单项以减少这里的代码大小)通过选择一个菜单选项,我希望它在此时要求用户输入用户名,如果新用户尚不存在,则创建一个新用户,或者附加统计信息。
第2部分 - 用户统计信息(这是代码的重要部分):
def play_the_game(playerinput, game_range):
while True:
board = [" "] * (max(game_range) + 1)
plays = []
player_icon, computer_icon = player_choice()
turn = who_goes_first()
print(turn + " will get the first turn of the game")
game_in_play = True
while game_in_play:
if turn == "player":
print_the_board(board, playerinput)
print("""
Computer: Choose your move carefully, human
""")
print("Enter your move between ", min(game_range), "-", max(game_range), ", ", min(game_range),
" for top left, ", max(game_range), " for bottom right")
player_move = input("Enter your move: ")
if player_move == "e":
break
if player_move == "u":
if len(plays) == 0:
print("No more moves left to undo - you cheated your way back to the beginning!")
continue
print("I shouldn't be letting you do this - it really feels like cheating...")
player_move = plays.pop()
board[player_move] = " "
player_move = plays.pop()
board[player_move] = " "
continue
if not player_move.isdigit():
AI_insults()
continue
if int(player_move) not in game_range or not free_space_on_board(board, int(player_move)):
AI_insults()
continue
make_a_move(board, player_icon, int(player_move))
plays.append(int(player_move))
if win_condition(board, player_icon, playerinput):
print_the_board(board, playerinput)
print("Congrats! You beat that pesky computer!")
game_in_play = False
else:
if draw_condition(board, game_range):
print_the_board(board, playerinput)
print("It's a draw!")
break
else:
turn = "Computer"
else:
move = computer_move(board, computer_icon, playerinput, game_range)
plays.append(move)
make_a_move(board, computer_icon, move)
if win_condition(board, computer_icon, playerinput):
print_the_board(board, playerinput)
AI_endgame_brag()
game_in_play = False
else:
if draw_condition(board, game_range):
print_the_board(board, playerinput)
print("It's a draw!")
break
else:
turn = "player"
if not end_game():
break
我遗漏了该代码中使用的大量函数来再次尝试保持这个小,但我想要完成的是为了让玩家的“win_condition”得到满足,然后它将+1添加到用户赢取统计数据,如果是“draw_condition”,则+1表示统计数据,如果计算机获胜,则输入+1。
我有另一部分菜单代码,玩家可以选择查看用户的统计信息,我想要打印用户名,他们的胜利,损失和抽奖。
到目前为止,我认为字典是最好的选择,如果这个用户名存在于字典中 - 然后使用此键并附加到其值,否则创建新密钥等...
答案 0 :(得分:0)
更新:好消息!我想到了我的问题的解决方案,这实际上是一个非常简单的解决方案,看起来像这样......
with open("users.json", "r") as f:
try:
user_dict = json.load(f)
except ValueError:
user_dict = {}
username = input("Please enter a username to create a new user, or to access an existing user: ")
if username not in user_dict:
user_dict[username] = [0, 0, 0]
然后在获胜,平局,失败等之后,我在休息前的每个输赢条件之后将此代码添加到游戏代码中。
if win_condition(board, player_icon, playerinput):
print_the_board(board, playerinput)
print("Congrats! You beat that pesky computer!")
user_dict[username][0] += 1
game_in_play = False
添加了“user_dict [username] [index] + = 1”,其中我只使用索引0表示胜利,索引1表示亏损,3表示绘制。
我现在已经在游戏结束时触发的函数中添加了一些代码......
with open("users.json", "w") as f:
json.dump(user_dict, f)
它与现在绑定到用户名的持久统计数据完美配合,感谢Stack我在这里找到的JSON命令......