DEV Community

Cover image for Build a Smart Snake Game Using Amazon Q CLI

Build a Smart Snake Game Using Amazon Q CLI

Introduction:

In the world of programming, few projects are as nostalgic and fun to build as the smart snake Game. Whether you're a beginner looking to sharpen your logic or an experienced developer revisiting the retro charm of early gaming, Snake is a timeless coding challenge.

In this blog, we’ll walk you through how we reimagined this game using Amazon Q CLI—a powerful AI-powered command line interface that helps accelerate development through conversational and code-driven assistance. By combining the intuitive support of Amazon Q with core programming principles, we built an interactive and fully functional Snake Game from the ground up.

Setting Up an Environment:

Setting up the deployment environment is nothing but installation of Amazon Q CLI package in the host. Here, in this project we have built a dedicated ubuntu server on which we have installed amazon-q cli package by following below steps -

  • Download amazon-q cli package for ubuntu
wget https://desktop-release.q.us-east-1.amazonaws.com/latest/amazon-q.deb
Enter fullscreen mode Exit fullscreen mode
  • Install the package
sudo apt-get install -f
sudo dpkg -i amazon-q.deb
Enter fullscreen mode Exit fullscreen mode
  • Launch amazon-q cli terminal. While doing that, it will ask to authenticate yourself using Builder ID or IAM Identity Center.

  • Add below parameters in sshd_config file in /etc/ssh/sshd_config

AcceptEnv Q_SET_PARENT
AllowStreamLocalForwarding yes
Enter fullscreen mode Exit fullscreen mode
  • Restart the sshd service
systemctl restart sshd
Enter fullscreen mode Exit fullscreen mode
  • Disconnect from the SSH session and reconnect.
  • After relogging into the server, type "q chat" to start chat session.

Why I chose this Game & Let Me Introduce About the Features ?

This is a smart snake game which everyone used to play on a mobile once upon a time, in today's generation it has been very nostalgic to think about this game and experience again. It might provide an amazing user experience if some new features can be added with existing one, so this will make the game a little bit exciting than the previous one.
Here, we included couple of more interesting features that can bring back the excitement which is lost today -

  • The fundamental feature has been remained same like old days where a snake is seen chasing for a rat and rat is moving here and there to be in safe side.

  • Every time snake catches the rat, it adds 20 points. Sometimes, user will receive some questions related to python or aws which will boost the earnings.

  • Every correct answers to python questions will sum up 100 points on current earnings, whereas every correct answers of AWS questions will add poison that snake can utilize to hunt rat directly by throwing rather than chasing.

  • Snake will gain good length with having more rats as a food. There we have to be little conscious if multiple snakes are not intercepted to each other that can lead the end of a game.

  • For newly joined players, there is an option to opt for auto-chase feature which can guide newly attempted candidates to have an experience on "How to Play". Once the candidate is confident about the steps to followed, then another option "Disable Auto-Chase" can be chosen to take the feelings of playing on your own.

Prompt Management:

Prompt selection is a critical steps to be done to have the expectations reflected in reality, as amazon q works upon the prompts that user is giving, so response would be more closer to expectations if prompts are given in correct and detailed manner. But how can we give better prompts -

  1. First, provide complete details of the objective which is to be developed.

  2. Second, give each details of the features you want to include. Explain how that features should work in the application you're going to build, also elaborate if there is any limitations/rules to be applied.

  3. Use some adjectives to highlight any particular keyword. For example, if you want to explain the attractiveness of UI/UX of any application, that adjectives can be playing an important role to help amazon q understand the specifications of application.

Below prompt has been given while developing the game -

Detailed prompts

How Amazon Q Comprehended the User Prompts and Converted into Code:

Based on the prompts given by user, amazon q started comprehending the prompts line by line and understood the requirement for which it has created couple of configuration files to develop game logic and UI to represent over the internet.
It has created below files to develop the gaming application interface -

  • Dockerfile
  • README.md
  • docker-compose.yml
  • script.js
  • snake-game.html
  • style.css

