Welcome to the Bunny Game, a beginner-friendly coding project designed to make learning Python fun and interactive! This project helps you explore the basics of programming by creating a simple game. It uses the Tkinter library to build graphical user interfaces (GUIs) and introduces key concepts like event handling, random movements, collision detection, and more.
The Bunny Game was born from a desire to make coding relatable and exciting for my kids. They often asked me, "What do you mean when you say you're coding?" It was hard to explain my work in a way they could understand. One day, I decided to turn it into an opportunity for them to learn, saying:
"Letβs make a game together, and Iβll show you what code is and how it can bring your ideas to life."
Since my daughter loves bunnies, we chose to build a game with a bunny as the main character. The result? An engaging, easy-to-understand project where they could not only play but also see how the code worked and even improve it themselves.
- Bunny: A cute character that can be controlled using the arrow keys π.
- Carrot: A sneaky carrot that moves randomly every second to "avoid" being caught π₯.
- Score Counter: A counter at the top of the screen that keeps track of how many carrots the bunny has caught.
The Bunny Game teaches fundamental coding principles while encouraging creativity. Youβll learn how to set up the game, understand the code, and even modify it to create your own features.
Python is the programming language weβll use to create and run the Bunny Game. Follow these steps to install Python on your computer:
-
Download Python:
- Open your browser and visit the official Python website.
- Click the Download Python button. The website will suggest the correct version for your operating system (Windows, macOS, or Linux).
-
Install Python:
- Windows:
- Open the downloaded file (e.g.,
python-3.x.x.exe
). - Check the box Add Python to PATH (this is very important!).
- Click Install Now and follow the instructions.
- Open the downloaded file (e.g.,
- macOS:
- Open the downloaded
.pkg
file. - Follow the prompts to install Python.
- Open the downloaded
- Linux:
- Python is often pre-installed. Check by typing the following command in the terminal:
python3 --version
- If Python isnβt installed, use your package manager to install it:
sudo apt-get install python3
- Python is often pre-installed. Check by typing the following command in the terminal:
- Windows:
-
Verify the Installation:
- Open a terminal or command prompt.
- Type:
or
python --version
python3 --version
- If you see a version number (e.g.,
Python 3.10.x
), Python is installed correctly.
You can write and run Python code using just the terminal, but a code editor makes the process much easier. It provides features like syntax highlighting, error checking, and easier navigation of your code. Here are some beginner-friendly options:
-
Thonny:
- A simple editor specifically designed for beginners.
- Download Thonny.
-
VS Code:
- A powerful, flexible editor that supports many programming languages.
- Download VS Code.
-
Notepad++ (My Recommendation):
- I personally use Notepad++ because itβs simple, lightweight, and free.
- Download Notepad++.
Choose the one that feels easiest for you to use.
The Bunny Game requires two libraries:
-
Tkinter:
- Tkinter comes pre-installed with Python and is used to create the game window and controls.
- On most systems, you donβt need to install it separately. For Linux, you may need to install it with:
sudo apt-get install python3-tk
-
Pillow:
- Pillow handles the images (bunny and carrot) in the game.
- To install Pillow, open a terminal or command prompt and type:
pip install pillow
-
Create a Folder:
- On your desktop, create a new folder named
BunnyGame
.
- On your desktop, create a new folder named
-
Move the Files:
- Copy the following files into the
BunnyGame
folder:bunny.py
(the game script)bunny.png
(the bunny image)carrot.png
(the carrot image)
- Copy the following files into the
-
Check the Folder:
- The folder should look like this:
BunnyGame/ βββ bunny.py βββ bunny.png βββ carrot.png
- The folder should look like this:
-
Open Command Prompt or Terminal:
- On Windows: Press
Win + R
, typecmd
, and press Enter. - On macOS/Linux: Open the Terminal app.
- On Windows: Press
-
Navigate to the Game Folder:
- Use the
cd
command to move into theBunnyGame
folder. For example:cd Desktop\BunnyGame
- Use the
-
Run the Game:
- Type the following command and press Enter:
python bunny.py
- Type the following command and press Enter:
-
Play!
- The game window will open. Use the arrow keys to move the bunny and catch the carrot!
Hereβs a quick breakdown of the gameplay:
- Bunny Movement:
- The bunny starts at the center of the screen and is controlled using the arrow keys.
- Carrot Movement:
- The carrot moves in random directions every second to avoid being caught.
- Score Counter:
- Every time the bunny catches the carrot, the score increases, and the carrot jumps to a new random position.
The game uses Python's Tkinter
library to create a window and canvas for the game elements. Here's what happens:
- The bunny and carrot are represented by images loaded from your computer.
- The canvas is a 500x500 white background where the game takes place.
self.canvas = tk.Canvas(root, width=500, height=500, bg='white')
The bunny moves in response to arrow key presses. Each key press updates the bunny's position on the canvas.
self.root.bind("<Up>", self.move_up)
self.root.bind("<Down>", self.move_down)
self.root.bind("<Left>", self.move_left)
self.root.bind("<Right>", self.move_right)
The move_up
, move_down
, move_left
, and move_right
methods update the bunny's position and check for collisions with the carrot.
The carrot moves in a random direction every second using the move_carrot_randomly
method. The after
method schedules this action to happen repeatedly.
self.root.after(1000, self.move_carrot_randomly)
The random
module in Python is used to introduce randomness into the game. This makes the game more dynamic and fun by ensuring the carrot moves unpredictably. Here's how the random
module, particularly the randint
function, is used in the Bunny Game:
The random.randint(a, b)
function generates a random integer between the values a
and b
, inclusive. For example:
import random
print(random.randint(1, 10)) # Outputs a random integer between 1 and 10
-
Random Initial Carrot Position:
- When the game starts, the carrot is placed at a random position on the canvas using
random.randint
to generate x and y coordinates. - Example from the code:
This ensures the carrot starts somewhere within the canvas but not too close to the edges.
self.carrot_x = random.randint(50, 450) self.carrot_y = random.randint(50, 450)
- When the game starts, the carrot is placed at a random position on the canvas using
-
Carrot Movement in Random Directions:
- The carrot moves randomly every second to avoid the bunny. The movement is determined by adding a random step (either -10, 0, or 10) to its current x and y coordinates:
dx = random.choice([-10, 0, 10]) # Random step in x direction dy = random.choice([-10, 0, 10]) # Random step in y direction
- The random step is applied to both x and y coordinates, causing the carrot to move up, down, left, right, or stay in place.
- The carrot moves randomly every second to avoid the bunny. The movement is determined by adding a random step (either -10, 0, or 10) to its current x and y coordinates:
-
Resetting the Carrot's Position:
- When the bunny catches the carrot,
random.randint
is used again to place the carrot at a new random location:self.carrot_x = random.randint(50, 450) self.carrot_y = random.randint(50, 450)
- When the bunny catches the carrot,
Randomness makes the game more engaging and unpredictable, as the player cannot anticipate where the carrot will move next. It also introduces variety, making each game session unique.
Encourage kids to modify the code and observe the effects:
-
Change the Range:
- What happens if the range of
random.randint
is reduced (e.g.,random.randint(100, 400)
)? - Try adjusting the step size in the random movement.
- What happens if the range of
-
Use Other
random
Functions:- Replace
random.choice([-10, 0, 10])
withrandom.uniform(-10, 10)
for smoother movements.
- Replace
-
Add Random Obstacles:
- Use
random.randint
to generate random positions for obstacles and add them to the canvas.
- Use
Understanding and experimenting with random
will help kids learn how to use randomness in programming to create exciting features in their projects!
When the bunny gets close to the carrot (distance < 20), the check_collision
method moves the carrot to a new random position and updates the score.
if abs(self.bunny_x - self.carrot_x) < 20 and abs(self.bunny_y - self.carrot_y) < 20:
self.update_score()
self.move_carrot()
The score starts at 0 and increases by 1 every time the bunny catches the carrot. It is displayed at the top of the game window.
self.score_label = tk.Label(root, text=f"Score: {self.score}", font=("Arial", 16), bg="white")
- Install Python and ensure you have the
Pillow
library installed (for image handling). - Save the bunny and carrot images to your computer and update the file paths in the code.
- Run the code and use the arrow keys to move the bunny.
- Try to catch the carrot as many times as possible while it moves around.
This game is just the beginning. Here are some ideas for extending and improving the game:
- Add a Timer: Limit the time and see how many carrots you can catch before time runs out.
- Obstacle Course: Add obstacles that the bunny needs to avoid.
- Levels: Increase the carrot's speed as the score goes up to make the game more challenging.
- Sound Effects: Play a sound whenever the bunny catches the carrot.
- Customize Graphics: Replace the bunny and carrot with your favorite characters.
By working on this project, kids will:
- Learn the basics of Python programming.
- Understand how to create a GUI using
Tkinter
. - Explore key coding concepts like event handling, loops, and randomization.
- Practice debugging and modifying code to see immediate results.
This project is all about creativity and learning. Whether you stick with the basics or add cool new features, enjoy the process of turning ideas into reality!