Create a Flappy Bird clone with Python P1
Posted in tutorials python -This is one part of a multi-part tutorial. To see other posts in the same series, please click below:
Part 4 - Make a “flapping” flappy bird
As you may remember, not long ago, I wrote two articles with some advice for those who want to learn some programming. Well, actually that was intended to be a three-part article, and one advice I was about to put on the final part was “Aim to do something from the first day”. What I meant by that is that instead of trying to learn everything there is about a language, you should have some goal in mind about what you would like to make, and then you break down the goal to smaller goals, and then from smaller goals to some particular skills that you can master one by one, to make the ultimate goal you have in the first place. Throughout the process you will learn a lot and, at the same time, become confident with your skills.
However, as I wrote that out yesterday, I realized that the things I was about to say would sound really weird and vague without any example, and for that reason, I decided that I will make an example of how I would solve a problem, so that you can follow along and learn not only the technical skills that involved, but also how you should deal with new topics, how to exploit what you’ve already known to learn what you have not, how to search for information, etc.
Mind you, though, that I do everything in Linux. If you’re using MacOS, things would be more or less the same, but if you’re on Windows, you may have to do some tweaks. I don’t think the difference will be too big, though, as most of things we do should be about Python, not OS.
The problem we want to tackle today is to make a game, and the game I choose is the famous Flappy Bird. The reason I chose this particular one is that I used to make it in Android some years ago, so I know how the game works, which allows me to focus on making it in Python and not to care about exploring the game’s particular matters.
Choosing a topic that you’re familiar with is one of the tricks that you should do when you start learning a new framework/language. As said it reduces the scope of new stuff you need to acquire to just the new framework/language.
Okay. Let’s start!
Setup python3 and pip
First thing first, you should have python3
installed on your system. If you’re on Linux or MacOS, you’ve probably had it, but just to make sure, you should open your terminal and run this command
python -V
If you get something like Python 3.8.2
, then you’re good to go, otherwise if you get Python 2.x.x
or Command not found
error, then try this:
python3 -V
If you still don’t get what you want, then you don’t have python3 installed yet. In that case type “How to install python3 in …” (fill in your OS type) to Google and pick any page you like. This one is a good instruction if you don’t want to search by yourself.
-V
, by the way, is a shortcut for “version” commonly used in several packages.
Next, you would want to check if you have pip
installed. Pip is the package manager for Python
, it allows you to install, update and remove the python libraries easily, so it’s almost compulsory to have pip
when you develop things in Python
. Similar to Python
, you can check if pip
exists by typing
pip -V
If you get an error, or if the version message says it belongs to python 2.x
, then you need to install pip
. For quick installation, run the following command
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python3 get-pip.py && rm get-pip.py
Explain: curl
downloads a file called get-pip.py
to your system, then you need to execute that file using your python 3 command (which is normally either python
or python3
, I will always use python3
from now on by the way), and pip will be installed for python3. The rm
part is to remove the get-pip.py
file from your system after you’ve done with it.
Just my opinion here: since python 2 was stopped in Jan 1st, 2020, if you are learning python in 2020 onward, you will not likely need python2. Just remove it if you can so that your environment gets less confusing.
Setup virtual environment
Virtual Environment, or virtualenv
is a popular way for managing your python packages in a per-project basis. Without it you’d have to install your python packages globally, so if two projects need the same package, but in different versions, then at least one of them won’t work well in your system (either that, or you have to remove and install to switch version all the time). virtualenv
solves that problem by putting everything you need to run a project into the project directory, so the system will load python and the packages from there, instead of from your global system. As you will later on find out, managing packages this way also provides a rather convenient way to copy the project to another system, by specify exactly the packages needed and their versions.
For those aforementioned benefits, it’s my recommendation that you make using virtualenvs a habit in your python development.
Let’s start with installing virtualenv
package globally:
pip install virtualenv --user
the --user
flag is added so that the installation does not interfere with system’s package.
Here there’s something that needs to be clarified: though we said that without
virtualenv
, the packages will have to be installed globally, it’s not exactly system-wide, or at least it doesn’t have to be so. With the--user
flag, we makes sure that globally implies that the packages will be common among projects executed by the same user (your user), but not among different users. If you’re not satisfied with this explanation, then just go with it for now. We’ll have to discuss deeper about UNIX-like systems to really understand this.
After the installation, you should be able to run
virtualenv --version
In case you get “Command not found” error for virtualenv
, then it’s probably not in your PATH
yet. In that case, run this to check where the package was installed:
python3 -m site --user-base
You’ll most likely get something like /home/<your-name>/.local
(/home/<your-name>/
can also be referenced by $HOME
). Then it can be inferred that your virtualenv package was installed in $HOME/.local/bin
. Verify it by running
$HOME/.local/bin/virtualenv --version
You can consider adding your user-base
to your PATH
, so that next time you won’t have to type the absolute path to your virtualenv when you want to run it.
Now is the time we can start our project. First, create a new directory and cd
into it:
mkdir flappybird && cd flappybird
Create a new virtualenv called venv
. Technically you can call the virtual env anything you want, but venv
is a common name for it, so using that name lets other people understand what’s in it easily.
NOTE: Even if you think you only code for yourself, trying to make everything explicitly understandable is a good practice in programming. Some years later you will forget the details, and by designing in a comprehensible style, you give your future self an easy time understanding what you write today.
virtualenv venv -p $(which python3)
The -p $(which python3)
part specifies that we want the new virtualenv to use python3. It won’t be needed if you only have one python in your system.
Now if you run ls
, you will see that there is a new directory called venv
in there. To activate it, you can run:
source venv/bin/activate
If you do everything correctly, you should see the name of the virtualenv (venv)
displaying in your current command line.
(venv) dieu@thinkpad:~/Github/flappybird$
Since we configured
virtualenv
to use python3, from now on you can runpython
instead ofpython3
(if you had to usepython3
previously).
From here, when you run pip install
, the newly installed package will not go into your system-base or user-base packages, but to the virtualenv of this particular project. Note that that behavior applied to that particular terminal window only (the one with (venv)
in the current line). If you want another terminal to belong to the virtual env, just run source
command for that terminal window.
When you want to exit the virtualenv, type deactivate
Above we have covered in details the steps that are recommended to getting started with python development. That might be a boring part, but that’s necessary preparation for a good development practice; next time, we’ll start with the fun part, where we actually code something up and run. See you then.
To see other posts in the same series, please click below: