栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

基于python的猜大小游戏

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于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("ntt未满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("nt再见,期待您下次进入本游戏哦  = ̄ω ̄= ")

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/843916.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号