dark room 的精简精简精简精简版(python类练习)

功能:砍树,卖木头,买工具

import sys, time

class Warmcabin:  
    def __init__(self,name):#初始化
        self.name = name
        self.wood = 500
        self.money = 10000
        self.tool = "hand"
        print(self.name)
        print("木头数量",self.wood,"钱",self.money,"当前工具",self.tool)
    
    def cuttree(self,tool):#砍树
        for i in range(11):
            if i != 10:
                sys.stdout.write("==")
            else:
                sys.stdout.write("== " + str(i*10)+"%/100%")
            sys.stdout.flush()
            time.sleep(0.2)
        
        print()
        if self.tool == "hand" :
            self.wood += 5
        elif self.tool == "axe" :
            self.wood += 10
        elif self.tool == "Guangtouqiang" :
            self.wood += 25
        print("砍树成功")   
        print()
        print(self.name)
        print("木头数量",self.wood,"钱",self.money,"当前工具",self.tool)
        pass

    def sellwood(self,wood):#卖树
        print("当前剩余木头数量:",self.wood,"       正版光头强收购站,我们的兑换比例是1:1")
        amount=int(input("请输入要卖的数量:"))
        if amount<0 :
            print("卖出无效,不支持负数")
        elif amount <= self.wood:
            self.wood -= amount
            self.money += amount
            print("出售成功:","木头数量-",amount,"钱+",amount)
            print()
            print(self.name)
            print("木头数量",self.wood,"钱",self.money,"当前工具",self.tool)
        else:
            print("卖出无效,原因是当前木头数量小于要出售的数量")               
        pass

    def buytool(self,money):#买工具
        print("你现在的钱:",self.money)
        print(toollist)
        tool=input("请输入你要买的工具:")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
        if tool == "axe":
            if self.money >= 100:
                self.money -= 100
                self.tool = "axe"
                del toollist["axe"]
                print("购买成功")   
                print() 
            else:
                print("购买无效,原因是钱不够")
                
       
        if tool == "hatchet":
            if self.money >= 500:
                self.money -= 500
                self.tool = "hatchet"
                del toollist["hatchet"]
                print("购买成功")   
                print() 
            else:
                print("购买无效,原因是钱不够")

        if tool == "Guangtouqiang":
            if self.money >= 1000:
                self.money -= 1000
                self.tool = "Guangtouqiang"
                del toollist["Guangtouqiang"]
                print("购买成功")   
                print() 
            else:
                print("购买无效,原因是钱不够")

       
        
        print(self.name)
        print("木头数量",self.wood,"钱",self.money,"当前工具",self.tool)
        pass

        
toollist={
     "axe":100,"hatchet":500,"Guangtouqiang":1000}#砍树工具

print("欢迎来到darkroom")
print()
na=input("请输入你的名字:")

zdh = Warmcabin(na)    #游戏初始化

br = True

print()

while br==True :       #开始游戏
    print(''' 1、砍树  2、卖木头  3、买工具  4、退出''')
    num=input("请输入选项:")
    if num == "1" :
        zdh.cuttree("")
        print()
    elif num == "2" :
        zdh.sellwood("")
        print()
    elif num == "3" :
        zdh.buytool("")
        print()
    elif num == "4" :
        br = False

你可能感兴趣的:(python,游戏)