Curvy Worm
Sat, Jun 2, 2018MIT’s Scratch platform can get under your skin. No pun inten… Well, no, actually, I’ll just own that. Pun intended. Their visual coding language and web-based IDE are loads of fun, and not just for kids, though definitely also for kids. Every once in a while I get the itch to mock up some old arcade game or other.
Below is my effort at a snake game. Unfortunately it requires Flash. If you can see it, press the green flag at the center of the frame to begin. Use your left and right arrow keys to control the worm’s direction. (So besides needing Flash, you also need a keyboard.)
You can view the project in Scratch, examine the source code, and even fork the project by following this link:
https://scratch.mit.edu/projects/34460794/
Unlike many versions of the game, mine allows you to move at oblique angles. Hence the name “Curvy Worm.” The screen also wraps.
It was quite fun to think about how to implement the worm. The head of the worm is actually the only part that moves. The entire body of the worm consists of overlapping immovable circles, each of which knows how many ‘cycles’ it should last. New circles are constantly being added behind the head, where each then counts down until it disappears. The oldest circle is always at the ‘end’ of the worm and when it disappears it creates the illusion that the tail is moving. When the head of the worm touches a food source, the lifespan for each body segment bumps up, causing the total number of segments to increase and effectively making the worm longer.
Each circle is a clone of just one body segment sprite, and the logic behind its behavior is fairly simple, so the seemingly complicated motion of the worm is actually the result of several independent objects acting in parallel. If you get good enough at the game it will eventually max out; Scratch limits the number of clones that can run simultaneously in a single project.
And yes, I do know that from experience.