{"id":20846276,"url":"https://github.com/eimamei/snake-qr","last_synced_at":"2025-08-14T00:05:44.010Z","repository":{"id":189349075,"uuid":"680106125","full_name":"EimaMei/snake-qr","owner":"EimaMei","description":"A simple and pure win32 Snake game, without any compressor trickery, just under 2,953 bytes!","archived":false,"fork":false,"pushed_at":"2024-05-20T17:32:11.000Z","size":29,"stargazers_count":12,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-10T19:23:21.037Z","etag":null,"topics":["bloat-free","c-programming","lightweight","qrcode","snake","win32"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EimaMei.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-08-18T11:01:13.000Z","updated_at":"2025-02-15T02:37:17.000Z","dependencies_parsed_at":"2025-04-10T19:04:41.049Z","dependency_job_id":"54665407-e8e3-438d-a59f-fb761f60eb12","html_url":"https://github.com/EimaMei/snake-qr","commit_stats":null,"previous_names":["eimamei/snake-qr"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/EimaMei/snake-qr","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EimaMei%2Fsnake-qr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EimaMei%2Fsnake-qr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EimaMei%2Fsnake-qr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EimaMei%2Fsnake-qr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EimaMei","download_url":"https://codeload.github.com/EimaMei/snake-qr/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EimaMei%2Fsnake-qr/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266412465,"owners_count":23924523,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bloat-free","c-programming","lightweight","qrcode","snake","win32"],"created_at":"2024-11-18T02:15:37.413Z","updated_at":"2025-08-14T00:05:43.944Z","avatar_url":"https://github.com/EimaMei.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Snake QR\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"images/code.png?raw=true\" alt=\"QR code\" style=\"width:60%\"\u003e\n    \u003cbr/\u003eA simple and pure win32 Snake game, without any compressor trickery, just under 2,953 bytes!\u003cbr/\u003e\n    \u003cbr/\u003e\n\u003c/p\u003e\n\n# Context/Credit\nThe original source code was taken from MattKC's [\"Can you fit a whole game into a QR code?\" video](https://www.youtube.com/watch?v=ExwqNreocpg\u0026ab_channel=MattKC) where he attempted to port the classic game of \"Snake\" into a single QR code using the Win32 API in C. Despite the efforts he failed and decided to use a compressor to complete the challange during the final stretch, which felt very lame and lazy to me. So much so that I took a glance at the source code to see if there were any optimizations to make.\n\nMy suspicions were proven correct as I was not only able to fit the program into the QR code threshold without changing its fundamental logic, but I also implememted some fixes and improvements compared to the original.\n\n# Most notable changes\n- No crinker/compressor is used to reduce the executable size. Only Win32 C code and some MSVC compiler flag shenanigans.\n- A few visual and gameplay fixes have been added to the game to make it much closer to the [HTML version](https://www.mattkc.com/etc/snakeqr/snake-minified.html) that was shown in the video.\n- You can pause the game now by pressing the enter key instead of the 'pause' or 'P' key.\n- You can retry the game by pressing the 'R' key.\n\n# Code/smaller changes\nTo see all of the differences it's best to compare [this repository's `snake.c`](https://github.com/EimaMei/snake-qr/blob/main/snake.c) and [the original](https://www.mattkc.com/etc/snakeqr/snake.c) side by side. Here is a compiled list of the changes:\n\n## Space saving changes\n- The input system has been reworked to utilize only 2 variables instead of using an array of valid inputs. This reduces quite a considerable amount of space inside the executable, as the game now does a whole less for the same result. \n- The food starts at a constant position instead of calling the `rand` function at startup.\n- All integers are `size_t` (register-sized instructions are usually preffered in most cases).\n- A custom `rand` is implemented instead of relying on the C standard version.\n- Code to change the snake's color depending on the game state (playing/won/lost) has been reworked to be completely branchless.\n- `WM_TIMER`'s `while` loop has been converted to a single `if` statement.\n- Removed the `bool moved` variable in `WM_TIMER` and used `goto` instead.\n- Removed the `else` statement at `if (snake_len == MAX_TILE_COUNT)`.\n- Removed `GetModuleHandleA` and `AdjustWindowRect`.\n- The app now uses stack memory instead of the heap to hold `snake_pos` (this reduces the amount of OS calls needed to be called).\n- Removed `if (msg.message == WM_QUIT)` in the main loop.\n- Removed `TranslateMessage` in the main loop.\n\n## Bug fixes\n- The original `snake.c` leaked memory by continuously not calling `DeleteObject` when using `CreateSolidBrush` despite [official documentation advising to cleanup after drawing](https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createsolidbrush). The only caveat with this is that this unfortunately comes with the cost of additional binary space.\n\n## Visual changes\n- The apple is now green.\n\n## Misc changes\n- You can now retry the game at anytime by pressing 'R'.\n- The game's performance has been improved (less branching, `PosEqual` does one check instead of 2, etc).\n- The pause button has been changed from `VK_PAUSE` and `P` to `VK_ENTER`.\n- Regular `WNDCLASSA` and `CreateWindowA` are used instead of the expanded versions.\n- The game doesn't overallocate `dir_queue` like it did in the original.\n\n# Building\nNOTE: `cl.exe` is assumed to be x86, not x64. Compiling it in 64-bit will considerably increase the size of the binary.\n\n## Windows\nJust copy the terminal commands from `Makefile` and paste them into the terminal.\n\n## Linux/*nix OSses\nThis requires downloading `msvc` from [msvc-wine](https://github.com/mstorsjo/msvc-wine). After that, just run `make` and you'll get correct binaries.\n\n# Making the QR code\nOnce you compiled `snake.exe`, on Unix you can run `make qrcode` to generate `code.png`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feimamei%2Fsnake-qr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feimamei%2Fsnake-qr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feimamei%2Fsnake-qr/lists"}