开发环境:
IDE:Pycharm
OS:mac Monterey version 12.5
游戏说明:
此游戏是一款扑克牌游戏,扑克牌颜色为红桃,黑桃,方块,梅花。牌值为1-13, JQK为牌值0.5
游戏规则:每轮参与玩家任意个,然后依次发牌,玩家不要牌,如果总牌值不超过11,继续给下一个玩家发牌,如果哪位玩家总牌值超过11, 就爆点,牌子清零。最后计算哪个玩家牌值最高,就算赢家。
源代码如下:
import random
# 最后的玩牌结果
result = {
}
# 组建玩家列表
def build_player(player_lst):
while 1:
player = input('请输入玩家姓名:(q/Q退出)')
if player.upper() == 'Q':
break
player_lst.append(player)
return player_lst
# 生成一副扑克牌
def get_poke_lst():
'''
生成一副牌
:return: 返回一副牌
'''
total_poke_lst = []
color_lst = ['红桃♥️', '黑桃♠️', '方片♦️', '梅花♣️']
num_lst = []
for num in range(1, 14):
num_lst.append(num)
for color in color_lst:
for num in num_lst:
item = (color, num,)
total_poke_lst.append(item)
return total_poke_lst
# 发牌函数
def get_poke(player, poke_lst):
'''
随机发牌,返回牌值
:param player: 玩家名
:param poke_lst: 总的牌列表
:return: score
'''
# 给用户发第一张牌
score = 0
index = random.randint(0, len(poke_lst) - 1)
poke = poke_lst.pop(index)
value = poke[1]
if poke[1] > 10:
value = 0.5
score += value
print(f'给{player}发的牌:{poke[0]}{poke[1]}, 此刻所有牌的面值总和:{score}')
return score
# 玩牌
def play_poke():
player_lst = []
player_lst = build_player(player_lst)
# 生成一副牌
total_poke_lst = get_poke_lst()
for player in player_lst:
# 给用户发第一张牌,牌值
score = get_poke(player, total_poke_lst)
# 用户选择是否继续要
while 1:
choice = input('是否继续要牌(Y/N)?')
choice = choice.upper()
# 用户输入的不是Y/N
if choice not in {
'Y', 'N'}:
print('输入有误,请重新输入')
continue
# 用户输入N, 不继续要牌
if choice == 'N':
print(f'{player}不要牌了')
break
score += get_poke(player, total_poke_lst)
print(f'{player} 此刻所有牌的面值总和:{score}')
# 大于11点, 则用户爆了切分值为0
if score > 11:
print(f'玩家{player}爆了')
score = 0
break
result[player] = score
return result
# 游戏及最后结果
def play():
print('欢迎进入摸大点扑克游戏'.center(60, '='))
result = play_poke()
print('\n')
print(' 最后的成绩 '.center(50, '*'))
print(result)
score_result = list(result.values()) # 分值的列表
for player in result:
if result[player] == max(score_result):
print(f'赢家为 {player}')
# 扑克界面
menu_info = '''
******* 摸大点扑克牌游戏 ********
【1】玩扑克
【2】退 出
'''
if __name__ == '__main__':
while 1:
print(menu_info)
choice_num = input('输入序号:')
if choice_num.isdecimal():
choice_num = int(choice_num)
if choice_num == 1:
play()
elif choice_num == 2:
exit('Bye.')
else:
print('请输入数字【1|2】')
运行结果如下图: