Saturday, August 3, 2019

Mini Electronic Connect4 Game

I've just finished a new electronics project: it's a mini electronic connect4 game, with a built-in computer opponent. It runs on an Adafruit M0 Featherboard and uses an bi-colour 8x8 LED matrix display as the gameboard. There are three buttons for controlling the player's moves in the game. The device has a 3D printed case, is portable, battery powered and USB-rechargeable (via the featherboard). The game functionality is programmed in Arduino and features an AI opponent, with a moderate level of difficulty.

I had the matrix display sitting around for a while, and I had been trying to think of a cool project to use it for. When a group of colleagues at work started up a connect4 python bot programming challenge, I got inspired to try and implement a bot into the lower-powered processing capabilities of a smaller micro-processor, so I thought this would be a great use for the display. I started off by testing out the display: I soldered on the backpack to the LED matrix and wires onto the power, ground and I2C lines on the backpack so I could connect it to a breadboard. I hadn't soldered on the headers for my featherboard M0, and I had a Gemma M0 sitting around so I connected it up to the backpack using some jumper leads and alligator clips to test it. I used the 3.3V output from the Gemma: seems to drive the matrix with no problems. I downloaded the Adafruit libraries to drive the LED backpack and the Adafruit GFX library, which provides some basic functionality for drawing primitive geometry and rendering text to the display: I ran one of the example scripts for the backpack and all was working well.

I started developing the code for a connect4 game and an AI to play against. The AI uses a Monte Carlo Tree Search algorithm for evaluating the next best move based on simulating games to approximate win/loss statistics. This seemed like the quickest and easiest approach to develop, seemed to require less memory that a Minimax approach, and I'm assuming provides a little bit of random variation in the AI's play, such that games against a human would be a bit more interesting.

Played around a bit with the AI to try and get it to prioritise early wins/avoiding early losses: cost for loss = 42-n_moves, cost for win = 42-n_moves, then score = win_cost - loss_cost: works OK, but doesn't correctly prioritise avoiding when the player will definitely win on the next turn: so I had to hard code a few initial checks in so the AI first looks for instant win moves, then for moves which block the player winning on the next move, then Monte Carlo.

Once I was happy with the code, I started working on the design of the physical device and casing. I started off with the idea of having a custom-built rotary input wheel on the top of the game that would allow the player to turn the wheel to move the location of the piece to drop, then click the wheel to drop the piece. I bought some mini trimpots to use as input with custom-designed 3D printed cog to interface with trimpot. After experimenting with a couple of 3D prints for the knob, I ended up dropping this idea as the trimpots I could find all had so much friction, that they were almost impossible to turn using a single finger without having to have a giant sized knob to achieve the appropriate torque. I ended up instead going with a more traditional arcade button layout with one button to move the cursor position left, one right and a third button to place the token.

I designed the 3D printed case to look a little bit like a connect4 board, except that it's quite fat, which is needed to fit everything inside (particularly the matrix display, which is quite thick when the breakout board is soldered on). It printed from multiple sections that are then glued together.

For the electronics, I began by laying out in the case where the components would fit and how I would wire everything together. The case is quite small, so I had to plan out the layout of wires to ensure it did't get too messy on the inside. I wired all the electronics together first, separate from the case, and tested everything was working, before moving on to connecting and gluing on the case.

The tabs I had designed to hold the micro to the case turned out to be a touch too big for the mounting holes on the board, so ended up having to snap them off and superglue the board to the case. The other sections of the case all fit together nicely, and I ended up using superglue to connect all the separate sections of the case together. The final product is fairly neat looking and compact: it even does look a little bit like a mini connect4 board!

I really enjoyed working on this project: it was fun to design a case that had a couple if different sections and was a little less "flat" than some of the previous 3D printed electronics projects I've worked on before.

Design Files:

3D Printed Case STL and OpenSCAD files:

Code for game control and AI:

Friday, April 26, 2019

PyWeek 27: Breakout Blue

"Breakout Blue" was our (Team Chimera) team entry for PyWeek 27 (April 2019), a twice yearly video game development competition that gets competitors to build a complete game from scratch in seven days using the python programming language. This competition's theme was "six", and we made a game in which you control six inmates trying to work together to perform a prison escape.

I teamed up with Lucid Design Art, the other half of Team Chimera: it was our first collaboration in about two years, and it went well. I worked on game programming, music and sound, and Lucid worked on game design and artwork. The game involved a combination of puzzle elements and a little bit of stealth, and turned out well. We placed second in the team entries and tied for second overall with a score of 4.13/5.0 so we were pretty pleased with the result.

Playthrough Video:

You can download and play the game from source (requires python (2 or 3) and pygame installed).

Saturday, March 16, 2019

3D Cube Rolling Ball Maze on Carvey

A while ago I made a rolling ball maze on Carvey, a soft-CNC machine at my local maker space. For a bit of extra fun, I've extended this now into the third dimension and created a cube-based rolling ball maze. The maze has six sides that are all inter-connected and makes for a bit more of a challenging experience. The cube maze is pretty much the same concept as a bunch of cheap plastic cube mazes you can but on Amazon etc. but a little bit bigger. To generate the actual maze itself, I developed a random maze generator in python using a recursive backtracking algorithm. I extended the approach to build the maze across the surface of a cube by placing maze nodes in a regular grid across each of the six faces of a cube and placing a number of random edges between adjacent face nodes to create paths that traverse the whole grid surface. The scripts I made output the generated maze as an image showing an "unfolded" view of the cube, and also spit out SVG files for carving.

