欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  互联网

Pygame实战练习之纸牌21点游戏

程序员文章站 2022-06-24 07:55:07
导语​昨天不是周天嘛?你们在家放松一般都会做什么呢?周末逛逛街,出去走走看电影......这是你们的周末。程序员的周末就是在家躺尸唐诗躺尸,偶尔加班加班加班,或者跟着几个朋友在家消遣时间打...

导语

Pygame实战练习之纸牌21点游戏

昨天不是周天嘛?

你们在家放松一般都会做什么呢?

周末逛逛街,出去走走看电影......这是你们的周末。

程序员的周末就是在家躺尸唐诗躺尸,偶尔加班加班加班,或者跟着几个朋友在家消遣时间打打麻将,扑克牌玩一下!

Pygame实战练习之纸牌21点游戏

尤其是放长假【ps:也没啥假,长假就是过年】在老家的时候,亲戚尤其多,七大姑八大姨的一年好不容易聚一次,打打麻将跟扑克这是常有的事儿,联络下感情这是最快的方式~

说起打扑克,我们经常就是玩儿的二百四、炸金花、三个打一个那就是叫啥名字来着,容我想想......

Pygame实战练习之纸牌21点游戏

​话说真词穷,我们那都是方言撒,我翻译不过来普通话是叫什么了,我估计240你们也没听懂是啥,23333~

Pygame实战练习之纸牌21点游戏

今天的话小编是带大家做一款21点的扑克游戏!

有大佬可优化一下这个代码,做一个精致豪华的界面就好了~~

Pygame实战练习之纸牌21点游戏

正文

游戏规则:21点又名黑杰克,该游戏由2到6个人玩,使用除大小王之外的52张牌,游戏者的目标是使手中的牌的点数之和不超过21点且尽量大。当使用1副牌时,以下每种牌各一张(没有大小王):

Pygame实战练习之纸牌21点游戏

(1)初始化玩家数:

<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('输入玩家数:'))
        except valueerror:
            print('无效输入!')
            continue
        if playercount < 2:
            print('玩家必须大于1!')
            continue
        else:
            break
    try:
        decks = int(input('输入牌副数:(默认等于玩家数)'))
    except valueerror:
        print('已使用默认值!')
        decks = playercount
    print('玩家数:', playercount, ',牌副数:', decks)
    cards = getcards(decks)  # 洗牌</span></span></span>

(2)建立了玩家列表,电脑跟玩家对战。

<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)  # 为各玩家随机排序</span></span></span>

(3)开始会设置2张明牌玩法都可以看到点数。

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#f8f8f8">def gamestart():
    print('为各玩家分2张明牌:')
    for i in range(playercount):  # 为每个玩家分2张明牌
        deal(playerlist[i]['cards'], cards, 2)
        playerlist[i]['score'] = getscore(playerlist[i]['cards'])  # 计算初始得分
        print(playerlist[i]['id'], ' ', getcardname(playerlist[i]['cards']),
              ' 得分 ', playerlist[i]['score'])
        time.sleep(1.5)</span></span></span>

(4)游戏为电脑跟玩家依次分发第三张暗牌,这是别人看不到的。

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#f8f8f8">def gameplay():
    for i in range(playercount):
        print('当前', playerlist[i]['id'])
        if playerlist[i]['id'] == '玩家':  # 玩家
            while(true):
                print('当前手牌:', getcardname(playerlist[i]['cards']))
                _isdeal = input('是否要牌?(y/n)')
                if _isdeal == 'y':
                    deal(playerlist[i]['cards'], cards)
                    print('新牌:', getcardname(playerlist[i]['cards'][-1]))
                    # 重新计算得分:
                    playerlist[i]['score'] = getscore(playerlist[i]['cards'])
                elif _isdeal == 'n':
                    break
                else:
                    print('请重新输入!')
        else:  # 电脑
            while(true):
                if isdeal(playerlist[i]['score']) == 1:  # 为电脑玩家判断是否要牌
                    deal(playerlist[i]['cards'], cards)
                    print('要牌。')
                    # 重新计算得分:
                    playerlist[i]['score'] = getscore(playerlist[i]['cards'])
                else:
                    print('不要了。')
                    break
        time.sleep(1.5)</span></span></span>

(5)随机洗牌:

<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       # 牌副数
    random.shuffle(cardslist)   # 随机洗牌
    return cardslist</span></span></span>

(6)设置牌名字典:

<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)判断胜负:

<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:  # 爆牌直接进入败者列表
            loserlist.append([playerlist[i]['id'],  playerlist[i]['score']])
        else:  # 临时胜者列表
            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)  # 根据分数值排序胜者列表
        for i in range(len(winnerlist)):  # 计算最低分玩家数量
            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)):  # 计算最高分玩家数量
            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)  # 根据分数值排序胜者列表
        for i in range(len(winnerlist)):  # 计算最高分玩家数量
            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>

游戏效果:咳咳咳.......感觉这游戏看运气也看胆量!!

Pygame实战练习之纸牌21点游戏

​总结

Pygame实战练习之纸牌21点游戏

哈哈哈!小编玩游戏比较废,你们要来试试嘛?无聊的时候可以摸摸鱼,打打酱油~

到此这篇关于pygame实战练习之纸牌21点游戏的文章就介绍到这了,更多相关pygame 21点内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!