Game Console from Raspberry Pi Pico


Recently, I ordered a Raspberry Pi Pico board for a project. But as this project need more time to start, and also these days are bored, I decided to make a simple gaming console from it. First of all I have never used RP board, so I had to do some studies. After reading on internet, I realize we can program raspberry pi with either using micropython or C++. As micropython is much easier I decided to use it. 

First I downloaded the micropython bootloader from internet. Then, I plugged my board to the computer and I copied the downloaded file to the board. 

From here, I need "Thonny" to upload and test my python code. So I downloaded it and installed. Then I started coding









Test 1 - Simple Game
For this test I decided to use 128*64 OLED and GY-521 Gyro module. I'm not going to use gyroscope, but I will use the accelerometer. Here is the description of my game.

There is a ball, and it is the player object. Then, there are bombs and a cup cake. Player can move according to the accelerometer.  If player hit on cup cake, player will get a one point. Then the bombs and cupcake moves to a random positions. But, if the player hit on bomb, the game will over and restarted.
 
Interface of the game

To design the game engine, I decided to make it like unity game engine. All game objects are stored in an array(List). Then at each frame, object reads inputs from accelerometer, then update function is called for each object. After that, another loop will call draw function of each object. Then the frame buffer will showed in the display. 
So, I wired all the pieces and then started coding. After testing and many times of failings, finally I could create the game I wished. 
But there was a little unfortunate. To exsiccate a single frame, it took about 33ms. It was to much. Without adding any delay, I could run the game at 32- 30fps. But, with the addition of extra game object, frame rate dropped by 2. Then I remembered that I put a 1ms delay for i2c writing function, in the display library. So, I decrease it to 1us. Now I can run the game at 37fps. Much better. But still, needed to optimize. So, my next step is to optimize the code further. Without that I can never draw advance graphics.
You can download all code files from here.

Comments