我正在做一个石头剪刀布游戏。
下面的代码有效,但是缺少两个关键的东西,我真的很想知道如何实现这一点:
如果出现平局,玩家必须能够再次选择。因此break语句必须执行,但是返回while循环并再次播放需要什么?
游戏结束时也是如此。玩家必须能够再次玩。我认为这与上面的代码相同。
答案非常有帮助,因为它确实可以帮助我更多地了解while循环。
def rps():
play = True
player1 = input('Player1: rock, paper of scissors? ')
player2 = input('Player2: rock, paper of scissors? ')
while play:
if player1 == 'rock' and player2 == 'rock': # deze methode werkt niet om opnieuw weer te beginnen
print ('Tie! Pick again')
break
elif player1 == 'rock' and player2 == 'paper':
print('Player 2 won')
break
elif player1 == 'rock' and player2 == 'scissors':
print ('Speler 1 won')
break
elif player1 == 'paper' and player2 == 'rock':
print('Speler 1 won')
break
elif player1 == 'paper' and player2 == 'paper':
print('Tie! Pick again')
continue
elif player1 == 'paper' and player2 == 'scissors':
print('Speler 2 won')
break
elif player1 == 'scissors' and player2 == 'rock':
print('Speler 2 won')
break
elif player1 == 'scissors' and player2 == 'paper':
print('Speler 1 won')
break
elif player1 == 'scissors' and player2 == 'scissors':
print('Tie! Pick again')
答案 0 :(得分:1)
在第二个while循环中包装游戏性循环。不要打断关系。游戏循环的输入发生在游戏循环中。退出或玩法的输入发生,并在游戏循环之前进行评估。
while True:
print("Welcome to Rock, Paper, Scissors.")
gameplay = input("(P)lay or (Q)uit?")
if (gameplay == 'Q'):
break
else:
while True:
player1 = input('Player1: rock, paper of scissors? ')
player2 = input('Player2: rock, paper of scissors? ')
{Gameplay Logic}
答案 1 :(得分:0)
此代码应能解决您的目的
def rps():
play = True
player1 = input('Player1: rock, paper of scissors? ')
player2 = input('Player2: rock, paper of scissors? ')
while play:
if player1 == 'rock' and player2 == 'rock': # deze methode werkt niet om opnieuw weer te beginnen
print ('Tie! Pick again')
elif player1 == 'rock' and player2 == 'paper':
print('Player 2 won')
if input('Type c to continue playing, anything else to quit') == 'y':
pass
else:
break
elif player1 == 'rock' and player2 == 'scissors':
print ('Speler 1 won')
if input('Type c to continue playing, anything else to quit') == 'c':
pass
else:
break
elif player1 == 'paper' and player2 == 'rock':
print('Speler 1 won')
if input('Type c to continue playing, anything else to quit') == 'c':
pass
else:
break
elif player1 == 'paper' and player2 == 'paper':
print('Tie! Pick again')
elif player1 == 'paper' and player2 == 'scissors':
print('Speler 2 won')
if input('Type c to continue playing, anything else to quit') == 'c':
pass
else:
break
elif player1 == 'scissors' and player2 == 'rock':
print('Speler 2 won')
if input('Type c to continue playing, anything else to quit') == 'c':
pass
else:
break
elif player1 == 'scissors' and player2 == 'paper':
print('Speler 1 won')
if input('Type c to continue playing, anything else to quit') == 'c':
pass
else:
break
elif player1 == 'scissors' and player2 == 'scissors':
print('Tie! Pick again')
player1 = input('Player1: rock, paper of scissors? ')
player2 = input('Player2: rock, paper of scissors? ')
只需减少一些if和 否则,但我保留了此信息是为了您的理解并进行一些细微调整 您的代码。