为了账号安全,请及时绑定邮箱和手机立即绑定

使用Python编程打造一款游戏

大家好,我是皮皮。

一、前言

前几天在Python最强王者交流群有个叫【Chloe】的粉丝问了一个Python小游戏的问题,这里拿出来给大家分享下,一起学习下。

https://img1.sycdn.imooc.com//6296d7020001a73506400464.jpg


二、解决过程

看上去代码有报错,截图如下。

https://img1.sycdn.imooc.com//6296d7030001611c06400305.jpg


这个错误倒是很常见,因为数据类型不同,直接相加肯定报错,如果需要更改的话,那么需要转一下数据类型,这里【沈复】大佬给出了答案,如下图所示。

https://img1.sycdn.imooc.com//6296d704000106e806400545.jpg


当然了,粉丝的代码残缺的太厉害了,少了5-7个函数,【月神】依次补充完整之后,总算可以进入游戏了,然后顺便找到了这个报错位置。

https://img1.sycdn.imooc.com//6296d70400016f0b06400498.jpg


这里问题还是不少的,【月神】帮忙更新了下代码,如下:

def replay():
    key = input('Do you want to play again? Enter Yes or No: ')    return True if key[0].upper() == 'Y' else False

https://img1.sycdn.imooc.com//6296d7050001023906400504.jpg


这样的话,就完美解决了。

https://img1.sycdn.imooc.com//6296d7050001b32b06400528.jpg


最后分享下这个游戏的完整的代码给大家,感兴趣的小伙伴们可以玩玩看。

print('Welcome to Tic Tac Toe!')from IPython.display import clear_outputimport randomdef 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 positiondef 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 Truedef replay():
    key = input('Do you want to play again? Enter Yes or No: ')    return True if key[0].upper() == 'Y' else Falsedef place_marker(board, marker, position):
    board[position] = markerdef 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 = True
    else:
        game_on = False

    while 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

https://img1.sycdn.imooc.com//6296d7050001049b06400354.jpg


三、总结

大家好,我是皮皮。这篇文章主要分享了使用Python编程打造一款小游戏,针对该问题给出了具体的解析和代码演示,帮助粉丝顺利解决了问题。

最后感谢粉丝【Chloe】提问,感谢【沈复】、【月神】给出的具体解析和代码演示,感谢【dcpeng】、【冯诚】等人参与学习交流。

小伙伴们,快快用实践一下吧!如果在学习过程中,有遇到任何问题,欢迎加我好友,我拉你进Python学习交流群共同探讨学习。


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消