class TestRepeatedTimer(TestCase): def setUp(self): self.timer = RepeatedTimer() self.time_fun = lambda x:hello(x) def test_start_input_args(self): time_fun = lambda x:print(x) self.timer = RepeatedTimer(1,time_fun,5,6) constructor_intearface = self.timer.start_input_args self.timer.start_input_args = 5,6 seting_input = self.timer.start_input_args self.assertEqual(constructor_intearface,seting_input) def test_set_attr(self): self.timer.set_attr(timer_function=self.time_fun, interval= 1, start_input_args = ['test_set_attr'],limit=10) self.timer.start() def test_start(self): self.timer.set_attr( timer_function = self.time_fun, interval=1 ) self.timer.start_input_args = ['test_start'] self.timer.start() def test_stop(self): self.timer.set_attr(timer_function=self.time_fun, stop_function=self.time_fun, interval= 0.01, limit=0.05) self.timer.start_input_args = ['test_stop'] self.timer.stop_input_args = ['stop'] self.timer.start()
class ObjectProp(object): def __init__(self, **attr): self.x = 0 self.y = 0 self.z = 0 self.t = 0 self.angle = 0 self.speed = 0.1 self.velocity = 0 self.mass = 1 self.time_stamp = 0.01 self._stable = True self.bottom = 0 self.gravity = 9.8 self.surface_x = 0 self.surface_y = 0 self.surface_z = 0 self.rt = RepeatedTimer(self.time_stamp, self._movement) # protected self.__command = [] self.__command_stop =[] self.set_setup(**attr) pass @property def command_update(self): return self.__command @command_update.setter def command_update(self, command): # check if the command is lambda expression if callable(command): self.__command = command @property def command_stop(self): return self.__command_stop @command_stop.setter def command_stop(self, command): # check if the command is lambda expression if callable(command): self.__command_stop = command def set_setup(self, **prop): default = ['x', 'y', 'z', 'angle', 'v', 't', 'mass', 'bottom', 'gravity', 'surface_x', 'surface_y', 'surface_z', 'time_stamp', 'command_update', 'command_stop'] # check if filed value is inside defulte filed fileds = list(prop.keys()) (state, diff) = is_member(fileds, default) assert state, 'their is no such member' + str(diff) # setup = default.copy() # setup.update(prop) for name in prop.keys(): self.__setattr__(name, prop[name]) def throw(self,vx: float = 0, vy: float = 0): self.rt.start_input_args = vx, vy self.rt.stop_function = self._stop_movment try: self.rt.start() # your long-running job goes here... finally: pass def _movement(self, vx: float = 0, vy: float = 0,vz: float = 0): self._stable = self.rt.is_running self.t += self.time_stamp + self.speed t = self.t # self.z = self.surface_z + vz * t self.x = self.surface_x + vx * t self.y = self.surface_y + vy * t - 0.5 * self.gravity * math.pow(t, 2) if self.bottom >= self.y: self.rt.stop() self._stable = True self.t = 0 self.update() def _stop_movment(self): if callable(self.command_stop): self.command_stop(self) def update(self): if callable(self.command_update): self.command_update(self)
class Goblin(Human): def __init__(self, environment, **attr): super().__init__(environment, **attr) self.images_path = full_file(['Resources', 'images', 'Enemy']) # create goblin self.power = 3 self.move_direction = 'center' self.walk_direction = 'left' self.high = 180 self.sound_hooch = full_file(['Resources', 'sound', 'wound.mp3']) self.sound_dead = full_file(['Resources', 'sound', 'died.mp3']) self.__attack = False self.timer = RepeatedTimer() def create(self): self.walkRight = [ self.load_image('R1E.png'), self.load_image('R2E.png'), self.load_image('R3E.png'), self.load_image('R4E.png'), self.load_image('R5E.png'), self.load_image('R6E.png'), self.load_image('R7E.png'), self.load_image('R8E.png'), self.load_image('R5E.png') ] self.walkLeft = [ self.load_image('L1E.png'), self.load_image('L2E.png'), self.load_image('L3E.png'), self.load_image('L4E.png'), self.load_image('L5E.png'), self.load_image('L6E.png'), self.load_image('L7E.png'), self.load_image('L8E.png'), self.load_image('L5E.png') ] self.standing = [self.load_image('L1E.png')] self.attack_right = [ self.load_image('R9E.png'), self.load_image('R10E.png'), self.load_image('R11E.png') ] self.attack_left = [ self.load_image('L9E.png'), self.load_image('L10E.png'), self.load_image('L11E.png') ] def draw(self): if self.__attack == False: super().draw() elif self.__attack == True: self._frame_count += 1 if self.move_direction == 'left': self._environment.win.blit( self.attack_left[self._frame_count % 3], (self.position_x, self.position_on_canvas_y())) elif self.move_direction == 'right': self._environment.win.blit( self.attack_right[self._frame_count % 3], (self.position_x, self.position_on_canvas_y())) def attack(self): self.timer.set_attr(timer_function=self.box_attack, start_input_args=[True], stop_function=self.box_attack, stop_input_args=[False], interval=0.1, limit=0.5) self.timer.start() def box_attack(self, state: bool = True): self.__attack = state