欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pygame實(shí)戰(zhàn)練習(xí)之紙牌21點(diǎn)游戲

 更新時(shí)間:2021年09月23日 10:12:01   作者:顧木子吖  
21點(diǎn)想必是很多人童年時(shí)期的經(jīng)典游戲,我們依舊能記得抱個(gè)老人機(jī)娛樂的場(chǎng)景,下面這篇文章主要給大家介紹了關(guān)于如何利用python寫一個(gè)簡單的21點(diǎn)小游戲的相關(guān)資料,需要的朋友可以參考下

導(dǎo)語

圖片

昨天不是周天嘛?

你們?cè)诩曳潘梢话愣紩?huì)做什么呢?

周末逛逛街,出去走走看電影......這是你們的周末。

程序員的周末就是在家躺尸唐詩躺尸,偶爾加班加班加班,或者跟著幾個(gè)朋友在家消遣時(shí)間打打麻將,撲克牌玩一下!

å³äºæ¯å¤©ç¡è§æå®çæç¬è¯´è¯´æ¯æç¬¬ä¸ä¸ªè¯´æå®çæ¯æ è¾ç个æ§ç½

尤其是放長假【ps:也沒啥假,長假就是過年】在老家的時(shí)候,親戚尤其多,七大姑八大姨的一年好不容易聚一次,打打麻將跟撲克這是常有的事兒,聯(lián)絡(luò)下感情這是最快的方式~

說起打撲克,我們經(jīng)常就是玩兒的二百四、炸金花、三個(gè)打一個(gè)那就是叫啥名字來著,容我想想......

äºæ¬¡å½æ°å¾é¾å

​話說真詞窮,我們那都是方言撒,我翻譯不過來普通話是叫什么了,我估計(jì)240你們也沒聽懂是啥,23333~

ä»å京 æ·±å³åå°å¹¿å·,æä¸æ¬¡æå¨å°åº¦å£é³ä¸,ææ ·æè½é¿åæ²å§

今天的話小編是帶大家做一款21點(diǎn)的撲克游戲!

有大佬可優(yōu)化一下這個(gè)代碼,做一個(gè)精致豪華的界面就好了~~

圖片

正文

游戲規(guī)則:21點(diǎn)又名黑杰克,該游戲由2到6個(gè)人玩,使用除大小王之外的52張牌,游戲者的目標(biāo)是使手中的牌的點(diǎn)數(shù)之和不超過21點(diǎn)且盡量大。當(dāng)使用1副牌時(shí),以下每種牌各一張(沒有大小王):

(1)初始化玩家數(shù):

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#f8f8f8">def iniGame():
    global playerCount, cards
    while(True):
        try:
            playerCount = int(input('輸入玩家數(shù):'))
        except ValueError:
            print('無效輸入!')
            continue
        if playerCount < 2:
            print('玩家必須大于1!')
            continue
        else:
            break
    try:
        decks = int(input('輸入牌副數(shù):(默認(rèn)等于玩家數(shù))'))
    except ValueError:
        print('已使用默認(rèn)值!')
        decks = playerCount
    print('玩家數(shù):', playerCount, ',牌副數(shù):', decks)
    cards = getCards(decks)  # 洗牌</span></span></span>

(2)建立了玩家列表,電腦跟玩家對(duì)戰(zhàn)。

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#f8f8f8">def createPlayerList():
    global playerList
    playerList = []
    for i in range(playerCount):
        playerList += [{'id': '', 'cards': [], 'score': 0}].copy()
        playerList[i]['id'] = '電腦' + str(i+1)
    playerList[playerCount-1]['id'] = '玩家'
    random.shuffle(playerList)  # 為各玩家隨機(jī)排序</span></span></span>

(3)開始會(huì)設(shè)置2張明牌玩法都可以看到點(diǎn)數(shù)。

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#f8f8f8">def gameStart():
    print('為各玩家分2張明牌:')
    for i in range(playerCount):  # 為每個(gè)玩家分2張明牌
        deal(playerList[i]['cards'], cards, 2)
        playerList[i]['score'] = getScore(playerList[i]['cards'])  # 計(jì)算初始得分
        print(playerList[i]['id'], ' ', getCardName(playerList[i]['cards']),
              ' 得分 ', playerList[i]['score'])
        time.sleep(1.5)</span></span></span>

(4)游戲?yàn)殡娔X跟玩家依次分發(fā)第三張暗牌,這是別人看不到的。

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#f8f8f8">def gamePlay():
    for i in range(playerCount):
        print('當(dāng)前', playerList[i]['id'])
        if playerList[i]['id'] == '玩家':  # 玩家
            while(True):
                print('當(dāng)前手牌:', getCardName(playerList[i]['cards']))
                _isDeal = input('是否要牌?(y/n)')
                if _isDeal == 'y':
                    deal(playerList[i]['cards'], cards)
                    print('新牌:', getCardName(playerList[i]['cards'][-1]))
                    # 重新計(jì)算得分:
                    playerList[i]['score'] = getScore(playerList[i]['cards'])
                elif _isDeal == 'n':
                    break
                else:
                    print('請(qǐng)重新輸入!')
        else:  # 電腦
            while(True):
                if isDeal(playerList[i]['score']) == 1:  # 為電腦玩家判斷是否要牌
                    deal(playerList[i]['cards'], cards)
                    print('要牌。')
                    # 重新計(jì)算得分:
                    playerList[i]['score'] = getScore(playerList[i]['cards'])
                else:
                    print('不要了。')
                    break
        time.sleep(1.5)</span></span></span>

