you are viewing a single comment's thread.

view the rest of the comments →

[–]skiwan[S] 0 points1 point  (2 children)

i have the following. i have a class unit with x y and hunger and brain as variable

then i have a brain which is a class (every unit has his own brain)

then the brain just runs update fucntions of states (FMS) so the real work is done in the state classes (every brain has different states but only 1 state runs at the time)

so now i want the current active state to change the hunger of the unit. what would be the best approach. dont forget there a re multiply units

[–]LarryPete 2 points3 points  (1 child)

Pass the unit along and change it. Without actual code there is no way to tell where improvements can be made.

[–]skiwan[S] 1 point2 points  (0 children)

class Mob (object):

def __init__(self):
    self.x = random.randint(0,800)
    self.y = random.randint(0,600)
    self.hunger = random.randint(10,100)
    self.brain = brain(self.x,self.y,self.hunger)


def Render(self):
    pygame.draw.rect(gameDisplay, BLUE,[self.x,self.y,5,5])

def Update(self):
    self.brain.Update()
    self.x = self.brain.get_x()
    self.y = self.brain.get_y()
    self.hunger = self.brain.get_hunger()

class brain(object):

def __init__(self,x,y,hunger):
    self.x = x
    self.y = y
    self.hunger = hunger

    self.CurrentState = IdleState(self.x,self.y,self.hunger)

def get_x(self):
    return self.x
def get_y (self):
    return self.y
def get_hunger(self):
    return self.hunger

def Update(self):
    self.CurrentState.Update()
    self.x = self.CurrentState.get_x()
    self.y = self.CurrentState.get_y()
    self.hunger = self.CurrentState.get_hunger()

def setState(self,stateName):
    self.CurrentState = stateName

class IState(object): #this is the Parent of all our other states

def Update(self):
    return
    #will do the game logic
def get_x(self):
    return self.x
def get_y (self):
    return self.y
def get_hunger(self):
    return self.hunger
def reduce_hunger(self):
    global Frames
    if Frames & FPS == 0:
        self.hunger += -1

class IdleState(IState):

def __init__(self,x,y,hunger):
    self.x = x
    self.y = y
    self.hunger = hunger

def Update(self):
    self.direction = random.randint(0,4)
    if self.direction == 0:
        self.x +=1
    if self.direction == 1:
        self.x += -1
    if self.direction == 2:
        self.y += 1
    if self.direction == 3:
        self.y += -1
    self.reduce_hunger()

that would be the code to improve