python - 我如何更改类内的变量?在 python

标签 python

我已经开始编写一款闲置游戏,但稍后我会加入一些类型的输入。

我的问题是我的 class 库存没有将 intdef chop/mine 添加到我的 variable "SP_wood/SP_stone",它只是将 variable 替换为它从 def chop/mine 中获得的数字。我试图将 int 直接提供给 def addwood/addstone 但这对我不起作用。 += 应该起作用并将两者相加。我应该在 class 之外创建 variable 并使其成为 global 吗?

import random
import time
idle = True


class Taskassigner:
    def __init__(self, tasknum):
        self.tasknum = tasknum
    def choosejob(self):
        if self.tasknum == 1:
            self.chop()
        if self.tasknum == 2:
            self.mine()
    def chop(self):
        wood = random.randint(1, 10)
        print('chopping wood')
        time.sleep(1.5)
        print('you got', wood)
        Stockpile(wood, 0)
        time.sleep(0.75)
    def mine(self):
        stone = random.randint(1, 10)
        print('mining for stone')
        time.sleep(1.5)
        print('you got', stone)
        Stockpile(0, stone)
        time.sleep(0.75)

class Stockpile:
    def __init__(self, wood, stone):
        self.wood = wood
        self.stone = stone
        self.SP_wood = 0
        self.SP_stone = 0
        self.Addwood(self.wood)
        self.Addstone(self.stone)
        self.check_wood()
        self.check_stone()
    def Addwood(self, addwood):
        self.addwood = addwood
        self.SP_wood += self.addwood
    def Addstone(self, addstone):
        self.addstone = addstone
        self.SP_stone += self.addstone
    def check_wood(self):
        print(self.SP_wood)
    def check_stone(self):
        print(self.SP_stone)


while idle:
    taskchance = random.randint(0, 100)
    if taskchance < 50:
        tasknum = random.randint(0, 2)
        job = Taskassigner(tasknum)
        job.choosejob()

    else:
        print('idle')
        time.sleep(0.5)

最佳答案

我会改变一些东西,主要是因为你让这比它需要的更难。它看起来不像你的 TaskAssigner 真的需要保持状态,所以让我们稍微重构一下它,使其基本上成为一个函数工厂。然后让我们更改这些函数,以便它们可以在我们新重构的 Stockpile 类上工作。不过封装做得很好!!!

def taskassigner(tasknum):
    """Function factory for tasks

    taskassigner(1) --> mine
    taskassigner(2) --> chop
    """

    def mine(stockpile):
        stockpile.stone += random.randint(1,10)
    def chop(stockpile):
        stockpile.wood += random.randint(1,10)

    tasks = [None, chop, mine]
    return tasks[tasknum]

class Stockpile(object):
    def __init__(self, wood, stone):
        self.wood = wood
        self.stone = stone
        # it's not really clear what SP_wood and SP_stone do
        # so I'm not sure if you actually need them!

因为 taskassigner 现在是一个函数工厂而不是一个类,我们的调用签名看起来也有点不同。

STARTING_WOOD, STARTING_STONE = 10, 10  # or whatever...
idle = True

player_stockpile = Stockpile(STARTING_WOOD, STARTING_STONE)
while idle:
    if random.randint(0, 1):  # since it's a coinflip, this is easier
        task_num = random.randint(1, 2)
        task = taskassigner(task_num)  # task is now one of your functions
        task(player_stockpile)
    else:
        time.sleep(0.5)

综上所述,您似乎应该为此使用单独的模块!

# /main.py
import tasks
import time

STARTING_STONE, STARTING_WOOD = 10, 10
idle = True


class Stockpile(object):
    # defined as above


player_stockpile = Stockpile(STARTING_WOOD, STARTING_STONE)
while idle:
    if random.choice((True, False)):
        task = random.choice(tasks.tasklist)
        task(player_stockpile)
    else:
        time.sleep(0.5)

# /tasks.py
def mine(stockpile):
    stockpile.stone += random.randint(1,10)

def chop(stockpile):
    stockpile.wood += random.randint(1,10)

tasklist = [mine, chop]

关于python - 我如何更改类内的变量?在 python ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30245153/

相关文章:

python - 转换 pandas 数据框时出现问题

python - MacOS 上的 Spyder。打字很卡

python - Raspberry Pi - 如果发生错误并中断正在运行的代码,是否可以重新启动 python 程序?

python - 使用 txt 文件中的多个根元素创建 xml

python - 当我按下 "Enter"按钮时,为什么我没有获得 self.entry 的值?

python - matplotlib中imshow()的 'turn off'模糊效果如何处理?

python - 使用 python 将 csv 转换为 xls

python - 是否可以使用单个记录器编写许多不同的日志文件?

Python Pandas GroupBy 直到值发生变化

python - 如何在 Python 中对稀疏矩阵中的整列进行加法运算