1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        使用Python編程打造一款游戲

        共 4467字,需瀏覽 9分鐘

         ·

        2022-04-14 12:10

        點(diǎn)擊上方“Python共享之家”,進(jìn)行關(guān)注

        回復(fù)“資源”即可獲贈Python學(xué)習(xí)資料

        靜夜四無鄰,荒居舊業(yè)貧。

        大家好,我是皮皮。

        一、前言

        前幾天在Python最強(qiáng)王者交流群有個叫【Chloe】的粉絲問了一個Python小游戲的問題,這里拿出來給大家分享下,一起學(xué)習(xí)下。

        二、解決過程

        看上去代碼有報(bào)錯,截圖如下。

        這個錯誤倒是很常見,因?yàn)閿?shù)據(jù)類型不同,直接相加肯定報(bào)錯,如果需要更改的話,那么需要轉(zhuǎn)一下數(shù)據(jù)類型,這里【沈復(fù)】大佬給出了答案,如下圖所示。

        當(dāng)然了,粉絲的代碼殘缺的太厲害了,少了5-7個函數(shù),【月神】依次補(bǔ)充完整之后,總算可以進(jìn)入游戲了,然后順便找到了這個報(bào)錯位置。

        這里問題還是不少的,【月神】幫忙更新了下代碼,如下:

        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?=?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

        三、總結(jié)

        大家好,我是皮皮。這篇文章主要分享了使用Python編程打造一款小游戲,針對該問題給出了具體的解析和代碼演示,幫助粉絲順利解決了問題。

        最后感謝粉絲【Chloe】提問,感謝【沈復(fù)】、【月神】給出的具體解析和代碼演示,感謝【dcpeng】、【馮誠】等人參與學(xué)習(xí)交流。

        小伙伴們,快快用實(shí)踐一下吧!如果在學(xué)習(xí)過程中,有遇到任何問題,歡迎加我好友,我拉你進(jìn)Python學(xué)習(xí)交流群共同探討學(xué)習(xí)。

        -------------------?End?-------------------

        往期精彩文章推薦:

        歡迎大家點(diǎn)贊,留言,轉(zhuǎn)發(fā),轉(zhuǎn)載,感謝大家的相伴與支持

        想加入Python學(xué)習(xí)群請?jiān)诤笈_回復(fù)【

        萬水千山總是情,點(diǎn)個【在看】行不行

        瀏覽 55
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評論
        圖片
        表情
        推薦
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            超碰人人艹 | 日韩特黄特色大片免费视频 | 我与一位67老妇的性往事 | 亚洲色图导航 | 啊灬啊灬啊灬快灬高潮了女故事 | 婷婷涩嫩草鲁丝久久午夜精品 | 亚洲欧美成人电影 | 欧美成人在线观看 | 少妇大叫好爽受不了午夜视频 | 亚洲在线视频免费观看 |