def test_step_before_any_done(self): global rec, next_done next_done = 0 node = CocosNode() name1 = '1' name2 = '2' a1 = UAction(name1) a2 = UAction(name2) composite = ac.spawn(a1, a2) a_copy = node.do(composite) dt = 0.1 rec = [] node._step(dt) assert rec[0] == (name1, 'step', dt) assert rec[0] == (name1, 'step', dt) assert not a_copy.done() assert len(rec) == 2
def test_receiving_stop_async_after_done1_before_done2(self): global rec, next_done next_done = 0 node = CocosNode() name1 = '1' name2 = '2' a1 = UAction(name1) a2 = UAction(name2) composite = ac.sequence(a1, a2) a_copy = node.do(composite) next_done = 1 dt = 0.1 node._step(dt) rec = [] a_copy.stop() assert rec[0] == (name2, 'stop') len(rec) == 0
def test_updating_above_min_below_max(self, duration1, duration2): # min < elapsed < next_elapsed < max global rec need_test = (duration1 != duration2) if not need_test: return node = CocosNode() name1 = '1' name2 = '2' a1 = UIntervalAction(name1, duration1) a2 = UIntervalAction(name2, duration2) composite = ac.spawn(a1, a2) node.do(composite) elapsed = 0.0 next_elapsed = (duration1 + duration2) / 2.0 # above min, below max dt = next_elapsed - elapsed node._step(dt) elapsed = next_elapsed next_elapsed = (elapsed + max(duration1, duration2)) / 2.0 dt = next_elapsed - elapsed rec = [] node._step(dt) # expected: # no call from the action with min duration # only one call from the action with max duration and will be .update if duration1 < duration2: oname1 = name1 oname2 = name2 else: oname1 = name2 oname2 = name1 rec_lo = [e for e in rec if e[0] == oname1] print('rec_lo:', rec_lo) assert len(rec_lo) == 0 rec = [e for e in rec if e[0] == oname2] rec = [e for e in rec if e[1] != 'step'] print('rec hi:', rec) assert len(rec) == 1 assert (rec[0][1] == 'update' and abs(rec[0][2] - next_elapsed / max(duration1, duration2)) < fe)
def test_receiving_stop_async_after_done2_before_done1(self): global rec, next_done next_done = 0 node = CocosNode() name1 = '1' name2 = '2' a1 = UAction(name1) a2 = UAction(name2) composite = ac.spawn(a1, a2) a_copy = node.do(composite) dt = 0.1 next_done = 2 node._step(dt) rec = [] print(rec) a_copy.stop() assert rec[0] == (name1, 'stop') assert len(rec) == 1
def test_step_setting_done1(self): global rec, next_done next_done = 0 node = CocosNode() name1 = '1' name2 = '2' a1 = UAction(name1) a2 = UAction(name2) composite = ac.sequence(a1, a2) a_copy = node.do(composite) dt = 0.1 rec = [] next_done = 1 node._step(dt) print rec assert rec[0][0] == name1 and rec[0][1] == 'step' assert rec[1][0] == name1 and rec[1][1] == 'stop' assert rec[2][0] == name2 and rec[2][1] == 'start' assert not a_copy.done()
def update_crossing_max_from_below_min(self, duration1, duration2): # elapsed < min <= max < next_elapsed global rec need_test = True try: a = 1.0 / duration1 b = 1.0 / duration2 except ZeroDivisionError: need_test = False if not need_test: # the test dont make sense for the parameters received, skip return node = CocosNode() name1 = '1' name2 = '2' a1 = UIntervalAction(name1, duration1) a2 = UIntervalAction(name2, duration2) composite = ac.spawn(a1, a2) node.do(composite) elapsed = 0.0 next_elapsed = min(duration1, duration2) / 2.0 # above 0, below min dt = next_elapsed - elapsed node._step(dt) next_elapsed = max(duration1, duration2) * 1.1 # above max dt = next_elapsed - elapsed rec = [] node._step(dt) # expected: for each action .update(1.0) and stop() called, in that order # nothing more # ? maybe step recx = [e for e in rec if e[1] != 'step'] rec = [e for e in recx if e[0] == name1] assert len(rec) == 2 assert rec[0][1] == 'update' and rec[0][2] == 1.0 assert rec[1][1] == 'stop' rec = [e for e in recx if e[0] == name2] assert len(rec) == 2 assert rec[0][1] == 'update' and rec[0][2] == 1.0 assert rec[1][1] == 'stop'
def test_update_crossing_max_from_above_min(self, duration1, duration2): # min < elapsed < max < next_elapsed global rec need_test = (duration1 != duration2) if not need_test: return node = CocosNode() name1 = '1' name2 = '2' a1 = UIntervalAction(name1, duration1) a2 = UIntervalAction(name2, duration2) composite = ac.spawn(a1, a2) node.do(composite) elapsed = 0.0 next_elapsed = (duration1 + duration2) / 2.0 # above min, below max dt = next_elapsed - elapsed node._step(dt) elapsed = next_elapsed next_elapsed = max(duration1, duration2) * 1.1 # above max dt = next_elapsed - elapsed rec = [] node._step(dt) # expected: no call from action with min duration, two calls from the # action with max duration: .update(1) and .stop() in that order if duration1 < duration2: oname1 = name1 oname2 = name2 else: oname1 = name2 oname2 = name1 rec_lo = [e for e in rec if e[0] == oname1] print('rec_lo:', rec_lo) assert len(rec_lo) == 0 rec = [e for e in rec if e[0] == oname2] rec = [e for e in rec if e[1] != 'step'] print('rec hi:', rec) assert rec[0][1] == 'update' and rec[0][2] == 1.0 assert rec[1][1] == 'stop' assert len(rec) == 2
def test_start(self, duration1, duration2): global rec node = CocosNode() name1 = '1' name2 = '2' a1 = UIntervalAction(name1, duration1) a2 = UIntervalAction(name2, duration2) composite = ac.sequence(a1, a2) rec = [] node.do(composite) print('start rec:', rec) num = 0 assert rec[num]==(name1, 'start') if duration1==0.0: assert rec[num + 1]==(name1, 'update', 1.0) assert rec[num + 2]==(name1, 'stop') assert rec[num + 3]==(name2, 'start') num = num + 3 assert len(rec)==num+1
def test_updating_below_min(self, duration1, duration2): # next_elapsed < min(duration1, duration2) global rec # only applies if min(duration1, duration2)>=epsilon >0.0 , 1.0/epsilon # well defined need_test = True try: a = 1.0 / duration1 b = 1.0 / duration2 except ZeroDivisionError: need_test = False if not need_test: # the test dont make sense for the parameters received, skip return node = CocosNode() name1 = '1' name2 = '2' a1 = UIntervalAction(name1, duration1) a2 = UIntervalAction(name2, duration2) composite = ac.spawn(a1, a2) node.do(composite) elapsed = 0.0 min_duration = min(duration1, duration2) for next_elapsed in [min_duration / 3.0, min_duration / 2.0]: dt = next_elapsed - elapsed rec = [] node._step(dt) rec = [e for e in rec if e[1] != 'step'] rec1 = [e for e in rec if e[0] == name1] assert len(rec1) == 1 assert (rec1[0][1] == 'update' and abs(rec1[0][2] - next_elapsed / duration1) < fe) rec2 = [e for e in rec if e[0] == name2] assert len(rec2) == 1 assert (rec2[0][1] == 'update' and abs(rec2[0][2] - next_elapsed / duration2) < fe) elapsed = next_elapsed
def test_life_cycle(self): global rec, next_done next_done = 0 name1 = '1' a1 = UAction(name1) node = CocosNode() composite = ac.Repeat(a1) #1st start rec = [] a_copy = node.do(composite) assert rec[0] == (name1, 'start') assert len(rec) == 1 assert not a_copy.done() #step in first repetition dt = 0.1 next_done = 0 rec = [] node._step(dt) assert rec[0] == (name1, 'step', dt) assert len(rec) == 1 assert not a_copy.done() #termination first repetion, start second repetition next_done = 1 rec = [] node._step(dt) assert rec[0] == (name1, 'step', dt) assert rec[1] == (name1, 'stop') assert rec[2] == (name1, 'start') assert len(rec) == 3 assert not a_copy.done() #step in second repetition next_done = 0 rec = [] node._step(dt) assert rec[0] == (name1, 'step', dt) assert len(rec) == 1 assert not a_copy.done()
def test_test_receiving_stop_async_before_duration1(self, duration1, duration2): #0<duration1 global rec if duration1==0.0: return node = CocosNode() name1 = '1' name2 = '2' a1 = UIntervalAction(name1, duration1) a2 = UIntervalAction(name2, duration2) elapsed = 0.0 rec = [] composite = ac.sequence(a1, a2) a_copy = node.do(composite) rec = [] a_copy.stop() assert rec[0]==(name1, 'stop') assert len(rec)==1
def test_actions_stepping_without_completion(self): global rec, next_done next_done = 0 node = CocosNode() name1 = '1' action1 = UAction(name1) name2 = '2' action2 = UAction(name2) a1_copy = node.do(action1) a2_copy = node.do(action2) rec = [] dt = 0.1 node._step(dt) recx = [e for e in rec if e[0] == name1] assert recx[0] == (name1, 'step', dt) assert len(recx) == 1 recx = [e for e in rec if e[0] == name2] assert recx[0] == (name2, 'step', dt) assert len(recx) == 1
def test_update_crossing_duration1_not_duration_not_at_1st_step( self, duration1, duration2): global rec if duration1 == 0.0 or duration2 == 0.0: return # todo node = CocosNode() name1 = '1' name2 = '2' a1 = UIntervalAction(name1, duration1) a2 = UIntervalAction(name2, duration2) composite = ac.sequence(a1, a2) rec = [] node.do(composite) elapsed = 0.0 next_elapsed = duration1 / 2.0 dt = next_elapsed - elapsed node._step(dt) elapsed = next_elapsed next_elapsed = (duration1 + duration2) / 2.0 dt = next_elapsed - elapsed rec = [] node._step(dt) #expect update(1), stop for action 1, start + update(x) for action2 recx = [e for e in rec if e[1] != 'step'] rec = [e for e in recx if e[0] == name1] print('rec', rec) assert rec[0][1] == 'update' and rec[0][2] == 1.0 assert rec[1][1] == 'stop' assert len(rec) == 2 rec = [e for e in recx if e[0] == name2] print('rec', rec) assert rec[0][1] == 'start' assert rec[1][1] == 'update' assert abs(rec[1][2] - (next_elapsed - duration1) / duration2) < fe assert len(rec) == 2
def test_step_after_done1_before_done2(self): global rec, next_done next_done = 0 node = CocosNode() name1 = '1' name2 = '2' a1 = UAction(name1) a2 = UAction(name2) composite = ac.sequence(a1, a2) a_copy = node.do(composite) dt = 0.1 next_done = 1 node._step(dt) next_done = 0 rec = [] dt = 0.2 node._step(dt) print rec assert 0 == len([e for e in rec if e[0] == name1]) assert rec[0] == (name2, 'step', dt) assert not a_copy.done()
def test_InstantAction_duration0_action(self): global rec node = CocosNode() name1 = '1' name2 = '2' duration1 = 0.0 a = UIntervalAction(name1, duration1) b = UAction(name2) composite = a + b rec = [] worker = node.do(composite) dt = 0.1 node._step(dt) print(rec) assert rec[0] == (name1, 'start') assert rec[1] == (name1, 'step', dt) assert rec[2] == (name1, 'update', 1.0) assert rec[3] == (name1, 'stop') assert rec[4] == (name2, 'start') rec = [] node._step(dt) for e in rec: assert e[0] == name2
def test_test_receiving_stop_async_after_duration1_before_duration2(self, duration1, duration2): #0<duration2 global rec if duration2==0.0: return node = CocosNode() name1 = '1' name2 = '2' a1 = UIntervalAction(name1, duration1) a2 = UIntervalAction(name2, duration2) elapsed = 0.0 rec = [] composite = ac.sequence(a1, a2) a_copy = node.do(composite) next_elapsed = (duration1+duration2)/2.0 dt = next_elapsed - elapsed node._step(dt) rec = [] a_copy.stop() assert rec[0]==(name2, 'stop') assert len(rec)==1
def test_update_below_duration1(self, duration1, duration2): global rec if duration1==0.0: return node = CocosNode() name1 = '1' name2 = '2' a1 = UIntervalAction(name1, duration1) a2 = UIntervalAction(name2, duration2) composite = ac.sequence(a1, a2) node.do(composite) elapsed = 0.0 # check cases 1st step and not 1st step for next_elapsed in [ duration1*0.5, duration1*0.75]: dt = next_elapsed - elapsed rec = [] node._step(dt) rec = [ e for e in rec if e[1]!='step' ] print(rec) assert rec[0][1]=='update' and abs(rec[0][2]-next_elapsed/duration1)<fe assert len(rec)==1 elapsed = next_elapsed
def test_reaching_done2(self): global rec, next_done next_done = 0 node = CocosNode() name1 = '1' name2 = '2' a1 = UAction(name1) a2 = UAction(name2) composite = ac.sequence(a1, a2) a_copy = node.do(composite) dt = 0.1 next_done = 1 node._step(dt) next_done = 2 rec = [] dt = 0.2 node._step(dt) print(rec) assert 0 == len([e for e in rec if e[0] == name1]) assert rec[0] == (name2, 'step', dt) assert rec[1] == (name2, 'stop') assert a_copy.done() assert len(rec) == 2
def test_life_cycle_when_duration_gt_0(self): global rec, next_done next_done = 0 name1 = '1' duration1 = 3.0 times = 2 a1 = UIntervalAction(name1, duration1) node = CocosNode() composite = ac.loop(a1, times) elapsed = 0.0 #1st start rec = [] a_copy = node.do(composite) assert rec[0] == (name1, 'start') assert len(rec) == 1 assert not a_copy.done() #step in first repetition dt = 0.1 next_done = 0 rec = [] node._step(dt) print('rec 1st repetition:', rec) elapsed += dt rec = [e for e in rec if e[1] != 'step'] assert rec[0] == (name1, 'update', elapsed / duration1) assert len(rec) == 1 assert not a_copy.done() #termination first repetion, start second repetition next_elapsed = duration1 * 1.5 dt = next_elapsed - elapsed rec = [] node._step(dt) elapsed = next_elapsed rec = [e for e in rec if e[1] != 'step'] print('rec end 1st reptition:', rec) assert rec[0] == (name1, 'update', 1.0) assert rec[1] == (name1, 'stop') assert rec[2] == (name1, 'start') assert rec[3] == (name1, 'update', (elapsed - duration1) / duration1) assert len(rec) == 4 assert not a_copy.done() #step in second repetition dt = 0.1 rec = [] node._step(dt) print('rec 1st repetition:', rec) elapsed += dt rec = [e for e in rec if e[1] != 'step'] assert rec[0] == (name1, 'update', (elapsed - duration1) / duration1) assert len(rec) == 1 assert not a_copy.done() #terminatation last repetition next_elapsed = times * duration1 * 1.1 dt = next_elapsed - elapsed rec = [] node._step(dt) rec = [e for e in rec if e[1] != 'step'] print('rec end 1st reptition:', rec) assert rec[0] == (name1, 'update', 1.0) assert rec[1] == (name1, 'stop') assert len(rec) == 2 assert a_copy.done()
def test_initial_actions_container_empty(self): node = CocosNode() assert len(node.actions) == 0
def __init__(self, position=(2, 1), level=None): super(Wobble, self).__init__() self.level = level self.move_speed = 200 self.status = "still" self.facing = "up" self.can_jump = True self.keyboard = { 'LEFT': False, 'RIGHT': False, 'SPACE': False, 'UP': False, 'DOWN': False, } self.sequences = { 'still': [ 'blob_1.png', ], 'walk': [ 'blob_1.png', 'blob_2.png', 'blob_3.png', 'blob_4.png', 'blob_5.png', 'blob_6.png', 'blob_7.png', 'blob_6.png', 'blob_5.png', 'blob_4.png', 'blob_3.png', 'blob_2.png', 'blob_1.png', ], 'jump': [ 'blob_8.png', ], 'death': ['death_1.png', 'death_2.png', 'death_3.png', 'death_4.png'] } self.tile_types = { 'block-02': 'wall', 'block-03': 'wall', 'block-04': 'wall', 'block-05': 'wall', 'block-06': 'wall', 'block-07': 'air', 'block-08': 'air', 'block-09': 'air', 'block-10': 'air', 'block-11': 'bad', 'block-12': 'bad', } self.images = {} for animation in self.sequences: self.images[animation] = ( pyglet.image.load("data/chars/wobble/" + filename) for filename in self.sequences[animation]) # self.images[animation] = (pyglet.image.load(os.path.join("chars","wobble",filename)) for filename in self.sequences[animation]) self.animations = { 'still': Sprite( pyglet.image.Animation.from_image_sequence( self.images['still'], 1, True)), 'walk': Sprite( pyglet.image.Animation.from_image_sequence( self.images['walk'], 0.05, True)), 'jump': Sprite( pyglet.image.Animation.from_image_sequence( self.images['jump'], 1, True)), # 'death': Sprite(pyglet.image.Animation.from_image_sequence(self.images['death'], 0.2, False)), } # print self.animations # self.sprite = Sprite(self.animations['still']) self.char = CocosNode() self.char.scale = 0.75 self.set_grid_position(position) self.x = 0 self.y = 0 self.sprite = None self.add(self.char) self.set_animation() # self.sprite = Sprite(self.animations['jump']) # self.do(Repeat(Liquid(waves=5,amplitude=5))) self.do(Repeat(Waves(hsin=False))) self.do(Repeat(Waves(vsin=False, amplitude=5))) self.schedule(self.step) self.grid_type(1, 1)
def __init__(self, seconds_interval: float) -> None: Notifier.__init__(self) self.timer = CocosNode() self.notify_func = lambda dt: self.notify() self.timer.schedule_interval(self.notify_func, seconds_interval) self.timer.pause_scheduler()