def test_animate_finite_seq(): sheet = spritesheet(**data) frames = list(sheet.animate("run-left")) assert len(frames) == 10 for i, frame in enumerate(frames): assert isinstance(frame, PIL.Image.Image) assert sheet[i] == frame
def test_get(): w = 64 h = 67 sheet = spritesheet(**data) frame = sheet[16] expected = sheet.image.crop((w * 6, h * 1, w * 7, h * 2)) assert frame == expected
def test_init(): sheet = spritesheet(**data) # Reframed by 2px assert sheet.width == 640 assert sheet.height == 134 assert sheet.frames.count == 20 assert sheet.frames.width == 64 assert sheet.frames.height == 67 assert sheet.frames.size == (64, 67) assert sheet.cache == {}
def test_animate_infinite_seq(): sheet = spritesheet(**data) runner = sheet.animate("run-right") frames = [] for _ in range(50): frames.append(next(runner)) expected = list(range(19, 9, -1)) * 5 for i, frame_ref in enumerate(expected): assert isinstance(frames[i], PIL.Image.Image) assert sheet[frame_ref] == frames[i]
def test_animate_subroutine(): sheet = spritesheet(**data) frames = list(sheet.animate("composite")) expected = [ 1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] assert len(frames) == 26 for i, frame_ref in enumerate(expected): assert isinstance(frames[i], PIL.Image.Image) assert sheet[frame_ref] == frames[i]
def main(num_iterations=sys.maxsize): data = { 'image': os.path.abspath( os.path.join( os.path.dirname(__file__), 'images', 'runner.png')), 'frames': { 'width': 64, 'height': 67, 'regX': 0, 'regY': 2 }, 'animations': { 'run-right': { 'frames': range(19, 9, -1), 'next': "run-right", }, 'run-left': { 'frames': range(0, 10), 'next': "run-left" } } } regulator = framerate_regulator() sheet = spritesheet(**data) runner = sheet.animate('run-right') x = -sheet.frames.width dx = 3 while num_iterations > 0: with regulator: num_iterations -= 1 background = Image.new(device.mode, device.size, "white") background.paste(next(runner), (x, 0)) device.display(background) x += dx if x >= device.width: runner = sheet.animate('run-left') dx = -dx if x <= -sheet.frames.width: runner = sheet.animate('run-right') dx = -dx
def main(num_iterations=sys.maxsize): data = { 'image': str(Path(__file__).resolve().parent.joinpath( 'images', 'jsw_{0}.gif'.format(device.mode)) ), 'frames': { 'width': 16, 'height': 16, 'regX': 0, 'regY': 0 }, 'animations': { 'willy-right': { 'frames': [8, 9, 10, 11], 'next': "willy-right" }, 'willy-left': { 'frames': [15, 14, 13, 12], 'next': "willy-left" }, 'maria': { 'frames': [4, 5, 4, 5, 4, 5, 4, 4, 4, 4, 4, 4, 6, 7, 7, 7, 6], 'next': "maria", 'speed': 0.5 }, 'saw-left': { 'frames': [191] * 128 + [56, 57, 58, 59] * 3, 'next': "saw-left", 'speed': 0.5 }, 'hare-left': { 'frames': [111, 110, 109, 108], 'next': 'hare-left', } } } sheet = spritesheet(**data) regulator = framerate_regulator(fps=10) maria = sheet.animate('maria') willy = sheet.animate('willy-right') saw = sheet.animate('saw-left') hare = sheet.animate('hare-left') wx = 24 hx = device.width clock = 0 dx = 8 while num_iterations > 0: with regulator: num_iterations -= 1 background = Image.new(device.mode, device.size) background.paste(next(maria), (0, 0)) background.paste(next(saw), (64, 0)) background.paste(next(willy), (wx, 0)) background.paste(next(hare), (hx, device.height - 16)) device.display(background) clock += 1 if clock % 4 == 0: wx += dx hx += -8 if wx >= device.width - sheet.frames.width: willy = sheet.animate('willy-left') dx = -dx wx = device.width - 24 if wx <= 16: willy = sheet.animate('willy-right') dx = -dx wx = 24 if hx + sheet.frames.width <= 0: hx = device.width
def test_animate_unknown_seq(): sheet = spritesheet(**data) with pytest.raises(AttributeError): list(sheet.animate("unknown"))
def test_get_outofrange(): sheet = spritesheet(**data) with pytest.raises(IndexError): sheet[-2] with pytest.raises(IndexError): sheet[3002]
def test_get_string(): sheet = spritesheet(**data) with pytest.raises(TypeError): sheet['banana']
def test_caching(): sheet = spritesheet(**data) assert sheet.cache == {} frame = sheet[17] assert list(sheet.cache.keys()) == [17] assert sheet.cache[17] == frame
def test_len(): sheet = spritesheet(**data) assert len(sheet) == sheet.frames.count