S
For this week, I wanted to build a connect four game. Connect Four was one of my favorite games when I was very little, and I initially hoped to build a full GUI in pygame or tkinter, but when I found that none of the implementations that I had tried were able to port to micro python, I instead moved to building a terminal game.
I wanted to use the controller board for the Gameboy that I'm working on, but since I have since broken the buttons out into a board without a processor, I used an older iteration in which I tried to build the full device on a single PCB.
I first worked on the storage of the board in memory, and then wrote a function to display the ‘board’ in the terminal using ASCII characters. Next I worked on refreshing the screen through clearing and redrawing after each interaction with the pico, which was initially difficult since the typical OS package in python does not precisely translate to micro python in this domain, but some quick stack overflow research solved the problem.
Next I worked on the game logic. The core loop would take input from the player, at first in the form of a number from 1-7 that would be entered on the keyboard rather than through the device. I defined a quick function to check all possible win conditions, and another that allowed each player to take turns claiming a square interspersed with checking for win conditions.
The final challenge was rendering a cursor at the top of the screen which the player could manipulate using the pico and connected buttons. To do this, I wrote a function which looped continuously until a player pressed the buttons which represent 'a' and 'b' on the gameboy, and allowed the player to move a character at the top of the screen left and right until they were ready to select. Once I had a definition of the selected column and redefined the render function to update based on the character position, this function could easily merge with the other functions that were designed to take in a numeric input from a keyboard.
The result is the functioning connect four game that you see below:
I am very happy with the result of this project, and I think that it is a good example of how to build a simple game in python. I think that the next step for this project would be to build a GUI in pygame or tkinter, and to port the game to the gameboy. I think that this would be a good project to work on over the break, and I hope to have a working version of the game on the gameboy by the end of the semester.