我正试图在我的战舰游戏中实现一个保存游戏功能

时间:2016-03-10 20:11:59

标签: python

我试图让每个玩家移动后我的游戏都会保存棋盘但是我不知道该怎么做。

def SavedGame(Filename, Board):
  BoardFile = open(Filename, 'w')
  for Row in range(11):
    for Column in range(11):
      File.write(Board[Row][Column])
      print(Board[Row][Column])
  BoardFile.close()

到目前为止我只有这个,但这没有任何作用,并且在代码的底部调用已保存的游戏

if __name__ == "__main__":
  TRAININGGAME = "Training.txt"
  LOADEDGAME = "SavedGame.txt"
  MenuOption = 0
  while not MenuOption == 9:
    Board = SetUpBoard()
    Ships = [["Aircraft Carrier", 5], ["Battleship", 4], ["Submarine", 3],       ["Destroyer", 3], ["Patrol Boat", 2], ["Newpiece", 1]]
DisplayMenu()
MenuOption = GetMainMenuChoice()
if MenuOption == 1:
  PlaceRandomShips(Board, Ships)
  PlayGame(Board,Ships)
if MenuOption == 2:
  LoadGame(TRAININGGAME, Board)
  PlayGame(Board, Ships)
if MenuOption == 3:
  SavedGame(LOADEDGAME, Board)
  PlayGame(Board, Ships)

以下是完整代码:

import random

def SavedGame(Filename, Board):
  BoardFile = open(Filename, 'w')
  for Row in range(11):
    for Column in range(11):
      File.write(Board[Row][Column])
      print(Board[Row][Column])
  BoardFile.close()

colandrow = ["0","1","2","3","4","5","6","7","8","9","10"]  
def GetRowColumn():
  print()
  Column = input("Please enter column: ")
  while Column not in colandrow:
    Column = input("Please enter column: ")
  Column = int(Column)
  Row = input("Please enter row: ")
  while Row not in colandrow:
    Row = input("Please enter row: ")
  Row = int(Row)
  print()
  return Row, Column

def MakePlayerMove(Board, Ships):
  Row, Column = GetRowColumn()
  if Board[Row][Column] == "m" or Board[Row][Column] == "h":
    print("Sorry, you have already shot at the square (" + str(Column) + "," + str(Row) + "). Please try again.")
  elif Board[Row][Column] == "-":
    print("Sorry, (" + str(Column) + "," + str(Row) + ") is a miss.")
    Board[Row][Column] = "m"
  else:
    ("Hit at (" + str(Column) + "," + str(Row) + ").")
    if Board[Row][Column] == 'A':
      print("""You have hit an aircraft
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▓▒▒▒▒▒▒▒▒▓▒
 ▒▒▓▓▓▓▓▓▓▓▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
""")
    elif Board[Row][Column] == 'B':
      print("""You have hit a Battleshit
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▓▒▒▒▒▒▒▒▒▓▒
 ▒▒▓▓▓▓▓▓▓▓▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
""")
    elif Board[Row][Column] ==  'S':
      print("""You have hit a Submarine
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▓▒▒▒▒▒▒▒▒▓▒
 ▒▒▓▓▓▓▓▓▓▓▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
""")
    elif Board[Row][Column] == 'D':
      print("""You have hit a destroyer
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▓▒▒▒▒▒▒▒▒▓▒
 ▒▒▓▓▓▓▓▓▓▓▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
""")
    elif Board[Row][Column] == 'P':
      print("""You have hit a patrol boat
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▓▒▒▒▒▒▒▒▒▓▒
 ▒▒▓▓▓▓▓▓▓▓▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
""")
    elif Board[Row][Column] == 'n':
      print ("""You have hit the new piece
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▓▒▒▓▒▒▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
 ▒▓▒▒▒▒▒▒▒▒▓▒
 ▒▒▓▓▓▓▓▓▓▓▒▒
 ▒▒▒▒▒▒▒▒▒▒▒▒
""")
    Board[Row][Column] = "h"

def SetUpBoard():
  Board = []
  for Row in range(11):
    BoardRow = []
    for Column in range(11):
      BoardRow.append("-")
    Board.append(BoardRow)
  return Board

