我刚刚开始使用Python并编写一个简单的石头剪刀游戏。 Here's到目前为止我所拥有的。我可以完美地与其他玩家玩游戏。我想知道的是如何保持胜利的得分,所以如果玩家1赢得第一场比赛就会说:
玩家1 = 1 玩家2 = 0
等等。此外,如果有任何提示可以使我的代码更有效(有一个很好的解释),那就太好了。
谢谢!
答案 0 :(得分:3)
考虑这个伪代码的建议(某些部分需要实现):
# a Player class. it should have a Name and a Score
class Player():
def __init__(name):
self.name = name
self.score = 0
# displays a prompt and reads in the players name returning a string
def getPlayerName():
# needs code here, see next function for idea of what
pass
# ask the player to make a choice, takes a Player object
# and returns a string
def getPlayerAttack(player):
print "%s, what do you choose?" % player.name
return raw_input("> ")
# determines who wins and updates score accordingly
# takes in the player objects and their attack choices
def attack(player1, choice1, player2, choice2):
if choice1 == choice2:
print "Its's a tie."
elif choice1 == "1" and choice2 == "2":
print "%s wins." % player2
player2.score = player2.score + 1
elif ... # other attacks
# display the scores of the two players
def displayScores(player1, player2):
print "%s vs %s" % (player1.score, player2.score)
player1 = Player(getPlayerName())
player2 = Player(getPlayerName())
while true:
choice1 = getPlayerAttack(player1)
choice2 = getPlayerAttack(player2)
attack(player1, choice1, player2, choice2)
displayScores(player1, player2)
这需要一些工作,并且不超级优化,但它应该是一个开始,应该显示更多的概念。使用Ctrl-C停止或添加停止条件(例如,任一玩家输入“0”) - 免费包含错误。 :)
快乐的编码。
答案 1 :(得分:0)
有几种方法可以提高它的效率,但如果你必须一次输入一个,我必须问你如何玩Rock Paper Scissors :-P
得分可以归结为:
if choice1 == choice2 :
print "Its's a tie."
elif choice1 - choice2 == 1 or choice2 - choice1 == 2 :
print "%s wins." % player1
else:
print "%s wins." % player2
如果你想做多个游戏,只需将整个事情放在while(1):
循环中,并为每个分数添加一个变量。然后它会变成:
score1 = 0
score2 = 0
while(1):
<lines 6-18>
if choice1 == choice2 :
print "Its's a tie."
elif choice1 - choice2 == 1 or choice2 - choice1 == 2 :
print "%s wins." % player1
score1 = score1 + 1
else:
print "%s wins." % player2
score2 = score2 + 1
print "%s: %d points. %s: %d points." % (player1, score1, player2, score2)
答案 2 :(得分:0)
试试这个:
player1 = raw_input("Player 1 name: ")
player2 = raw_input("Player 2 name: ")
while(1)
player1score = 0
player2score = 0
print "%s, what do you choose?" % player1
print "1. Rock"
print "2. Paper"
print "3. Scissors"
choice1 = raw_input("> ")
print "%s, what do you choose?" % player2
print "1. Rock"
print "2. Paper"
print "3. Scissors"
choice2 = raw_input("> ")
if choice1 == "1" and choice2 == "1":
print "Its's a tie."
if choice1 == "1" and choice2 == "2":
print "%s wins." % player2
player2score=player2score+1
if choice1 == "2" and choice2 == "1":
print "Player 1 wins." % player1
player1score=player1score+1
if choice1 == "1" and choice2 == "3":
print "Player 1 wins." % player1
player1score=player1score+1
if choice1 == "3" and choice2 == "1":
print "%s2 wins." % player2
player2score=player2score+1
if choice1 == "2" and choice2 == "3":
print "%s wins." % player2
player2score=player2score+1
if choice1 == "3" and choice2 == "2":
print "Player 1 wins." % player1
player1score=player1score+1
print "Player1: %s" % player1score
print "Player2: %s" % player2score