Building a Connect4 Game in Pygame with the Amazon Q CLI

Bhavesh GohelBhavesh Gohel
4 min read

What is Connect 4 ?

Connect Four (also known as Connect 4, Four Up, Plot Four, Find Four, Captain's Mistress, Four in a Row, Drop Four, and in the Soviet Union, Gravitrips) is a game in which the players choose a color and then take turns dropping colored tokens into a six-row, seven-column vertically suspended grid. - Wiki

Gameplay of Connect Four

Gif by Silver Spoon - Own work, CC BY-SA 3.0, Link


Hey Bhavesh, What's the motivation to build this game ?

If you’ve ever played Connect 4 (or “Four in a Row”), you know the thrill of lining up four pieces in a row—vertically, horizontally, or diagonally—to claim victory.

Kids at my home playing Connect4 game

Kids at my home playing Connect4 (on left - Aaradhya | on right - Mantra)


In this post, we’ll walk through how to recreate this classic game in Python using Pygame, step-by-step style but without writing any code. 😎

Did i hear, Without Writing Code??? 🤯

YESSSSS !! Without Writing Code!

If you are in, lets go. 🚀 Before going ahead we will need Amazon Q Developer CLI installed in our system.


What is Amazon Q Developer CLI ?

Amazon Q Developer CLI (or Amazon Q CLI) is a command-line tool that integrates Amazon Q, a generative AI-powered assistant, into your terminal environment. It allows you to interact with your development environment and ask questions in natural language, receiving responses and even automating actions through commands.

Head over to this official page to install on your system.

I am waiting.... Please go ahead and install. ⏳️

DONE ?

OK, if it is installed you might see following screen on your terminal when you type:

q chat

Amazon Q CLI on Ubuntu

Now lets ask about our game plan building Connect for with Pygame.

Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. - Wiki

For building game I have typed following ( with little bit of prompt engineering..)

hi help me build a connetc4 pyhton game with pygame. 
go step by step. add option to play with bot or person. 
create a folder name "connect4-pygame" and add a specification file. 
in that add what you think will be each step to build this.

Amazon Q CLI

Based on our input the Amazon Q CLI has started working... first it created the specification file... as we have asked for.

Creating specification file help maintain scope of work and avoid generating different code than what is expected. consider it like giving blueprint of doing something.

Amazon Q CLI

Once Specification file is created, Amazon Q CLI will start working on the implementation.

Amazon Q CLI

It will also create the Readme and other files required as per the specification.

Amazon Q CLI

Once completed you will see what it task has been completed and how to run the application.

Amazon Q CLI

Here is the first version of the application it generated.

Amazon Q CLI

Connect4 Pygame

The game runs great but, there are few bugs in first version, like:

  • The difficulty levels was not selectable

  • There was no button for going back once start playing.

  • UI was okies and player number was not showing.

So lets fix this now by giving this information to Amazon Q CLI on the same terminal we were chatting.

I typed:

add some navigation option and dificulty selection. 
Also make ui morden looking.

Amazon Q CLI

After few iteration... here is the final output.

Gamplay

Gameplay


Conclusion

In this article, we've seen how Amazon Q CLI can transform the way we approach game development. Without writing a single line of code ourselves, we were able to create a fully functional Connect 4 game with both single-player and multiplayer options.

The power of generative AI tools like Amazon Q CLI lies in their ability to understand our requirements and translate them into working code. With just a few prompts, we went from a concept to a playable game, and then refined it with additional features like difficulty levels and improved UI.

Give it a try yourself - you might be surprised at what you can build with just a conversation!

If you still have any queries, feel free to reach out to me on LinkedIn.


Resources

0
Subscribe to my newsletter

Read articles from Bhavesh Gohel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Bhavesh Gohel
Bhavesh Gohel

Chief Technology Officer with a strong focus on IoT Development and a passion for leveraging technology to drive innovation. Currently leading the development of an IoTdashboard - An End to End Industrial IoT Platform that connects machines and sensors to the cloud, enabling data-driven insights and driving business growth. Recognized as an AWS Community Builder and AWS User Group (UG) Ahmedabad Leader, actively fostering a community of technology enthusiasts. Over 10+ years of expertise in the Electronics and Information Technology industry, continuously adapting to emerging trends and delivering scalable solutions. Talks about #aws, #iot, #serverless, #technology, and #iotsolutions.