Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 猪罐头游戏';I don’我不知道如何循环遍历所有的玩家_Python_While Loop_Pygame - Fatal编程技术网

Python 猪罐头游戏';I don’我不知道如何循环遍历所有的玩家

Python 猪罐头游戏';I don’我不知道如何循环遍历所有的玩家,python,while-loop,pygame,Python,While Loop,Pygame,我已经解决了大部分问题,但似乎不知道如何循环每个玩家,让他们玩游戏。这是我目前掌握的代码。我认为main()函数中的while循环是错误的,但我不知道如何修复它。如果你能给我指出正确的方向,请告诉我。如果你掷1,我还需要弄清楚如何结束回合 import random def instructions(): print ("==================================================") print ("\nWelcome to the

我已经解决了大部分问题,但似乎不知道如何循环每个玩家,让他们玩游戏。这是我目前掌握的代码。我认为main()函数中的while循环是错误的,但我不知道如何修复它。如果你能给我指出正确的方向,请告诉我。如果你掷1,我还需要弄清楚如何结束回合

import random


def instructions():
    print ("==================================================")
    print ("\nWelcome to the Game of Pig.  To win, be the")
    print ("player with the most points at the end of the")
    print ("game.  The game ends at the end of a round where")
    print ("at least one player has 100 or more points.\n")
    print ("On each turn, you may roll the die as many times")
    print ("as you like to obtain more points.  However, if")
    print ("you roll a 1, your turn is over, and you do not")
    print ("obtain any points that turn.\n")


def num_players():
    while True:
        players = raw_input("How many players will be playing? ")

        if players.isdigit():
            return int(players)
        else:
            print "\nPlease enter a valid number of players.\n"


def name_players(players):
    count = 1
    list_of_players = []
    for i in range(players):
        name = raw_input("Enter the name for Player {}: ".format(count))
        list_of_players.append(name)
        count += 1
    print ""
    return list_of_players


def start_game(list_of_players):
    points = 0
    for player in list_of_players:
        print "{0} has {1} points.".format(player, points)
    print ("==================================================\n")
    s = input("How many sides of the dice do you want? ")
        for player in list_of_players:
        print ("\n{0}'s turn:").format(player)
        answer = raw_input("Press y to roll the dice?")
        while answer == 'y' and points <= 100:
            roll = random.randrange(1, s)
            if roll > 1:
                points += roll
                print "{0} has {1} points.".format(player, points)
                answer = raw_input("Press y to roll the dice?")


def main():
    instructions()
    players = num_players()
    list_of_players = name_players(players)
    start_game(list_of_players)


if __name__ == "__main__":
    main()
随机导入
def指令():
打印(“============================================================================”)
打印(“\n欢迎参加猪的游戏。要赢得比赛,请成为赢家”)
打印(“在游戏结束时点数最多的玩家”)
打印(“游戏。游戏在一轮结束时结束,其中”)
打印(“至少有一名玩家拥有100或更多积分。\n”)
打印(“每转一圈,您可以滚动模具多次”)
打印(“您希望获得更多分数,但如果”)
打印(“你掷1,你的回合结束了,你没有”)
打印(“获取转动的任何点。\n”)
def num_players():
尽管如此:
玩家=原始输入(“将有多少玩家参与游戏?”)
if players.isdigit():
返回整数(玩家)
其他:
打印“\n请输入有效的玩家数量。\n”
def名称_玩家(玩家):
计数=1
球员名单=[]
对于范围内的i(玩家):
name=raw_输入(“输入播放器的名称:”.format(count))
列出所有玩家。追加(姓名)
计数+=1
打印“”
返回球员名单
def开始游戏(玩家列表):
分数=0
对于所有玩家列表中的玩家:
打印“{0}有{1}个点。”。格式(播放器,点)
打印(“===========================================================================================\n”)
s=输入(“您想要骰子的多少面?”)
对于所有玩家列表中的玩家:
打印(“\n{0}的回合:”)。格式(播放器)
回答=原始输入(“按y键掷骰子?”)
而答案==“y”和第1点:
点数+=滚动
打印“{0}有{1}个点。”。格式(播放器,点)
回答=原始输入(“按y键掷骰子?”)
def main():
指示()
players=num_players()
球员名单=球员姓名(球员)
开始游戏(玩家列表)
如果名称=“\uuuuu main\uuuuuuuu”:
main()

您遇到的问题是由于函数start\u游戏中的局部变量points。您的所有玩家都共享同一个变量以跟踪他们的分数。因此,一旦第一名玩家达到/通过100分,游戏逻辑将转移到第二名玩家。但是,积分变量仍然保留前一个玩家的分数(大于或等于100)。正因为如此,下面的玩家永远不会有机会进入while循环

每次积分变量保持值大于或等于100时,您应该为每个玩家声明积分可变,或者重置积分变量

def start_game(list_of_players):
    points = 0 #<----------------- points is local variable to the function
    for player in list_of_players:
        print("{0} has {1} points.".format(player, points))
    print ("==================================================\n")
    s = int(input("How many sides of the dice do you want? ")) ###
    # validate value of s?
    for player in list_of_players:
        ### the variables declared in for loop is unique to each player
        print ("\n{0}'s turn:".format(player))
        answer = input("Press y to roll the dice?")
        while answer == 'y' and points <= 100:
            roll = random.randrange(1, s + 1, 1) ### randrange 
            if roll > 1:
                points += roll
                print("{0} has {1} points.".format(player, points))
                answer = input("Press y to roll the dice?")
def开始游戏(玩家列表):

积分=0#好吧,
对于玩家列表中的玩家:
实际上似乎在所有玩家中循环。你遇到了什么问题?对于第一个玩家来说效果很好,但如果有多个玩家,它只会跳到游戏结束,而不是让他们玩,你的主要问题是你只使用一个值来跟踪分数。我认为每个玩家都应该有一个单独的分数,你可能需要另一个变量来跟踪他们在当前一轮中迄今为止获得的分数(如果他们掷1,他们将无法保持该分数)。