0%
0%

HariFun 203 - Pico Snake

Slithering Snake Game in MicroPython

Similar projects worth following
2k views
Snake Game, but with a twist!
The snake actually slithers as it moves.
It was a LOT of fun trying to figure out how to do code this.

• How hard could it be?

Hari Wiguna05/09/2021 at 05:56 0 comments

There are probably easier ways to implement this, but I really enjoy working within self imposed constraints.  I wanted it to be on the 128x64 pixel OLED that I have, so I could not afford to waste screen real estate on big sprites. The sprites I use are only 4x4 pixels! I wanted it to be written in MicroPython, because I enjoy the interactivity and super fast no compile dev cycles.

After many false starts I settled to build the snake out of four curves: A, B, C, and D.

By drawing the four sprites at various offsets, I was able to draw the snake slithering in all horizontal and vertical directions.

The challenge is how to draw a continuous snake when turning.  Some turns are naturally easy, but some are problematic depending on which phase of the slithering the snake is at when user asks for a turn.

For example if snake is going left to right, and user press down when head is C would require a different curve when the head is B, or A, or D.

Since we need to know when the snake's head hit an apple, the actual head point must be consistently maintained.  Note the dots on the diagram below. That's the actual snake segment positions, the slithering curves are drawn relative to those points.

Not so obvious is when going the opposite direction, it is not a simple matter of reversing the sequence of segments.  Note that going left to right, head is on the bottom right corner of C.  However going right to left, head needs to be on the bottom left side of C otherwise, the snake would lag when eating an apple in that direction :-(

I actually end up writing a utility to visualize the snake segments and create lookup tables.  It is ugly, but I was unable to find an elegant solution.

Surprisingly MicroPython is more than fast enough to do all these lookups and draw the snake basically pixel by pixel!

Share

Discussions

cafekarel344 wrote 06/06/2021 at 18:36 point

Looks fun. I want to modify this snake project for my dream snake blog, you can see here https://dreamsofsnakes.com/

Are you sure? yes | no

Sanjay Johny wrote 05/13/2021 at 03:13 point

Really nice !

Are you sure? yes | no

apaularoth wrote 05/12/2021 at 20:59 point

You inspired many readers with your creative approach to this old game.

Are you sure? yes | no

nolaforensix wrote 05/12/2021 at 16:54 point

This is a really cool project and would be a great basis for people to get involved in micro-video game programming!  Would you consider posting a simple schematic and indicate where you got the parts (OLED, buttons, etc.)?

Are you sure? yes | no

tyler wrote 05/11/2021 at 01:07 point

This looks like a whole new take on the game, we should port it to the web!

Are you sure? yes | no

Hari Wiguna wrote 05/11/2021 at 03:20 point

Are you volunteering? :-)

Are you sure? yes | no

tyler wrote 05/11/2021 at 14:30 point

Well, maybe... (ooh, python!) I guess I'll put it on Github if I do. Need a coding project anyway...

Are you sure? yes | no

Mike Szczys wrote 05/10/2021 at 15:15 point

I've never seen something like this and it adds a whole new layer of joy to the classic game. Nice work!

Are you sure? yes | no

Hari Wiguna wrote 05/11/2021 at 03:25 point

Inspiration is a strange thing.  I don't recall how I got the idea, but once it was in my head, I knew it would be cool.  Thanks for the encouraging comment Mike!

Are you sure? yes | no

Similar Projects

Project Owner Contributor

mechaFeet

Sarah Petkus

Project Owner Contributor

Fruit Coder: a game to get girls coding

Kyall

Project Owner Contributor