人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

Pygame實戰(zhàn)練習之紙牌21點游戲

發(fā)布日期:2022-01-01 16:04 | 文章來源:CSDN

導語

昨天不是周天嘛?

你們在家放松一般都會做什么呢?

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

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

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

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

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

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

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

正文

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

(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ù):(默認等于玩家數(shù))'))
 except ValueError:
  print('已使用默認值!')
  decks = playerCount
 print('玩家數(shù):', playerCount, ',牌副數(shù):', decks)
 cards = getCards(decks)  # 洗牌</span></span></span>

(2)建立了玩家列表,電腦跟玩家對戰(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)  # 為各玩家隨機排序</span></span></span>

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

<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)游戲為電腦跟玩家依次分發(fā)第三張暗牌,這是別人看不到的。

<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 # 牌副數(shù)
 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)  # 根據(jù)分數(shù)值排序勝者列表
  for i in range(len(winnerList)):  # 計算最低分玩家數(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)):  # 計算最高分玩家數(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ù)分數(shù)值排序勝者列表
  for i in range(len(winnerList)):  # 計算最高分玩家數(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>

游戲效果:咳咳咳.......感覺這游戲看運氣也看膽量?。?/p>

​總結

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

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

版權聲明:本站文章來源標注為YINGSOO的內容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內容來源于網友推薦、互聯(lián)網收集整理而來,僅供學習參考,不代表本站立場,如有內容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。

相關文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務

7x24全年不間斷在線

專屬顧問服務

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務熱線

關注
微信

關注官方微信
頂部