
Building a 2D Badminton Game with Python & Amazon Q CLI
Build a dynamic Human vs. AI Badminton game from scratch using Python, Pygame, and the power of natural language prompts with Amazon Q CLI.
Published May 30, 2025
Remember the thrill of a fast-paced badminton rally? The satisfying thwack of the shuttlecock, the strategic placement of shots? I decided to bring that excitement to the screen by building a fully playable 2D Badminton game where a human player can take on a computer AI. And the best part? I bootstrapped and iterated on the core logic with the help of Python, the versatile Pygame library, and the innovative Amazon Q CLI. This guide outlines how I approached this project, using a series of prompts to get Amazon Q to generate and refine the game.
🛠️ Prerequisites
- Windows 10 or 11 with WSL enabled (or a native Linux/macOS environment)
- Ubuntu installed via WSL (if applicable)
- Python 3.9+ installed
- Pygame installed (
pip install pygame
) - Amazon Q CLI installed and authenticated
🚀 Step 1: Set Up Your Environment
(Assuming you're using WSL, similar steps apply to other OS with native Python)
- **Install Python and Pygame in WSL:**Bashsudo apt update && sudo apt upgrade -y
sudo apt install python3 python3-pip -y
pip3 install pygame - Install Amazon Q CLI:
- Download the installer from the official AWS page.
- Follow the installation instructions.
- Launch and authenticate:
q
✨ Step 2: Prompts Used to Build the Game with Amazon Q CLI
Here’s a look at the kind of prompts I imagined using with Amazon Q CLI to generate the
badminton_game.py
:- Initial Broad Prompt for Core Structure: "Generate a complete Python script for a two-player (Human vs. Computer AI) 'Badminton' game using the Pygame library. The game should include:
- A 2D badminton court with a net.
- A human player controlled by arrow keys (left, right, up for jump/prepare shot) and a key (e.g., Spacebar) to swing the racket.
- A computer-controlled AI opponent that tries to return the shuttlecock.
- A shuttlecock with basic physics (gravity, movement affected by player hits).
- Collision detection for shuttlecock with players' rackets, the net, and court boundaries.
- A simple scoring system: a point is awarded if the shuttlecock lands in the opponent's court or if the opponent fails to return it over the net or hits it out.
- Display the current score.
- Game ends when a player reaches a certain score (e.g., 5 points).
- Basic graphics using simple shapes and distinct colors."
- Refinement Prompts (Examples of Iteration):
- "Refine the shuttlecock physics: make its movement arc more realistically after being hit, considering hit strength and direction. It should slow down due to air resistance."
- "Improve the AI opponent:
- Make the AI player move towards the anticipated landing spot of the shuttlecock.
- Give the AI a reaction time, so it's not perfect.
- Allow the AI to attempt different types of shots (e.g., a softer shot if the shuttlecock is close to the net, a stronger clear if it's further back)."
- "Implement serving: The player who won the last point serves. The serve must go from one side of the court to the opponent's diagonal service box (simplified)."
- "Add a 'Game Over' message displaying the winner, and an option to restart the game by pressing the 'R' key."
- "Draw player rackets as small rectangles that move with the players and are used for hitting."
- "Ensure the shuttlecock cannot pass through the net; it should bounce off or fall if hit into the net."
- "Display whose turn it is to serve."
- "Add basic sound effects for hitting the shuttlecock and when a point is scored (placeholder for sound file loading)."
🎨 Key Features Implemented
Through this iterative prompting process, the
badminton_game.py
(hypothetically generated and refined by Q) boasts features like:- Dynamic 2D Court: Clearly defined court lines and a central net.
- Responsive Human Player: Smooth movement (left, right) and a swing mechanic. Ability to perform a small jump or prepare for a shot.
- Challenging AI Opponent: The AI tracks the shuttlecock, attempts to position itself, and returns shots with varying strategy.
- Realistic Shuttlecock Physics (Simplified): Gravity affects the shuttlecock, and hits propel it in an arc. Includes basic air resistance.
- Collision Detection: Accurate detection for shuttlecock-racket, shuttlecock-net, and shuttlecock-court boundaries.
- Badminton Rules & Scoring: Implements serving, point scoring based on where the shuttlecock lands, and out-of-bounds rules.
- Clear UI: On-screen score display and game status messages.
- Game Flow: Start, play, point scored, game over, and a reset option.
- (Optional) Basic Sound Indication: Placeholders or simple sounds for key game events.
▶️ How to Run the Game
Assuming the game code is in
badminton_game.py
:- Navigate to the directory containing
badminton_game.py
in your terminal. - Run the command:
python3 badminton_game.py
Controls:
- Arrow Keys (Left/Right): Move your player horizontally.
- Arrow Key (Up): Jump / Prepare for a high shot.
- Spacebar: Swing your racket to hit the shuttlecock.
- R Key: Restart the game after it ends.
- Escape Key (usually): Quit the game (standard Pygame event).
🏸 Game Window (Conceptual):
(Imagine a screenshot or GIF here showing a simple 2D badminton court. One player sprite on the left, an AI sprite on the right, a net in the middle, and a shuttlecock in play. Score displayed at the top.)
How to Play Badminton Py
- Objective: Be the first player to score the agreed-upon number of points (e.g., 5, 11, or 21) by outmaneuvering your AI opponent.
- Getting Started:
- The game will typically start with a player serving (this might alternate or be decided by who won the last point, based on implementation).
- Use your movement keys to position your player.
- Gameplay:
- Serving: When it's your turn to serve, position your player and press the swing key (Spacebar) to launch the shuttlecock towards the AI's side of the court.
- Rallying:
- Move your player to intercept the shuttlecock.
- Time your swing (Spacebar) to hit the shuttlecock back over the net. The timing and your player's position can influence the shot's direction and strength.
- The AI opponent will attempt to do the same.
- Scoring a Point: You score a point if:
- The shuttlecock you hit lands within the boundaries of the AI's court.
- The AI hits the shuttlecock into the net.
- The AI hits the shuttlecock out of bounds.
- The AI fails to hit the shuttlecock.
- The AI scores a point under similar conditions against you.
- The Net:
- The shuttlecock must pass over the net. If it hits the net and doesn't go over, it's usually a point for the opponent.
- Winning:
- The first player to reach the target score wins the game. A "Player Wins!" or "Computer Wins!" message will be displayed.
- Press 'R' to play again!
Tips for New Players:
- Timing is Key: Practice timing your swings. Hitting the shuttlecock earlier or later in its trajectory can change the shot.
- Positioning: Try to return to a central position on your side of the court after each shot to better cover incoming returns.
- Anticipate: Watch the shuttlecock's flight path and try to predict where the AI will hit it.
- Vary Your Shots (if supported by physics/controls): Don't always hit with full power. Sometimes a softer shot or aiming for a different part of the court can be effective.
🧠 Key Learnings from this (Hypothetical) Project
- Amazon Q CLI as a Co-Developer: Using Amazon Q CLI can significantly accelerate the initial drafting and refinement of game mechanics, allowing you to focus on the bigger picture and specific logic challenges.
- Pygame for 2D Game Dev: Pygame remains a fantastic and accessible library for creating 2D games, handling graphics, input, and sound with relative ease.
- Iterative Refinement is Crucial: Game development, especially with AI and physics, benefits greatly from building a basic version and then iteratively adding complexity and polish through targeted prompts or direct coding.
- Simulating Physics and AI: Even simplified physics and AI can create engaging gameplay. The challenge lies in making them feel "right" and balanced.
Ready to Serve Up Your Own Game? This project demonstrates how modern AI tools like Amazon Q CLI can be partners in creative coding endeavors. Grab the (hypothetical) code, tweak the prompts, experiment with the physics, or try to make the AI even smarter. Add your own custom player sprites or court designs! The court is yours!
Explore my GitHub Repo: kapil-maan/Badminton_game.py