基于python的猜大小游戏

前言:这是我因为结课要交项目写的一个小游戏,我本身非常不喜欢写注释,因为我自己写的代码我自己一定会看得懂和记得住,所以本文没什么注释。

简介:基于python基本语法编写的,玩家通过猜测电脑投掷的三枚骰子总点数之和的范围,跟电脑进行比赛pk。玩家可以在对局开始前通过修改 max_time 设定对局次数,对局结束后在界面得到完整的对局情况和比赛结果。同时玩家可以通过修改 hard 提高游戏难度。(玩家输入会更完善)

知识点:if语句 / while语句 / for迭代 / ljust / center

代码:这里放的是拆解后的代码

① 年龄判断和初始金额分配:

hard = 0  # int( random.randint(1, 50) ) 注释内是另一个游戏难度
if player_information["player_age"] < 18:
    print("\n\t\t未满18岁不能进入本游戏哦 o(* ̄▽ ̄*)ブ ~")
    sys.exit(0)
elif 18 <= player_information["player_age"] <= 30:
    money = 100 + hard
else:
    money = 300 + hard
player_information["money"] = money
print("{}您好,欢迎进入本游戏,您的初始金额为:{}\n".format(player_information["player_name"], player_information["money"]))

② 游戏说明:ljust和center嵌套使用 (可以分开,但我喜欢写在一起)

print("*".center(50, "*"))
print("!".ljust(48), "!")
print("!".ljust(14), "游戏说明".center(14, "*") .ljust(31),"!" )
print("!".ljust(6), "电脑每次投掷三枚骰子,总点数<10为小,反之为大".center(20, "*") .ljust(30),"!" )
print("!".ljust(7), "玩家猜大或猜小,猜中金额+10,猜错金额-10".center(20, "*") .ljust(31),"!" )
print("!".ljust(48), "!")
print("*".center(50, "*"))
print("\n")

③ 骰子投掷并计算点数总和:

dices = []
   for i in range(0, 3):
      dices.append(random.randint(1, 6))
      total = sum(dices) 

④ 比赛PK:

player_input = input("请输入 大(big) or 小(small) : ")
        if (total >= 10 and p_input == "big") or (total < 10 and p_input == "small"):
            player_information["money"] += 10
            print("\t骰子点数为:{0},点数总数为:{1}".format(dices,total))
            print("\t恭喜您取得本次胜利!!! 金额+10  \^o^/ ")
        elif (total >= 10 and p_input == "small") or (total < 10 and p_input == "big"):
            win_time = 0
            player_information["money"] -= 10;
            print("\t骰子点数为:{}".format(dices))
            print("\t哦吼,您输了,再接再厉!!! 金额-10  T_T ")
            lose_time += 1
        else:
            print("\t输入错误,请重新进入游戏  ′⌒` ")
            sys.exit(0)

⑤ 道具使用:连赢三次触发

 multi = 1
    if win_time % 3 == 0:
       if len(player_properties) > 0:  # 如果玩家有道具 选择是否使用道具
          use_pro = input("请问是否使用道具 是(yes) or 否(no): ")
          if use_pro.lower() == "yes":
              use_pro = str(input("\t请选择所使用的道具编号:{}".format(properties)))
              use_pro_count -= 1
              if use_pro == "a":
                   if use_pro_a > 0:
                       multi = 3
                       use_pro_a -= 1
                       player_properties.remove(player_properties[use_pro_a])
                       print("\t您的金额增加30,a类道具剩余{}个".format(use_pro_a))
                   else:
                       rint("\t您的a类道具不足,请及时购买")
               elif use_pro == "b":
                   if use_pro_b > 0:
                       multi = random.randint(1, 5)
                       use_pro_b -= 1
                       player_properties.remove(player_properties[use_pro_b])
                       print("\t您的金额增加{0},b类道具剩余{1}个".format(10 * int(multi),use_pro_b))
                   else :
                       print("\t您的b类道具不足,请及时购买")
              else:
                   print("\t输入错误,本店没有该道具,您失去了这次机会,下次注意哦  ●'◡'● ")
           elif use_pro.lower() == "no":
               print("\t您已放弃本次使用机会")
           else:
               print("\t输入错误,您错过了本次使用机会,下次注意哦  p≧w≦q")
               player_information["money"] += 10 * int(multi);
               print("\t您现有金额为:{}".format(player_information["money"]))

⑥ 道具购买:

        if player_information["money"] % 100 == 0:  #用户金额若是100的倍数则可购买道具
            shop = input("您现有金额为:{},请问是否购买道具 是(yes) or 否(no): ".format(player_information["money"]))
            if shop.lower() == "yes":
                shop = str(input("\t请选择想要购买的道具编号:{}".format(properties)))
                use_pro_count += 1
                if shop == "a":
                    use_pro_a += 1
                    player_properties.append("a")  #添加道具
                    player_information["money"] -= 20
                    print("\t购买成功! 金额-20 剩余金额为{0} a类道具剩余{1}个".format(player_information["money"], use_pro_a))
                elif shop == "b":
                    use_pro_b += 1
                    player_properties.append("b")  #添加道具
                    player_information["money"] -= 25
                    print("\t购买成功! 金额-25 剩余金额为{0} b类道具剩余{1}个".format(player_information["money"], use_pro_b))
                else:
                    print("\t商城暂未上架该道具,敬请期待后续版本哦  O(∩_∩)O ")
            elif shop.lower() == "no":
                print("\t您已放弃本次购买机会")
            else:
                print("\t输入错误,您错过了本次购买机会,下次注意哦  p≧w≦q")

⑦ 玩家注册:

player_name = input("请玩家填写用户名:")
player_age = input("{}您好,请输入您的年龄:".format(player_name))
player_information = {"player_name": player_name, "player_age": int(player_age)}

⑧ 进入游戏:

result = input("是否进入游戏 是(yes) or 否(no):")
if (result.lower() == "yes"):
    # 省略
    print("游戏结束!{0}一共进行{1}次对局,赢了{2}次".format(player_name,run - 1, (run - lose_time - 1)))
else:
    print("\n\t再见,期待您下次进入本游戏哦  = ̄ω ̄= ")

  • 15
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值