python - 我怎样才能让用户选择玩哪种模式?

标签 python

我想让用户选择是否要随机化坐标,或者是否要为 Python 上的战舰游戏输入坐标。应该注意的是,我正在使用 PyScripter 的可移植应用程序来构建它。

随机版本

from random import randint

Battleship_Board =[]


for x in range (0,5):
Battleship_Board.append(["O"] * 5)

def print_Battleship_Board(Battleship_Board):
for row in Battleship_Board:
    print (" ".join(row))

print ("Let's play Battleships!")
print_Battleship_Board(Battleship_Board)

def random_row(Battleship_Board):
return randint(0, len(Battleship_Board) - 1)

def random_col(Battleship_Board):
return randint(0, len(Battleship_Board[0]) - 1)

Battleship_Row = random_row(Battleship_Board)
Battleship_Column = random_col(Battleship_Board)

for turn in range(5):
    Guess_Board_Row = int(input("Guesss the X value"))
    Guess_Board_Column = int(input("Guess the Y value"))

if Guess_Board_Row == Battleship_Row and Guess_Board_Column == Battleship_Column:
    print("You sunk my battleship!")
    break
else:

    if (Guess_Board_Row < 1 or Guess_Board_Row > 5) or (Guess_Board_Column < 1 or Guess_Board_Column > 5):
            print("Apologies, that's not on the grid")

    elif(Battleship_Board[Guess_Board_Row][Guess_Board_Column] == "X"):
            print("You already guessed that value")

    else:
        print("You missed my battleship")
        Battleship_Board[Guess_Board_Row][Guess_Board_Column] = "X"

        print("Turn" + str(turn+1) + " out of 4.")
        print_Battleship_Board(Battleship_Board)

    if turn >=4:
        print("Game Over")

如您所见,我让用户根据列和行的随机值来玩游戏。

输入的版本

Battleship_Board =[]


for x in range (0,5):
Battleship_Board.append(["O"] * 5)



def print_Battleship_Board(Battleship_Board):
for row in Battleship_Board:
    print (" ".join(row))

print ("Let's play Battleships!")
print_Battleship_Board(Battleship_Board)

Battleship_Row =  int(input("Please enter a X value"))
Battleship_Column = int(input("Please enter a Y value"))

if (Battleship_Row < 1 or Battleship_Row > 5) or (Battleship_Column < 1 or Battleship_Row > 5):
print("Apologies, that's not on the grid")

for turn in range(5):
    Guess_Board_Row = int(input("Guess the X value"))
    Guess_Board_Column = int(input("Guess the Y value"))

if Guess_Board_Row == Battleship_Row and Guess_Board_Column == Battleship_Column:
    print("You sunk my battleship!")
    print("My Ship was here: [" + str(Battleship_Row) + "][" + str(Battleship_Column) + "]")
    break

else:
    if turn == 5:
        Battleship_Board[Battleship_Row][Battleship_Column] = "X"
        print_Battleship_Board(Battleship_Board)
        print("Game Over")
        print("My Ship was here: [" + str(Battleship_Row) + "][" + str(Battleship_Column) + "]")

    else:
        if (Guess_Board_Row < 1 or Guess_Board_Row > 5) or (Guess_Board_Column < 1 or Guess_Board_Column > 5):
            print("Apologies, that's not on the grid")

        elif(Battleship_Board[Guess_Board_Row][Guess_Board_Column] == "X"):
            print("You already guessed that value")

        else:
            print("You missed my battleship!")
            Battleship_Board[Guess_Board_Row][Guess_Board_Column] = "X"

        print("Turns taken out of 5:", turn + 1)
        print_Battleship_Board(Battleship_Board)

这是用户输入值的地方。

我想在这两个版本之间建立选项,供用户在启动应用程序时进行选择。我怎样才能做到这一点?

最佳答案

您可以使用命令行参数来允许用户在启动游戏时选择模式:

import sys

if len(sys.argv) > 1 and sys.argv[1] == '<some-value>':
    # Randomize
else:
    # Prompt user for input

有关命令行参数的更多信息 here 。对于用户友好(更有趣)的命令行选项,请查看 argparse .

关于python - 我怎样才能让用户选择玩哪种模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36534795/

相关文章:

php - Python/PHP Tesseract 输出优化技巧

python - Tkinter 网格权重的表现不符合我的预期

python - 车类错误

python - python3中@staticmethod的好处?

python - 计算轴未对齐的两个矩形之间的交集面积

python - 字典Python的智能循环

python - Django 模型字段选择 - 听写不是更好吗?

python - Windows上本地django开发如何配置apache和mod_wsgi

python - 如何让我的 discord 机器人在加入语音 channel 时立即播放音频文件,例如 airhorn solutions 机器人?

python - 在 numpy 数组上应用矢量化和非矢量化函数