After creating the configuration files, we have instructed amazon q to build a docker image of that application and create a container with (80:8000) port mapping, so it has created "Dockerfile" and "docker-compose.yml".

> Build docker image using the script files it generated and run a container that accepts the traffic at 8000 port at container interface from 80 port of EC2 instance.
Enter fullscreen mode Exit fullscreen mode
# Snake Game Project Explanation

This directory contains a modern implementation of the smartsnake game with educational elements focused on Python programming and AWS knowledge. Here's a
breakdown of the key files:

## 1. script.js
This is the main JavaScript file that contains all the game logic:

• **Game Mechanics**: Implements the smart snake game with a moving rat (instead of static food)
• **Auto-Chase Feature**: The snake can automatically chase the rat using a pathfinding algorithm
• **Educational Challenges**: Periodically presents Python and AWS questions to the player
• **Reward System**:
  • Python questions give 100 points and make the snake longer
  • AWS questions give "poison" that can be used to slow down the rat
• **Game Controls**: Handles keyboard input, button clicks, and game state management

The script includes various functions for:
• Initializing and drawing the game
• Moving the snake and rat
• Collision detection
• Challenge generation and validation
• Score tracking and high score persistence

## 2. snake-game.html
The HTML structure of the game that includes:
• Canvas element for rendering the game
• Score display and control buttons
• Modal for educational challenges
• Game over screen
• Instructions section

## 3. style.css
Provides the visual styling for the game with:
• Retro gaming aesthetic using "Press Start 2P" font
• Animated gradient background
• Neon-style elements with glow effects
• Responsive design for different screen sizes
• Styling for game elements, modals, and buttons

## 4. Dockerfile
Configures a Docker container using:
• Nginx Alpine as the base image
• Copies the game files to the Nginx web server directory
• Configures Nginx to listen on port 8000
• Exposes the necessary port for web access

## 5. docker-compose.yml
Simplifies deployment with Docker Compose by:
• Building the container from the Dockerfile
• Mapping port 80 on the host to port 8000 in the container
• Setting the container to restart automatically unless stopped manually

## 6. README.md
Provides comprehensive documentation about:
• Game features and mechanics
• Educational content
• Technologies used
• Project structure
• Installation and running instructions
• Future enhancement ideas

## How to Run the Game

You can run the game using Docker:
bash
docker-compose up -d


Then access the game by opening a web browser and navigating to http://localhost.

Alternatively, you can simply open the snake-game.html file directly in a web browser without Docker.

The game combines entertainment with educational content, making it a fun way to learn about Python programming concepts and AWS services while playing a
classic game.
Enter fullscreen mode Exit fullscreen mode

Now the game is ready to play. Let's have look how this looks and creates a vibe 😊

Smart Snake Game

Moving snake towards the rat

Python quiz to be asked to earn 100 points

Lessons Learned

Our weekend project taught us several valuable lessons:

  • AI coding assistants can develop such an amazing things within few minutes with a magic.
  • Tools like AI coding assistant has not only been helpful for coders, but also it has been a dream platform for non-codes who has been dreaming of building such an amazing application, but couldn't due to lack of web development skills.
  • From business point of view, this is really a cost effective solutions to be introduced.
  • Pygame is a crucial package to make this sort of games.

Conclusion:

This is really a good experience to have such a game within few minutes and moreover when it's working as you expected, that would really be a pleasure. But this amazon q cli is not only just for developing application, but also infrastructure build using terraform and writing some other configuration as per the requirements, making things in a single place and package it properly for deployment can be done without having any headache. This tool opens up lots of opportunities to be attempted, the real need is bring up some amazing ideas which you can provide to amazon q to get this implemented.

The way I developed this game can be taken for any other tool or services to develop, the only things to be focused is the approach we're going to take. So, altogether this has been really game changing era of AI where we are not bound to dream only, rather to apply in the real world as well.

I have published the files for this game in GitHub. Please have a look into this and share feedback 😊
GitHub: https://github.com/dasanirban834/game-development-using-amazonQ
Game Link: http://alb-1907765677.us-east-1.elb.amazonaws.com/

Thanks!!

Top comments (0)