Hi everyone, from this post we will start learning PyGame. And programming games are real fun, that is why I am here with this PyGame Tutorial for Beginners.
If you are interested in learning game development with python, and you have absolutely no idea about it, then this is the right place for you. I am assuming the readers here are no knowledge of pygame, but you must know the basics of python Ok.
So, let’s start with the very obvious question.
Contents
What is PyGame?
PyGame is a set of python modules, and these modules are specially designed for writing or coding games. For more information about PyGame you can visit the Official PyGame Website.
What kind of games we can make with PyGame?
You might be thinking what kind of games you can make with PyGame. And honestly speaking it is good for 2D games. And if you are new to game programming then PyGame is good to get started.
You can check this link to see the games that are built with PyGame.
Who can follow this PyGame Tutorial for Beginners?
Any one with the basic knowledge of python can follow this tutorial. You do not need to be an expert or advanced python geek, just a basic understanding of python is more than enough to follow this.
Tools Required
For getting started you just need a simple code editor, notepad++, Visual Studio Code anything is more than enough. But you can also go with some powerful IDE, for example my favourite, JetBrains PyCharm. The community edition is also very very powerful and it is absolutely free.
Installation
Now we have had enough discussion and now we should start the learning part. So we will start from the Installation of PyGame. As this series is called PyGame Tutorial for Beginners, we will cover the installation as well ;).
But before installing PyGame you also need Python and yes it is something obvious.
Installing Python
- Go to Python’s Website. And download the Python according to your system. In my case I am using Python 3.7, 64bit windows installer.
- So you just need to download the executable installer (in case of windows) and then you can install it. The installation process is same as you install any other application in your windows computer.
Setting Up Environment Variables
- Once you have finished installing Python, you also need to setup environment variables. The default installation directory is
C:\Users\<your user name>\AppData\Local\Programs\Python\Python37
- In the search menu, write environment
- Select Edit the system environment variables. It will open a new window, from here in the bottom you will see a button named Environment Variables…, click on it and it will open the following window.
- In the bottom box (System variables) click on New…
- Here you need to write “Path” in Variable name and in Variable value I have the path for the Python installation directory and the Scripts directory which is inside the Python Installation directory, separated by a semi colon (;).
- Now apply the changes and path are set.
- To confirm it you can try running python command in command prompt.
Installing PyGame
- I am using a 64bit, Python but the problem is we do not have an official 64bit PyGame. But we have UnOfficial 64bit PyGame so this is what we will be using.
- Click on this link, here you will find the unofficial 64bit PyGame Windows Binary.
- I am using the highlighted one.
- Now to install PyGame open command prompt and navigate to the directory where you have downloaded the .whl file in command prompt. (In my case it is in my desktop).
- Now, first you need to install wheel, for this run the following command in command prompt.
1 2 3 |
pip install wheel |
- And it will install wheel.
- Now we will run the following command.
1 2 3 |
pip install pygame-1.9.3-cp37-cp37m-win_amd64.whl |
- And also make sure the PyGame WHL file exist in the current directory.
The IDE or Code Editor
Now, this is completely your personal choice, but I think I should tell you what I am using. Python also have IDLE, its own inbuilt editor but I will be using Visual Studio Code, it is again an absolutely free tool.
But yes, it is not necessary that you should also use Visual Studio Code, you can use whatever editor you want to use.
Creating our Game Window
Now let’s do a little coding, and create our very first game window. Excited?. Yes. So let’s begin.
- Create a file named game.py anywhere in your computer. (But it is a good practice to organize your files by keeping them in separate folder, so I would tell that create a folder for your game project).
- Inside the file game.py write the following code.
1 2 3 4 5 6 7 8 9 10 11 |
import pygame pygame.init() game = pygame.display.set_mode((600,400)) pygame.display.set_caption("My First Game") input("Enter anything...") |
- import pygame: The pygame module we need to import.
- pygame.init(): This function initializes all pygame modules that are imported.
- pygame.display.set_mode(): In this line we are setting our game window’s width and height.
- pygame.display.set_caption(): This method taking a string, and it is the title of our game’s window.
- input(): Using this function just to hold the window, else when you will run it window will displayed for a fraction of second and then it will close.
Now, you can run your code and to do this, again you need to go to the directory in command prompt where your game.py file is located. If you are using Visual Studio Code then you can use the integrated terminal of Visual Studio Code (View -> Integrated terminal).
For executing the code write the following command.
1 2 3 |
python game.py |
And you will see the following output.
As you can see we have our Game Window. So that is all for this tutorial friends. If you found it helpful then please share this PyGame Tutorial for Beginners with your friend who want to learn about Game Development. And more tutorial will come soon stay tuned.
Lastly don’t hesitate in commenting below if you have any question. Thank You 🙂
How to use pygame in an Android device? Because I have no computer. I have downloaded Pydroid3 and Qpython on my device.