(5)隨機(jī)洗牌:

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#f8f8f8">def getCards(decksNum):
    cardsList = ['Aa', 'Ab', 'Ac', 'Ad',
                 'Ka', 'Kb', 'Kc', 'Kd',
                 'Qa', 'Qb', 'Qc', 'Qd',
                 'Ja', 'Jb', 'Jc', 'Jd',
                 '0a', '0b', '0c', '0d',
                 '9a', '9b', '9c', '9d',
                 '8a', '8b', '8c', '8d',
                 '7a', '7b', '7c', '7d',
                 '6a', '6b', '6c', '6d',
                 '5a', '5b', '5c', '5d',
                 '4a', '4b', '4c', '4d',
                 '3a', '3b', '3c', '3d',
                 '2a', '2b', '2c', '2d']
    cardsList *= decksNum       # 牌副數(shù)
    random.shuffle(cardsList)   # 隨機(jī)洗牌
    return cardsList</span></span></span>

(6)設(shè)置牌名字典:

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#f8f8f8">cardNameDict = {'Aa': '黑桃A', 'Ab': '紅桃A', 'Ac': '梅花A', 'Ad': '方片A',
                'Ka': '黑桃K', 'Kb': '紅桃K', 'Kc': '梅花K', 'Kd': '方片K',
                'Qa': '黑桃Q', 'Qb': '紅桃Q', 'Qc': '梅花Q', 'Qd': '方片Q',
                'Ja': '黑桃J', 'Jb': '紅桃J', 'Jc': '梅花J', 'Jd': '方片J',
                '0a': '黑桃10', '0b': '紅桃10', '0c': '梅花10', '0d': '方片10',
                '9a': '黑桃9', '9b': '紅桃9', '9c': '梅花9', '9d': '方片9',
                '8a': '黑桃8', '8b': '紅桃8', '8c': '梅花8', '8d': '方片8',
                '7a': '黑桃7', '7b': '紅桃7', '7c': '梅花7', '7d': '方片7',
                '6a': '黑桃6', '6b': '紅桃6', '6c': '梅花6', '6d': '方片6',
                '5a': '黑桃5', '5b': '紅桃5', '5c': '梅花5', '5d': '方片5',
                '4a': '黑桃4', '4b': '紅桃4', '4c': '梅花4', '4d': '方片4',
                '3a': '黑桃3', '3b': '紅桃3', '3c': '梅花3', '3d': '方片3',
                '2a': '黑桃2', '2b': '紅桃2', '2c': '梅花2', '2d': '方片2'}</span></span></span>

(7)判斷勝負(fù):

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#f8f8f8">def showWinAndLose():
    loserList = []  # [['id', score], ['id', score], ...]
    winnerList = []  # [['id', score], ['id', score], ...]
    winnerCount = 0
    loserCount = 0
    for i in range(playerCount):
        if playerList[i]['score'] > 21:  # 爆牌直接進(jìn)入敗者列表
            loserList.append([playerList[i]['id'],  playerList[i]['score']])
        else:  # 臨時(shí)勝者列表
            winnerList.append([playerList[i]['id'], playerList[i]['score']])
    if len(winnerList) == 0:  # 極端情況:全部爆牌
        print('全部玩家爆牌:')
        for i in range(len(loserList)):
            print(loserList[i][0], loserList[i][1])
    elif len(loserList) == 0:  # 特殊情況:無人爆牌
        winnerList.sort(key=lambda x: x[1], reverse=True)  # 根據(jù)分?jǐn)?shù)值排序勝者列表
        for i in range(len(winnerList)):  # 計(jì)算最低分玩家數(shù)量
            if i != len(winnerList)-1:
                if winnerList[-i-1][1] == winnerList[-i-2][1]:
                    loserCount = (i+2)
                else:
                    if loserCount == 0:
                        loserCount = 1
                    break
            else:
                loserCount = len(loserList)
        if loserCount == 1:
            loserList.append(winnerList.pop())
        else:
            while(len(loserList) != loserCount):
                loserList.append(winnerList.pop())
        for i in range(len(winnerList)):  # 計(jì)算最高分玩家數(shù)量
            if i != len(winnerList)-1:
                if winnerList[i][1] == winnerList[i+1][1]:
                    winnerCount = (i+2)
                else:
                    if winnerCount == 0:
                        winnerCount = 1
                    break
            else:
                winnerCount = len(winnerList)
        while(len(winnerList) != winnerCount):
            winnerList.pop()
        print('獲勝:')
        for i in range(len(winnerList)):
            print(winnerList[i][0], winnerList[i][1])
        print('失?。?)
        for i in range(len(loserList)):
            print(loserList[i][0], loserList[i][1])
    else:  # 一般情況:有人爆牌
        winnerList.sort(key=lambda x: x[1], reverse=True)  # 根據(jù)分?jǐn)?shù)值排序勝者列表
        for i in range(len(winnerList)):  # 計(jì)算最高分玩家數(shù)量
            if i != len(winnerList)-1:
                if winnerList[i][1] == winnerList[i+1][1]:
                    winnerCount = (i+2)
                else:
                    if winnerCount == 0:
                        winnerCount = 1
                    break
            else:
                winnerCount = len(winnerList)
        while(len(winnerList) != winnerCount):
            winnerList.pop()
        print('獲勝:')
        for i in range(len(winnerList)):
            print(winnerList[i][0], winnerList[i][1])
        print('失敗:')
        for i in range(len(loserList)):
            print(loserList[i][0], loserList[i][1])</span></span></span>

游戲效果:咳咳咳.......感覺這游戲看運(yùn)氣也看膽量??!

​總結(jié)

圖片

哈哈哈!小編玩游戲比較廢,你們要來試試嘛?無聊的時(shí)候可以摸摸魚,打打醬油~

到此這篇關(guān)于Pygame實(shí)戰(zhàn)練習(xí)之紙牌21點(diǎn)游戲的文章就介紹到這了,更多相關(guān)Pygame 21點(diǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論