python写小游戏需要注意的点_用Python实现21点小游戏(简易)

import time

import random

def welcome_game():

print('欢迎光临21点小游戏')

time.sleep(1) # 休眠1秒

print('祝您玩的愉快')

game_cmd()

# 判断是否游戏

def game_cmd():

f_command = input('是否进入游戏?y(进入)/n(退出)')

if f_command == 'y':

game_start()

elif f_command == 'n':

print('期待您的下次光临')

quit()

else:

print('输入错误,请查证后再次输入')

game_cmd()

# 新扑克牌列表

def card_list():

f_list = ['J','Q','K','A']*4

card_list =[i for i in range(2,11)]*4

card_list.extend(f_list)

# 打乱扑克牌顺序

random.shuffle(card_list)

return card_list

# 概率控制

def computer_p(score):

# 前面的数占总数的百分比

p = (21-score)/13

# 如果符合这些情况,就继续要牌,否则返回0

if p > 1/4 and score < 13 or p > 1/3 and score <16 or p > 1/2 and score <17 or p > 3/4 and score <18:

x = 1

else :

x = 0

return x

# 计算得分

def sorce(score_list):

score = 0

num = score_list.count('A')

for i in score_list:

if i == 'J' or i =='Q' or i == 'K':

score += 10

elif i == 'A':

score += 1

else:

score += i

# 21点最多加1个10分

if num >= 1 and score < 12:

return score+10

else:

return score

# 判断函数

def pd_fun(new_card_list):

if len(new_card_list) > 8:

again_cmd = input('请选择是否继续?y/n')

if again_cmd == 'y':

x = True

person = []

computer = []

return x, person, computer

else:

quit()

else:

again_cmd = input('请选择是否继续?y/n')

print('我要重新取牌了')

if again_cmd == 'y':

game_start()

else:

quit()

# 开始游戏

def game_start():

person = []

computer = []

# 取出一副顺序随机的扑克牌

new_card_list = card_list()

person_score = 0

computer_score = 0

x = 1

while x:

# 选择是否要牌

Pcmd = input('请选择是否要牌?:y/n')

Ccmd = '1'

if Pcmd =='y':

# 取出的牌和剩余牌数

card= new_card_list.pop(-1)

person.append(card)

person_score = sorce(person)

print('你的牌为:', person)

print('点数和为:', person_score)

if person_score > 21:

print('你输了!')

# 调用函数判断是否继续玩

x,person,computer = pd_fun(new_card_list)

else:

Pcmd = 'n'

if computer_p(sorce(computer)) == 1:

card = new_card_list.pop(-1)

computer.append(card)

computer_score = sorce(computer)

#print('电脑牌为:', computer)

#print('点数和为:', computer_score)

if computer_score > 21:

print('电脑输了')

x,person,computer = pd_fun(new_card_list)

else:

Ccmd = 'n'

if Pcmd == 'n' and Ccmd == 'n':

if person_score > computer_score:

print('你的牌面:{},电脑牌面{},你赢了!'.format(person,computer))

elif person_score == computer_score:

print('你的牌面:{},电脑牌面{},平局!'.format(person,computer))

else:

print('你的牌面:{},电脑牌面{},电脑赢!'.format(person,computer))

x,person,computer = pd_fun(new_card_list)

if __name__ == '__main__':

welcome_game()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值