我如何在python中使这个程序成为两个玩家?

时间:2017-06-27 15:54:50

标签: python python-3.x

任何人都可以帮我改变这个程序,以便2名玩家可以玩吗? 定义一个模拟掷骰子的程序

def dice(sides):
# Get a random number between 1 and 6
    throw=random.randint(1,6)
    print("score",throw)
    return throw
#Welcome messages
print("Snakes and ladders")
#Main program for the game
Player1=input("What is the name of player 1? ")
import random
position=0
#eeping it all in a loop will allow the players to keep rolling the dice until the game has finished
while position<49:
    throw=input("Roll the dice")
    dice1=dice(6)
    dice2=dice(6)
    total=dice1+dice2
#If the total of the dice thrown is not a double the player will move the total numbers of squares
    if dice1!=dice2:
        print("You are now on space", position+total)
        position=total+position
    if position>=49:
        print("You have won")
        if position<=0:
            print("You are on square 0")
#Now I will check to see if the player has rolled a double
    if dice1==dice2:
        print("This is a double you will now move back",total,"spaces")
        if position<=0:
                print("You are on space 0")
        else:
                print("You are now on space", position-total)
#This means that if the vaule of dice1 is the same as dice2 then it will move back and if not it will move forward

1 个答案:

答案 0 :(得分:0)

通过使用类实现此代码,您可以学到很多东西。

至于保持代码与您正在使用的代码尽可能相似,有很多方法可以添加第二个播放器。

例如,您可以添加另一个变量position_p2来存储玩家2的位置,然后对玩家2上的玩家1重复相同的检查,如下所示:

def dice(sides): # NOTE function names should start with verbs, e.g. roll_dice
# Get a random number between 1 and 6
    throw=random.randint(1,6)
    print("score",throw)
    return throw
#Welcome messages
print("Snakes and ladders")
#Main program for the game
Player1=input("What is the name of player 1? ") #NOTE variables should be lower case

# Ask player2 for it's name
player2 = input("What is the name of player 2? ")
import random #NOTE imports should go at the beggining of the script
position_p1=0 # rename so that is easy to remember that it refers to p1
# store p2 position
positoin_p2 = 0
#eeping it all in a loop will allow the players to keep rolling the dice until the game has finished
while position<49:
    throw_p1=input("Player 1: roll the dice") #same as above
    dice1=dice(6)
    dice2=dice(6)
    total=dice1+dice2
#If the total of the dice thrown is not a double the player will move the total numbers of squares
    if dice1!=dice2:
        position_p1=total+position_p1 # you can use position_p1 += total
        print("You are now on space", position_p1)

    if position_p1>=49:
        print("Player 1 won") # Change so that you know which player has won
        if position_p1<=0:
            print("You are on square 0")
#Now I will check to see if the player has rolled a double
    if dice1==dice2:
        print("This is a double you will now move back",total,"spaces")
    position_p1 -= total
        if position_p1<=0:
                print("You are on space 0")
        else:
                print("You are now on space", position_p1)
#This means that if the vaule of dice1 is the same as dice2 then it will move back and if not it will move forward

    throw_p2=input("Player 2: roll the dice")
    dice1=dice(6)
    dice2=dice(6)
    total=dice1+dice2
#If the total of the dice thrown is not a double the player will move the total numbers of squares
    if dice1!=dice2:
        position_p2=total+position_p2 # you can use position_p1 += total
        print("You are now on space", position_p2)

    if position_p2>=49:
        print("Player 2 won") # Change so that you nkow which player has won
        if position_p2<=0:
            print("You are on square 0")
#Now I will check to see if the player has rolled a double
    if dice1==dice2:
        print("This is a double you will now move back",total,"spaces")
    position_p2 -= total
        if position_p2<=0:
                print("You are on space 0")
        else:
                print("You are now on space", position_p2)
#This means that if the vaule of dice1 is the same as dice2 then it will move back and if not it will move forward

这不是执行此操作的最佳方式,但它应该有效,并且与您发布的代码类似。如果你想改进代码,你可以尝试在一个函数中放置两个玩家相同的部分。您还可以将玩家位置存储在字典中,以便将游戏扩展到n个玩家。如果你想练习,你可以为游戏中的每件事(游戏本身,玩家等)制作课程。你可以做很多其他的事情,但从一些简单的概括开始,比如为n个玩家制作它。