我想让用户选择他们是想让坐标随机化,还是他们想要为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)这里是用户输入值的地方。
我希望在这两个版本之间建立选项,供用户在启动应用程序时选择。我怎么能这么做?
发布于 2016-04-10 20:11:54
您可以使用命令行参数,允许用户在启动游戏时选择该模式:
import sys
if len(sys.argv) > 1 and sys.argv[1] == '<some-value>':
# Randomize
else:
# Prompt user for input更多关于命令行参数这里的信息。对于更方便用户的命令行选项,请查看argparse解析。
https://stackoverflow.com/questions/36534795
复制相似问题