number = 10 #made it into a global variable instead of a local variable.!
def LoadGame(Filename, Board):
  BoardFile = open(Filename, "r")
  for Row in range(number):
    Line = BoardFile.readline()
    for Column in range(number):
      Board[Row][Column] = Line[Column]
  BoardFile.close()

def PlaceRandomShips(Board, Ships):
  for Ship in Ships:
    Valid = False
    while not Valid:
      Row = random.randint(0, 10) 
      Column = random.randint(0, 10) 
      HorV = random.randint(0, 1)
      if HorV == 0:
        Orientation = "v" 
      else:
        Orientation = "h" 
      Valid = ValidateBoatPosition(Board, Ship, Row, Column, Orientation)
    print("Computer placing the " + Ship[0])
    PlaceShip(Board, Ship, Row, Column, Orientation)

def PlaceShip(Board, Ship, Row, Column, Orientation):
  if Orientation == "v":
    for Scan in range(Ship[1]):
      Board[Row + Scan][Column] = Ship[0][0]
  elif Orientation == "h":
    for Scan in range(Ship[1]):
      Board[Row][Column + Scan] = Ship[0][0]

def ValidateBoatPosition(Board, Ship, Row, Column, Orientation):
  if Orientation == "v" and Row + Ship[1] > 10:
    return False
  elif Orientation == "h" and Column + Ship[1] > 10:
    return False
  else:
    if Orientation == "v":
      for Scan in range(Ship[1]):
        if Board[Row + Scan][Column] != "-":
          return False
    elif Orientation == "h":
      for Scan in range(Ship[1]):
        if Board[Row][Column + Scan] != "-":
          return False
  return True

def CheckWin(Board):
  for Row in range(10):
    for Column in range(10):
      if Board[Row][Column] in ["A","B","S","D","P","N"]:
        return False
  return True

def PrintBoard(Board):
  print()
  print("The board looks like this: ")  
  print()
  print (" ", end="")
  for Column in range(11):
    print(" " + str(Column) + "  ", end="")
  print()
  for Row in range(11):
    print (str(Row) + " ", end="")
    for Column in range(11):
      if Board[Row][Column] == "-":
        print(" ", end="")
      elif Board[Row][Column] in ["A","B","S","D","P","N"]:
        print(" ", end="")                
      else:
        print(Board[Row][Column], end="")
      if Column != 10:
        print(" | ", end="")
    print()



def DisplayMenu():
  print("MAIN MENU")
  print()
  print("1. Start new game")
  print("2. Load training game")
  print("3. Load a saved game")
  print("9. Quit")
  print()

def GetMainMenuChoice():
  print("Please enter your choice: ", end="")
  Choice = input()
  while Choice not in ["1","2","3","9"]:
    Choice = input("Please enter your choice: ")
  Choice = int(Choice)
  print()
  return Choice

def PlayGame(Board, Ships):
  GameWon = False
  while not GameWon:
    PrintBoard(Board)
    MakePlayerMove(Board, Ships)
    GameWon = CheckWin(Board)
    if GameWon:
      print("All ships sunk!")
      print()

if __name__ == "__main__":
  TRAININGGAME = "Training.txt"
  LOADEDGAME = "SavedGame.txt"
  MenuOption = 0
  while not MenuOption == 9:
    Board = SetUpBoard()
    Ships = [["Aircraft Carrier", 5], ["Battleship", 4], ["Submarine", 3], ["Destroyer", 3], ["Patrol Boat", 2], ["Newpiece", 1]]
    DisplayMenu()
    MenuOption = GetMainMenuChoice()
    if MenuOption == 1:
      PlaceRandomShips(Board, Ships)
      PlayGame(Board,Ships)
    if MenuOption == 2:
      LoadGame(TRAININGGAME, Board)
      PlayGame(Board, Ships)
    if MenuOption == 3:
      SavedGame(LOADEDGAME, Board)
      PlayGame(Board, Ships)

我也想制作一个2人游戏,所以我可以对抗电脑,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:-1)

如果您想存储数据,可以使用PlayerPrefs

执行此操作

http://docs.unity3d.com/ScriptReference/PlayerPrefs.html

PlayerPrefs.SetInt(30 "number");

并获取数据:

PlayerPrefs.GetInt("number"); // returns 30