Pixel Forge
Make a sprite, undo mistakes and compress its pixels
A planned Python mini-project for two 55-minute lessons. Build an original 8 × 8 image and four small functions. This project does not mark syllabus topics complete or replace outstanding homework. No new deadline is assigned.
Save your current code first
No imports, installation, graphics library or live input are needed. The supplied helpers display the image, copy its rows and flatten its pixels. The four TODO functions are deliberately unfinished.
Download your existing playground code before loading the starter. Saved browser code takes priority: select Pixel Forge and use Load example, confirming replacement only after saving. The initial Run shows the sample alien. An enabled unfinished checkpoint raises NotImplementedError; that is an expected TODO, not a broken playground.
Each Run starts again from START_ROWS. Preserve your original design in those eight strings, each containing exactly eight 0s or 1s. Download your .py file at each checkpoint; browser storage alone is not your backup.
Words and representation
- Bitmap
- An image represented by a grid of pixel values. Here 0 is blank and 1 is filled. Rows and columns are numbered 0 to 7; row comes first.
- Snapshot and LIFO
- A snapshot is an independent copy of an earlier image. History is a stack: LIFO means last in, first out. Undo restores the most recently saved image.
- Run-length encoding and lossless
- Run-length encoding (RLE) stores each consecutive group of equal bits as a bit and a count. Lossless means decoding recovers every original bit in its original order.
Lesson 1 · Pixels, snapshots and undo
Make an original sprite and implement toggle_pixel and undo. Keep render, copy_pixels and flatten as supplied helpers.
Read the grid and predict
Run the unchanged starter. Locate (0, 0), (0, 7), (7, 0) and (7, 7). Predict which printed spaces change if you toggle each corner. A toggle switches 0 to 1 or 1 to 0.
Draw your original sprite
Edit START_ROWS into an original creature, badge or tiny landscape. Keep eight rows of eight binary digits. Show a partner the design and explain your row/column convention.
Implement a safe pixel edit
Implement only TODO 1. For valid integer coordinates, save the image before changing one pixel and return True. For negative or out-of-range coordinates, return False without changing either the image or history. Core tests use integer coordinates.
Open a thinking hint
Python accepts negative list indices. Which lower and upper bounds must both be checked? Why must the snapshot have independent rows? Use the supplied copy_pixels helper; decide when it belongs in the edit.
Implement undo and partner-test
Implement TODO 2. Return the most recent saved image and remove that history entry. Empty history returns the current image unchanged. The caller must keep the returned image with pixels = undo(pixels, history). Do not add a snapshot during undo.
Partner test: all four corners; row and column −1 and 8; two distinct edits followed by two undos; then an empty-history undo. Predict the image and history length before each call. Check that invalid edits leave both unchanged and that a later edit cannot alter an older snapshot.
Open a thinking hint
Which saved image should come back first after two edits? Trace three named pictures on paper before changing the code.
Debug and save evidence
Fix one mismatch, or explain why a chosen edge case passes. Save your .py file and a short record: original sprite, two predicted/actual tests, one history-length trace and one next step. Mark unrun tests NOT TESTED. This is a class checkpoint, not an extra worksheet.
Lesson 2 · Encode, exchange and compare
Implement encode_rle and decode_rle, then test correctness and compare an imagined packed payload.
Predict runs and expected outputs
Read pixels left to right, top to bottom; a run can cross a row boundary. Predict the encoding of 00011, 101 and the empty string. Compare with the test vectors below before implementing anything.
Implement the encoder and decoder
Implement TODO 3 and TODO 4. Inputs for the core task are valid binary strings of at most 64 bits and ordered (bit, count) pairs with counts 1..64 and total length at most 64. Empty inputs are valid. You do not need a parser for text input.
Open a thinking hint
Encoder: what information describes the run currently growing, and how will you account for the final run? Decoder: how does a count determine the amount of output? Think through an empty input before your loop.
Partner tests and data exchange
First test known expected encodings and independent decoding examples, then round trips. Two incorrect functions can agree with each other. Test empty input, one bit, all zeros, alternating bits and a final run of a different length.
Exchange a valid original 64-pixel design or its run pairs with a partner. Check eight rows of eight bits, or binary run values with positive counts totaling 64. Compare the decoded string against the sender’s original flatten output, not just your own round trip. Keep invalid exchanges as a discussion, not fabricated success.
Compare payloads and debug
Use our imagined packed format: each run takes 1 pixel bit plus 8 count bits, so RLE payload = 9 × number of runs. Counts are 1..64 here. The raw 8 × 8 one-bit bitmap payload is 64 bits. Exclude headers and dimensions from both.
Print the raw and RLE payload totals for your design, 64 zeros and 01 repeated 32 times. Find one design that shrinks and one that expands. These are payload bit counts, not Python memory use or bytes in a text file of digits. A lossless method can expand data.
Save and explain the result
Save your .py file, one known-vector test, a partner exchange comparison and a three-row payload table. Explain why undo needs independent snapshots and why lossless does not mean smaller. Note any unfinished function or test honestly. No new deadline or extra homework is assigned here; keep existing homework instructions.
Known test vectors · expected results, not algorithms
| Input bits | Expected runs | Expected decoded bits |
|---|---|---|
"" | [] | "" |
"00011" | [("0", 3), ("1", 2)] | "00011" |
"101" | [("1", 1), ("0", 1), ("1", 1)] | "101" |
"0111" | [("0", 1), ("1", 3)] | "0111" |
Check the extreme payload cases
64 zeros form one run: 9 bits. Alternating 01 repeated 32 times forms 64 runs: 576 bits. Both decode without losing pixels; only the first is smaller than the raw 64-bit payload.
If you finish early
Optional extensions after core tests: redo, a colour palette with a revised bit budget, or validation of malformed exchange data. Plan the behavior first. These are not required for the two-lesson project.