2017-05-29 69 views
0

我被告知需要将main()函数放入我的代码中以防止错误并确保程序的顺畅。然而,当第一块棋盘出现时,加载我的游戏时退出。为什么程序无法加载?

如果我错过了任何东西(未来的潜在错误),我想知道。

未来我还打算将这个游戏放在使用烧瓶的网站上,玩家使用箭头键选择他/她想要击打的位置,然后按动空间栏。如果有人可以给我一个关于如何开始的想法,我将不胜感激。

感谢您的帮助提前。

该计划是在python基于控制台的2玩家战列舰船游戏:

from random import randint 

game_board = [] 
player_one = { 
    "name": "Player 1", 
    "wins": 0, 
    "lose": 0 
} 
player_two = { 
    "name": "Player 2", 
    "wins": 0, 
    "lose": 0 
} 
total_turns = 0 

# Building our 5 x 5 board 
def build_game_board(board): 
    for item in range(5): 
     board.append(["O"] * 5) 

def show_board(board): 
    print("Find and sink the ship!") 
    for row in board: 
     print(" ".join(row)) 

# Defining ships locations 
def load_game(board): 
    print("WELCOME TO BATTLESHIP!") 
    print("START") 
    del board[:] 
    build_game_board(board) 
    show_board(board) 
    ship_col = randint(1, len(board)) 
    ship_row = randint(1, len(board[0])) 
    return { 
     'ship_col': ship_col, 
     'ship_row': ship_row, 
    } 

ship_points = load_game(game_board) 

# Players will alternate turns. 
def player_turns(): 
    if total_turns % 2 == 0: 
     return player_two 

    else: 
     return player_one 

# Allows new game to start 
def play_again(): 
    answer = input("Would you like to play again?") 
    if answer == "yes" or answer == "y": 
     total_turns = 0 
     ship_points = load_game(game_board) 

    else: 
     print("Thanks for playing!") 
     exit() 

# What will be done with players guesses 
def input_check(ship_row, ship_col, player, board): 
    guess_col = 0 
    guess_row = 0 
    while True: 
     try: 
      guess_row = int(input("Guess Row:")) - 1 
      guess_col = int(input("Guess Col:")) - 1 
     except ValueError: 
      print("Enter a number only.") 
      continue 
     else: 
      break 
    match = guess_row == ship_row - 1 and guess_col == ship_col - 1 
    not_on_game_board = (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4) 

    if match: 
     player["wins"] += 1 
     print("Congratulations! You sunk my battleship!") 
     print("Thanks for playing!") 
     play_again() 

    elif not match: 
     if not_on_game_board: 
      print("Oops, that's not even in the ocean.") 

     elif board[guess_row][guess_col] == "X": 
      print("You guessed that one already.") 

     else: 
      print("You missed my battleship!") 
      board[guess_row][guess_col] = "X" 
     show_board(game_board) 

    else: 
     return 0 


def main(): 
    begin = input('Type \'start\' to begin.') 
    while (begin != str('start')): 
     begin = input('Type \'start\' to begin.') 
    for games in range(3): 
     for turns in range(6): 
      total_turns += 1 
      if player_turns() == player_one: 
       print("Player One") 
       input_check(
        ship_points['ship_row'], 
        ship_points['ship_col'], 
        player_one, game_board 
       ) 

      elif player_turns() == player_two: 
       print("Player Two") 
       input_check(
        ship_points['ship_row'], 
        ship_points['ship_col'], 
        player_two, game_board 
       ) 

      else: 
       continue 

      if total_turns == 6: 
       print("The game is a draw") 
       play_again() 

      else: 
       continue 

if __name__ == "main": 
    main() 
+0

什么是你的** **的问题? – moritzg

+0

为什么游戏退出? – zalidael

回答

3

更改为以下:

if __name__ == "__main__": 
+0

但这不是你唯一的问题'UnboundLocalError:本地变量'total_turns'参考前引用' – moritzg

+0

工作正常!谢谢! – zalidael

+0

@moritzg为了解决这个问题,我把变量放到主函数中。所以total_turns = 0就在增量开始之前。 – zalidael

相关问题