Flask接口签名sign原理与实例代码浅析
311
2022-08-30
使用Python编程打造一款游戏(python 制作游戏)
大家好,我是皮皮。
一、前言 前几天在Python最强王者交流群有个叫【Chloe】的粉丝问了一个Python小游戏的问题,这里拿出来给大家分享下,一起学习下。
二、解决过程 看上去代码有报错,截图如下。
这个错误倒是很常见,因为数据类型不同,直接相加肯定报错,如果需要更改的话,那么需要转一下数据类型,这里【沈复】大佬给出了答案,如下图所示。
当然了,粉丝的代码残缺的太厉害了,少了5-7个函数,【月神】依次补充完整之后,总算可以进入游戏了,然后顺便找到了这个报错位置。
这里问题还是不少的,【月神】帮忙更新了下代码,如下:
def replay(): key = input('Do you want to play again? Enter Yes or No: ') return True if key[0].upper() == 'Y' else False
这样的话,就完美解决了。
最后分享下这个游戏的完整的代码给大家,感兴趣的小伙伴们可以玩玩看。
print('Welcome to Tic Tac Toe!')
from IPython.display import clear_output import random
def choose_first(): if random.randint(0,1) == 0: return 'player2' else: return 'player1'
def player_input(): marker = ''
while not (marker == 'X' or marker == 'O'): marker = input("Do you want to be X or O? ").upper()if marker == 'X': return 'X'else: return 'O'
def player_choice(board): position = 0
while position not in [1, 2, 3, 4, 5, 6, 7, 8, 9] or not space_check(board, position): position = int(input('Choose your next position: (1-9): '))return position
def space_check(board, position): return board[position] == ' '
def full_board_check(board): for i in range(1,10): if space_check(board,i): return False return True
def replay(): key = input('Do you want to play again? Enter Yes or No: ') return True if key[0].upper() == 'Y' else False
def place_marker(board, marker, position): board[position] = marker
def win_check(board, mark): return ( (board[1]mark and board[2]mark and board[3]mark) or (board[4]mark and board[5]mark and board[6]mark) or (board[7]mark and board[8]mark and board[9]mark) or (board[1]mark and board[4]mark and board[7]mark) or (board[2]mark and board[5]mark and board[8]mark) or (board[3]mark and board[6]mark and board[9]mark) or (board[1]mark and board[5]mark and board[9]mark) or (board[3]mark and board[5]mark and board[7]mark) )
def display_board(board): clear_output()
print(' | |')print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])print(' | |')print('-----------')print(' | |')print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])print(' | |')print('-----------')print(' | |')print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])print(' | |')
while True: # Reset the board theBoard = [' '] * 10 player1_marker = player_input() player2_marker = player_input()
turn = choose_first()print(turn + ' will go first')play_game = input('Are you ready to play? yes or no? ')if play_game[0].lower() == 'y': game_on = Trueelse: game_on = Falsewhile game_on: if turn == 'Player1': # Player1 turn display_board(theBoard) position = player_choice(theBoard) place_marker(theBoard, player1_marker, position) if win_check(theBoard, player1_marker): display_board(theBoard) print('Congratulations! You have won the game!') game_on = False else: if full_board_check(theBoard): display_board(theBoard) print('The game is a draw!') break else: turn = 'Player2' else: # player2 turn display_board(theBoard) position = player_choice(theBoard) place_marker(theBoard, player2_marker, position) if win_check(theBoard, player2_marker): display_board(theBoard) print('Player2 has won!') game_on = False else: if full_board_check(theBoard): display_board(theBoard) print('The game is a draw!') break else: turn = 'Player1'if not replay(): break
三、总结 大家好,我是皮皮。这篇文章主要分享了使用Python编程打造一款小游戏,针对该问题给出了具体的解析和代码演示,帮助粉丝顺利解决了问题。
最后感谢粉丝【Chloe】提问,感谢【沈复】、【月神】给出的具体解析和代码演示,感谢【dcpeng】、【冯诚】等人参与学习交流。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~