The maze is carved as six separate sections from 9mm MDF and six 2mm thick clear acrylic panels (to stop the ball falling out). The panels are then glued and screwed together by hand. I had a look into a couple of different options for the track size of the maze. I have a few 2mm steel ball-bearing balls, but found that these don't actually work well: they are so small that roll too quickly in the maze and it becomes very frustrating to move them around by tilting the maze. I ended up using a silver coloured decorative ball for cake decorating: turned out to have the right size (approx. 4mm diameter) and rolls quite nicely.

Putting it all together was a little bit finicky: seeing as Carvey can only cut in 2.5 dimensions (three axis carving) I had to design the pieces to be of varying sizes such that they all interlock together and allow for the tracks to join up. The little bits of wood at the bottom were glued in as a way to chock the two smallest panels up against the clear panelling so the whole thing stays together.

Design files for the maze I ended up using (as SVG files that can be used for Carvey/Easel) can be downloaded here:

Python scripts for generating new random mazes and associated design files available via Github:

Saturday, December 22, 2018

More 3D Printed Christmas Tree Decorations

Made a few more Nintendo-themed 3D printed Christmas decorations, to add to my existing ones. These are a bunch of power-ups form Super Mario Bros. 3 on the NES. Using a python script to read in an image file and convert into ASCII code for OpenSCAD, then importing this into an OpenSCAD file to generate the stl files for each of the five colour layers, each printed separately and then "clicked-in" by hand to finish.

You can download the stl files to print here: Mario Power-up Christmas Tree Decorations (thingiverse)

Merry Christmas!

Sunday, November 18, 2018

PyWeek 26: Storm Drain Odyssey

"Storm Drain Odyssey" was a team entry by Team Universe Factory (for which I was part of) for PyWeek 26 (October 2018), a twice yearly video game development competition that gets competitors to build a complete game from scratch in seven days using the python programming language. This year's theme was "Flow" and we created a 3D dungeon exploration/puzzle game where you control a pet goldfish navigating your way out of a sewer labyrinth.

I'd been itching to be part of a team entry (I've done the last three comps solo), so I was stoked when I was able to join Universe Factory (definitely one of the most competitive teams in PyWeek, with several existing titles :) ) ... the team was very welcoming to new-comers. I focussed on graphics, 3D modelling and graphical programming (with a bit of sound-design) and it was a good experience working in a team but not leading or being responsible for major game mechanics: Cosmologicon, our team lead, ran a very tight ship and was very organised, so everyone seemed to be on the same page. I learnt a ton of practical experience in version control and managing working on software in a team (which I don't do much), so that was cool too. I also learnt how to use scripting with OpenSCAD to quickly turn around 3D environment models (which I documented in a game page blog here), a technique that I think I would definitely try to use again.

The game ended up coming second in the team division, which we were fairly happy with. There's a source and Windows standalone version that can be downloaded from the game page here:

"Storm Drain Odyssey" on

Saturday, November 3, 2018

Record Clock

It was my wife's birthday coming up soon, and I knew she wanted to get a new clock for our living room, so in the do-it-yourself spirit, I decided to make her one. I'd seen a couple of previous projects where clocks had been build around an old LP vinyl record, and wanted to try the same idea myself, and see if I could do it on Carvey (3D carving machine).

I picked up a $7 clock from Kmart and pulled off the case to retrieve the clock mechanism inside. I bought a second hand 7'' single vinyl record ("Total Eclipse of the Heart" by Bonnie Tyler, quite randomly :) ) which I would use to carve on my clock face using Carvey. I ended up going with a 7'' as I couldn't fit a 12'' inside Carvey. I knocked up a quick design in Easel by just using a standard font and a few icons for fun arranged around a 7'' circle. To do the setup in Carvey, I used a bit of MDF waste board and an additional 1-by-1'' piece of 2mm scrap wood to raise the height of Carvey's smart sensor to a little bit above the 1mm thickness of the record: because the record is a circle, you can't get it under the clamp while also being able to carve on all sides. I carved with a 1/16'' up-cut bit (the only 1/16'' bit available), and set the material type to "acrylic" (seemed the closest match). This worked pretty well, however the vinyl is pretty soft and so there was a pretty sizeable bur of vinyl that was stuck around the bit for most of the process: this ended up "buffing" around the edge of each number cutout, but didn't really have too much of a negative impact on the final result.

Once the clock face was carved, I glued on the clock mechanism to the back, placed the clock hands back on and pasted on a family pic to the record label area: if I'd found a second hand single of a song my wife really liked, I might have just kept the label as-is.

Final result looks pretty cool, and not bad for a $9 present :) (I might be a bit of a cheap skate sometimes, but in this case I did get her a few other things too!)

Friday, August 31, 2018

"Ret-Row": a 5th Kajam entry

"Ret-row" is my entry for the 5th Kajam, a month-long event in which people make a video game from scratch, focusing each time on a specific aspect of game making, as chosen by its host. This Kajam's theme was "Retro", so a made a little retro style shoot-em-up where you control a row-boat. I started off pottering with some pixel art and music, not thinking I would actually submit anything, but found that the game started to take shape after a week or two, so decided to see it through to completion.


I did all of the game's pixel art on my phone in spare pockets of time using the app "8bit Painter". The music was done in Musescore using a set of NES instruments, to give a bit of an old-school flavour. The game is programmed in Python and uses Pygame.




Mac OSX 10.7+:


Source code distribution (compatible with Windows/Mac OSX/Linux, requires python and pygame):