Game render approach - keycube/K3_embedded_game GitHub Wiki
For our game, we chose to display a 3D perspective of a cube seen from the inside. Since it's a rather simple shape to draw, we considered two approach :
- Use pre drawn bitmaps to display shapes.
- Create render on-the-go by drawing shapes (primitives)
Using bitmaps
We already experimented how to use bitmaps in previous examples. So this solution is the easiest as we only need to edit images in order to alter the game look.
The main idea is to have a main background image displaying the blank cube (without any key pressed), and then drawing a smaller image for each key above the main image. This way, we can avoid to store a bitmap for each key combination.
Using primitives
One drawback of using bitmap is, if we want to animate anything, we need several images. In this case, this means using much more memory and we are quite limited on this point. An alternative to using prerendered bitmaps is using a library providing shape draw functions. Indeed, we could draw the same perspective cube shape we wanted, only by using lines during run time. This method has the advantage to be more flexible, as it provides the ability to "interpolate" between two images. This way we can create animations more easily just by lerping lines positions from one value to another.
The shape drawing library
This library is quite easy to use:
from adafruit_display_shapes.line import Line
splash = displayio.Group()
display.root_group = splash
line = Line(0, 0, 400, 240, 0xFFFFFF)
splash.append(line)
Just include the shape you want to draw, call it, and it generates a bitmap for you.
We use this in the code example primitive_test
for reference.
This library generates shapes as bitmaps from code. This means we must regenerate the whole bitmap if we want to change the shape. And this is where the performance issue arise: with only 5 lines drawn (cf. code example), if we try to redraw them each frame (to animate them in our case), we end up with only ~2 frame per second, which is not enough to make a fast animation. A slightly better approach to animate the line, is to only interpolate the x and y position, but these properties are from the bitmap, and are not specific to the shapes. This defeats the purpose of using generated shapes, as we can do the same thing with pre rendered bitmaps.
Maybe using another library written in C rather than Python would be more effective, such as the Adafruit GFX library, as used in this guide. However, we did not found a way to make it work while using CircuitPython on the PICO, even though we can find this library compiled in micropython in the library package.
Guide used for this section.