Graphics is hard. I’m quite pleased with the Gameduino’s usability with the Arduino, and people do sometimes mail me with "hey that was easier than I expected" messages. But this year I’ve been working on another way: a CircuitPython library for the Gameduino.
To be honest, I prefer to use Python for controlling the Gameduino instead of the Arduino. It’s so much quicker to get something running. Of course, you pay a price in speed: Python is slower than C. But very often, your time matters more. This is important whether you’re using it for work or if you want to get something running on Saturday morning.
So here’s a brief walkthrough of CircuitPython on the Dazzler. If you have pledged for the Dazzler Python Game Pack then you can follow along straight out of the box. Connect to the Adafruit Metro M4’s serial console, and type:
>>> import bteve as eve >>> gd = bteve.Gameduino() >>> gd.init()
at this point the Gameduino is initialized. You can confirm this by printing out the screen width and height:
>>> gd.w 1280 >>> gd.h 720
Correct! The Dazzler is indeed 1280x720 pixels. But the screen is black; how about making it bright red:
>>> gd.ClearColorRGB(255, 0, 0) >>> gd.Clear()
This sets the clear color to bright red — that’s the RGB triple of (255, 0, 0).
gd.Clear() actually does the clear.
After you’ve typed this in, though, the screen doesn’t change.
This is because the Gameduino has fancy OpenGL-style double-buffered graphics.
You need to tell it to swap this new screen for the old one, like this:
OK, enough of the bright red. How about a nice relaxing triangle instead?
>>> gd.Clear() >>> gd.Begin(eve.LINE_STRIP) >>> gd.Vertex2f(100, 100) >>> gd.Vertex2f(500, 200) >>> gd.Vertex2f(200, 500) >>> gd.Vertex2f(100, 100) >>> gd.swap()
Begin(eve.LINE_STRIP) starts drawing a line strip, a connected series of lines. Then each
gd.Vertex2f() draws one corner of the triangle. The fourth call closes the loop.
The Dazzler can do a lot more, of course. It can paint images on the screen almost as easily. First load the image with:
>>> gd.cmd_loadimage(0, 0) >>> gd.load(open("blinka.png", "rb"))
Then draw it like the triangle above, but with
LINE_STRIP changed to
>>> gd.Clear() >>> gd.Begin(eve.BITMAPS) >>> gd.Vertex2f(100, 100) >>> gd.Vertex2f(500, 200) >>> gd.Vertex2f(200, 500) >>> gd.swap()
How about filling the screen with 100 random Blinkas?
>>> import random >>> gd.Clear() >>> gd.Begin(eve.BITMAPS) >>> for i in range(100): ... gd.Vertex2f(random.randrange(gd.w), random.randrange(gd.h)) >>> gd.swap()
Vertex2f() call chooses a random point on the screen, and draws the Blinka sprite there.
It’s as easy as that. Of course the Gameduino can do much more, and the CircuitPython API makes it all quite straightforward. The library will ship with a few small games in CircuitPython, as well as some demos and tutorials. Here’s one I’m currently working on, a match-3 game: