创造一个小游戏。为什么播放器变量不起作用?

时间:2016-04-11 14:04:15

标签: python python-3.x

我正在用Python创建一个小游戏。游戏的目的是匹配所有卡。我的原始代码有2个子程序,它们有很多重复的代码(找到here)。我试图压缩他们,但它没有工作。

这是当前的代码:

def pick(Player):
    P1score = 0
    P2score = 0
    Player = 0
    if Player == 0:
        Player  = Player+1
    elif Player == 1:
        Player = 2
    elif Player == 2:
        Player = 1
    if len(set1) == 0:
        won(P1score,P2score)
    else:
        print("It's Player" +str(Player) + "'s turn and there are still "+ str(len(set1)) + " pairs left to find!")
        side1 =int(input("What number would you like to pick from side one?"))
        side2 =int(input("What number would you like to pick from side two?"))
        #This function makes the player input 2 valid card numbers
        while side1 not in range(len(set1)) or side2 not in range(len(set2)):
            side1 =int(input("What number would you like to pick from side one?"))
            side2 =int(input("What number would you like to pick from side two?"))
        s1pick = set1[side1]
        s2pick = set2[side2]
        matching(s1pick, s2pick, set1, set2,P1score, P2score)

def matching(s1pick,s2pick,set1,set2,P1score,P2score,Player):
    picks = []
    picks.append(s1pick)
    picks.append(s2pick)
    if picks[0] == picks[1]:
        if Player == 1:
            P1score = P1score+1
        elif Player == 2:
            P2score = P2score + 1
        for i in range (10):
            print("Correct")
            time.sleep(0.2)
        print("Good Job, you successfully picked " + picks[0] + " and "+ picks[0])
        set1.remove(s1pick)
        set2.remove(s2pick)
        pick(Player)
    else:
        print("Sorry, not a match! From side one you picked "+ picks[0] + " and from side 2 you picked " + picks[1])
        pick(Player)

Player = 0
pick(Player)

上面有更多代码,但它不会对此部分产生影响。

基本上,我希望它将变量Player从1更改为2,然后再更改为1,依此类推,因此程序的其余部分知道将该点归功于谁。

为什么这不起作用?

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

__author__ = "mexO"
# SnapGameForFun
import random
from random import shuffle
import time

cards = ["Horse", "Cow", "Pig", "Goat", "Chicken", "Sheep", "Donkey", "Duck", "Rabbit"]
print("Here are all the cards there could be, try to find out where they could be hiding!")
time.sleep(3)
for i in cards:
    print(i)
    time.sleep(0.5)

print("The cards have been numbered 1 to 9.\nPick a number from the first list and one from the second list."
      "\nIf they match the cards will be removed from the list.\nEasy right?\n\n\n\nLets start!")

set1 = ["Horse", "Cow", "Pig", "Goat", "Chicken", "Sheep", "Donkey", "Duck", "Rabbit"]
set2 = ["Horse", "Cow", "Pig", "Goat", "Chicken", "Sheep", "Donkey", "Duck", "Rabbit"]

random.shuffle(set1,random.random)
random.shuffle(set2,random.random)

P1score = 0
P2score = 0
#--------------------------WON--------------------------
#--------------------------WON--------------------------
#--------------------------WON--------------------------

def won(P1score,P2score):
    if P1score > P2score:
        winner = "Player 1"
        winnerscore = P1score
    else:
        winner = "Player 2"
        winnerscore = P2score
    print("Well done to {0} who got {1} of the 10 pairs correct!".format(winner, winnerscore))

def pick(player=0):
    player = player % 2
    global P1score, P2score
    if len(set1) == 0:
        won(P1score,P2score)
    else:
        print("It's Player {0}'s turn and there are still {1} pairs left to find!".format(player + 1, str(len(set1))))
        side1 =int(input("What number would you like to pick from side one?"))
        side2 =int(input("What number would you like to pick from side two?"))
        while side1 not in range(len(set1)) or side2 not in range(len(set2)):
            side1 =int(input("What number would you like to pick from side one?"))
            side2 =int(input("What number would you like to pick from side two?"))
        s1pick = set1[side1]
        s2pick = set2[side2]
        if player == 0:
            matching(s1pick, s2pick, set1, set2, P1score, player)
        else:
            matching(s1pick, s2pick, set1, set2, P2score, player)

def matching(s1pick,s2pick,set1,set2,score,player):
    global P1score, P2score
    if s1pick == s2pick:
        if player == 0:
            P1score += 1
        else:
            P2score += 1
        print('P1score:', P1score, 'P2score', P2score)
        for i in range (10):
            print("Correct")
            time.sleep(0.2)
        print("Good Job, you successfully picked {0} and {1}\n".format(s1pick, s2pick))
        set1.remove(s1pick)
        set2.remove(s2pick)
        pick(player + 1)
    else:
        print("Sorry, not a match! From side one you picked {0} and from side 2 you picked {1}\n".format(s1pick, s2pick))
        print('P1score:', P1score, 'P2score', P2score)
        pick(player+1)


if __name__ == "__main__":
    pick()

您还需要解决几个问题: 1.您需要输入的数字是0-8而不是1-9 2.如果用户只是在没有任何输入的情况下进入输入或输入无法转换为int的内容,则程序将死亡。