"""Draw a 32 × 32 pixel archery bow using only Python's standard library.""" from pathlib import Path import struct import zlib SIZE = 32 pixels = [[(0, 0, 0, 0) for _ in range(SIZE)] for _ in range(SIZE)] PALETTE = { 'outline': (53, 31, 29, 255), 'wood': (157, 83, 39, 255), 'light': (231, 167, 79, 255), 'grip': (79, 47, 42, 255), 'wrap': (191, 123, 72, 255), 'string': (224, 215, 181, 255), 'steel': (190, 218, 222, 255), 'shine': (239, 251, 246, 255), 'feather': (71, 156, 138, 255), } def dot(x, y, color): if 0 <= x < SIZE and 0 <= y < SIZE: pixels[y][x] = PALETTE[color] def line(x0, y0, x1, y1, color): dx, dy = abs(x1 - x0), -abs(y1 - y0) sx, sy = (1 if x0 < x1 else -1), (1 if y0 < y1 else -1) error = dx + dy while True: dot(x0, y0, color) if (x0, y0) == (x1, y1): break twice = 2 * error if twice >= dy: error += dy x0 += sx if twice <= dx: error += dx y0 += sy # The taut string sits behind the carved wooden limbs. line(20, 3, 20, 28, 'outline') line(19, 3, 19, 28, 'string') # Hand-placed rows preserve crisp, deliberately stepped pixel edges. limb = [(19, 2), (18, 3), (16, 4), (14, 5), (13, 6), (12, 7), (11, 8), (10, 9), (10, 10), (9, 11), (9, 12), (8, 13), (8, 14), (8, 15), (8, 16), (8, 17), (8, 18), (9, 19), (9, 20), (10, 21), (10, 22), (11, 23), (12, 24), (13, 25), (14, 26), (16, 27), (18, 28), (19, 29)] for x, y in limb: for offset in range(-1, 3): dot(x + offset, y, 'outline') for x, y in limb: dot(x, y, 'light') dot(x + 1, y, 'wood') for y in range(13, 19): for x in range(7, 11): dot(x, y, 'grip') for y in (13, 15, 17): line(8, y, 10, y + 1, 'wrap') # A nocked arrow, with teal fletching and a silver broadhead. line(4, 15, 26, 15, 'outline') line(4, 16, 26, 16, 'outline') line(5, 15, 25, 15, 'light') for x, y in [(3, 12), (4, 13), (5, 14), (3, 18), (4, 17)]: dot(x, y, 'outline') dot(x + 1, y, 'feather') for x, y in [(25, 12), (26, 13), (27, 14), (28, 15), (27, 16), (26, 17), (25, 18)]: dot(x, y, 'outline') for y, end in [(13, 25), (14, 26), (15, 27), (16, 26), (17, 25)]: line(25, y, end, y, 'shine' if y <= 15 else 'steel') def chunk(kind, data): return struct.pack('>I', len(data)) + kind + data + struct.pack('>I', zlib.crc32(kind + data) & 0xffffffff) raw = b''.join(b'\x00' + bytes(channel for pixel in row for channel in pixel) for row in pixels) png = b'\x89PNG\r\n\x1a\n' png += chunk(b'IHDR', struct.pack('>IIBBBBB', SIZE, SIZE, 8, 6, 0, 0, 0)) png += chunk(b'IDAT', zlib.compress(raw, 9)) png += chunk(b'IEND', b'') output = Path(__file__).with_name('archery-bow.png') output.write_bytes(png) print(f'Created {output}: {SIZE}x{SIZE} RGBA PNG ({len(png)} bytes)')