I've started coding an idle game but I will put some type of input in later.
My problem is that my class stockpiles doesn't add int's from def chop/mine to my variable "SP_wood/SP_stone", it's just replacing the variable with the number it got from def chop/mine. I've tried to give the int straight to def addwood/addstone but that didn't work for me. The += should work and add the two together. Should i make the variable outside the class and make it 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)
random.randintincludes both endpoints, so what you presumably think is a coinflip is actually weighted towards "No".len(range(0, 50)) < len(range(50, 101))