如何在类中更改变量?在python中

时间:2015-05-14 18:55:57

标签: python

我已经开始编写空闲游戏的编码,但我稍后会输入某种类型的输入。

我的问题是,我的class库存不会将int剁/我的def添加到我的variable“SP_wood / SP_stone”,它只是取代variable来自def chop / mine的数字def。我试图直接给+= addwood / addstone,但这对我不起作用。 variable应该可以工作并将两者加在一起。我应该在class之外制作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) 吗?

{{1}}

2 个答案:

答案 0 :(得分:3)

我会改变一些事情,主要是因为你做得比实际需要的更难。它看起来不像你的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]

答案 1 :(得分:0)

你需要将你的库存分开(但你应该传递它而不是让它全局化)

import random
import time
idle = True


class Taskassigner:
    def __init__(self, tasknum,stockpile):
        self.tasknum = tasknum
        self.stockpile = stockpile
    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)
        self.stockpile.AddWood(wood)
        time.sleep(0.75)
    def mine(self):
        stone = random.randint(1, 10)
        print('mining for stone')
        time.sleep(1.5)
        print('you got', stone)
        self.stockpile.AddStone(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)

player_stockpile = Stockpile(0,0)
while idle:
    taskchance = random.randint(0, 100)
    if taskchance < 50:
        tasknum = random.randint(0, 2)
        #be sure to pass in the player stockpile
        job = Taskassigner(tasknum,player_stockpile)
        job.choosejob()

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