Raspberry Pi Pico 2 Snake game console

It’s Maker Monday, on which we like to showcase excellent builds by the Raspberry Pi community. This one made its way into the latest issue of Raspberry Pi Official Magazine and was created by our friend Arnov Sharma.

If you owned a Nokia mobile phone in the late 1990s, then you will likely remember the game Snake. It came preloaded on the Nokia 6110 in 1997 and took the world by storm as users — including a host of celebrities — became utterly enthralled by the simple premise and potential of racking up a high score.

the console resting on a busy desktop setup with a short green snake heading towards the red dot representing food
Although you might have expected a Snake game to be written in Python, this one was developed using a variant of C++

For those unaware, Snake was a spin on the 1976 game Blockade by Gremlin Industries, and the idea was that players would control a limbless reptile around a small area, gobbling up food while trying to avoid making the snake hit itself. With each bite, the snake would grow longer, making it more likely to collide with the walls or its own tail. So, it took quite a bit of skill to precisely move around the screen — a task made more difficult using a phone’s hard keypad. 

Arnov Sharma was reminded of this game when he got hold of a matrix panel. Given the blocky, pixelated nature of Snake’s rather primitive graphics, he realised the panel could lend itself to a replication of the game, so he began to consider the practicalities of producing a unique game console.

“My objective or goal for developing this project was to create a gaming console that used my 64×32 P3 matrix panel,” he says of a Waveshare unit he purchased from PBCWay for $22. “I thought this console would be useful for coding simple games.”

With that in mind, he began to think about how it would be powered and how this large screen could be comfortably held and controlled.

Arnov has lost his Snake game as a big red GAME OVER screen has appeared
The LEDs are used to great effect to clearly show when the player’s game has come to an end; the resulting score is displayed in the top-right corner

Recreating hiss-tory  

Since Arnov was looking to create retro-style games — the likes of which were commonplace back in the 1970s and 1980s — he decided he didn’t need the immense processing power offered by a Raspberry Pi 5. At the same time, he realised he would still require a powerful device that had a fast CPU, a good amount of RAM, and flash storage, so he decided he would create a streamlined build based around Raspberry Pi Pico 2.

“This project’s code was quite large and it involved the use of a matrix library, which requires speed and computing power,” he explains. “Raspberry Pi Pico 2 was ideal for this project.”

At the rear of the console is a Raspberry Pi Pico 2 microcontroller connected to a custom-designed PCB and powered by an 18650 lithium battery; this is powerful enough to run a game of Snake quickly and smoothly

Once the main two components were selected, Arnov then turned his attention to how the game console would be controlled and how it would look. With such a large screen, it made sense to have the controller on one side and something to grip on the other.

Fang-tastic timing

In that sense, the game console would resemble the Nintendo Switch to some extent, although this format also conjures up memories of Sony’s PlayStation Portable. In any case, he reckoned that the types of games he would create wouldn’t need more than a D-pad (that is, a set of four directional buttons: up, down, left, and right), although this did mean foregoing a fire button. “The most basic game I was able to create in a week was the classic Snake game,” says Arnov. This took up most of the project’s development time.

“I completed the entire project in one day, but it took me a week to finalise the code,” he notes. This is impressive, since the project entailed building a 3D model of the console using Autodesk Fusion 360 and working out where the components would go. A custom Pico Driver Board and Button Board were also designed using PCB CAD software. These were produced by PCBWay.

“I created a frame-like part that transforms the matrix into a handheld game console,” Arnov explains. “The hardware also features a power circuit that includes a power management IC, a lithium cell, and a few mandatory components. This setup powers both the matrix and Pico.”

“The main challenge was the onboard power source; I created a customised circuit that houses the power management IC that powers the matrix and Pico. The challenge was to make the circuit smaller and lighter. I am still thinking about making it smaller, which I may address in a future edition.”

Since Snake only requires directional controls, the console has just four buttons

Scaling up

Assembling the components was relatively straightforward. “The setup works by connecting Pico 2’s GPIOs to the matrix’s HUB75 connector and using necessary libraries to control the matrix,” Arnov says.

“To power Pico 2 and the matrix, we used a power circuit that uses a lithium ion cell with a nominal voltage of 3.7V. However, Pico 2 and the matrix require a stable 5V, so we used a power management system to boost the cell voltage from 3.7V to 5V. We added a button board with I/O pins that connect to Pico 2’s GPIO pins to control the snake’s movements.”

Arnov tested out the build using a cellular automation called Game of Life, which was created in 1970 by British mathematician John Horton Conway. He had previously converted the code for another project, which had allowed him to see something happening on the screen, so he knew the connections and configuration worked. He was then able to turn his attention to producing a game. “In terms of coding, this was put together from scratch,” he says.

Arnov represented the snake as a series of green LEDs and the food as randomly appearing red LEDs, which the player must ‘eat’ by using the buttons to direct the legless reptile towards them. When the snake collides with the red dot, its length expands and the on-screen score is updated. If the snake collides with itself, the screen freezes, turns red, and displays the words ‘Game Over’ along with the final score. 

A new game is ready to play within five seconds, which gives the player enough time to grab a modern smartphone and take a quick photo if the score is impressively high. Given how addictive Snake is, the player is then likely to get stuck in once more — but Arnov is now considering which other classics he may be able to play on his portable console. “I’d need to build them myself or port an existing game, but that’s something for the future,” he says.

Arnov builds lots of cool things — subscribe to his YouTube channel for more

The post Raspberry Pi Pico 2 Snake game console appeared first on Raspberry Pi.



from News - Raspberry Pi https://ift.tt/E5oCxgd

Comments

Popular Posts