The video games industry has been booming with unprecedented growth in revenue for the past 5 years or so. Now is a great time to consider a career in game development, since the demand for new and unique games is high.
Studios are looking for fresh talent, and a lot of new hires will be game programmers who develop their upcoming games. As a programmer, you’ll be expected to code in multiple languages.
One of those is Python, which can be described as an “interpreted high-level programming language”. It is object-oriented, has built-in high-level data structures, and uses dynamic typing in combination with dynamic binding.
Is python good for game development? Python is an excellent choice for rapid prototyping of games. But it has limits with performance. Therefore for more resource-intensive games, you should consider the industry standard which is C# with Unity or C++ with Unreal.
Some popular games like EVE Online and Pirates of the Caribbean were created using Python. There are libraries and frameworks such as Pygame, Pyglet, and PyOgre to help you create your own games in Python.
But most people getting into game development are looking for a robust platform that offers plenty of scalabilities, all the way from hobby projects to big studio titles. Unity and Unreal offer exactly that, and you need to know C# or C++ to use them.
Entire development platforms with every tool you need including editors, animation, render engines, export functionality, etc. Python is sort of stuck in the middle ground in this regard. Yeah, Pygame is okay. But that’s just it, and okay isn’t the best.
Most people who are looking to create a serious career in the gaming industry will choose C# or C++ instead, using Python for hobby projects and understanding the concepts of coding.
Game Development In Python With Pygame
If you want to get into game development with Python, the Pygame framework has pretty much everything you need to create a simple 2D game. Pygame is a low-level graphics library, built as a wrapper around SDL (which is an open-source 2D graphics library).
Pygame also has wrappers for sound and music. DUGA is an example of a retro FPS game created entirely with PyGame, inspired by early shooters such as DOOM and Wolfenstein 3D. Of course, you wouldn’t want to create a game that you actually plan to sell entirely in PyGame.
You are limited to creating stuff like visual novels and physics-based 2D platformers. A lot of the modules in PyGame are old and inefficient, so you’re better off using something like Unity or Unreal if you want to make a game that can actually be put on sale on Steam or the App Store.
But if you’re just making a hobby project or prototyping ideas for a game, PyGame will get the job done (provided you know what you’re doing). New to game programming? PyGame is a nice place to get started since it is really easy to learn.
First, you will need a code editor. Visual Studio Code should be more than enough, just make sure you install the python plugin. Or, you can choose an IDE like JetBrains PyCharm. And you’ll obviously need Python, so go to their official website and download the latest version.
Run the executable, and once it is installed, you’ll need to load up Pygame. The easiest way to get Pygame is to use the pip tool, which is a package installer. Use the –user flag to let it know that you want Pygame installed into the home directory, and not globally.
python3 -m pip install -U pygame –user
To check if it is working, run one of the included examples
python3 -m pygame.examples.aliens
If everything checks out, you’re good to go.
Features of Pygame:
- Doesn’t require OpenGL
- Certain pygame functions release the global interpreter lock, adding support for multi-core CPUs
- C code is several times faster than python code, so some of the core functions within Pygame use optimized C (other core functions use assembly code)
- Portable design, it supports pretty much every OS out there
- Simple and easy to use
- You don’t need a GUI to use pygame
- Fewer lines of code
Pygame is good for basic 2D games, but not much more. Which is why you should check out Panda3D. It is a game engine that lets you create 3D projects with Python.
Panda3D includes graphics, audio, I/O, collision detection, and is free software distributed under a revised BSD license. You can code your games in Python, or directly access the engine via C++ code. The Panda3D core is written in C++, which boosts speed.
A powerful binding layer exposes the majority of API through Python-based interfaces. You’ll find that Panda3D supports most industry standard physics engines such as Bullet, PhysX, and ODE. It also has powerful profiling and debugging tools and supports plugins which let you deploy your game to browsers.
Python vs Java and C++ In Game Development
Generally speaking, Python is used in fewer places within games compared to C++. And for good reasons too. Anytime you want to write speed-dependent code or code that is “close to the metal” you use C or C++ since it is easier to optimize for hardware.
Java while being a step down from C++ in terms of speed, is still faster than Python. For instance, Java supports multithreading, unlike Python which has the dreaded GIL (global interpreter lock) that prevents it from using more than one CPU core at a time.
This means on a 4 core CPU, Java or C++ can potentially run up to 4x faster just on the basis of using all available processor resources. But even if your program uses only one CPU core, Python will be slower.
Python is a dynamically typed language, and why that affects its execution speed compared to statically typed languages such as Java and C++. Sockets in Java come in handy for creating multiplayer games, and Java is often used in Android game development.
There are ways to get around Pythons inherently sluggish nature, by using wrappers and bindings. Plugin 3rd party libraries written in faster languages like C, and your Python program will speed up immensely.
You can also use faster implementations of Python, like PyPy which uses JIT compilation that is 4x faster than CPython. PyPy works by taking the source code and converting it into RPython, which is a statically typed restricted subset of Python.
RPython is easier to compile into a more efficient code, and PyPy translates it into a form of bytecode with the help of an interpreter written in C.
Why Python Is Used For Rapid Game Prototyping
Because writing code in Python is just faster. We talked about how pure Python code takes longer to execute, but the one thing that most people miss out on is development time.
How long it takes you to write a program is just as important, if not more, than the execution time of your code. Truth is, you can solve most simple problems with fewer lines of code in Python compared to C++ or Java.
This gives you more time to place your focus on other aspects of your game such as level design or art. Game scripting and AI is really easy to do with Python, even though graphical stuff is better done with C++.
You come up with a game concept, hammer out a quick prototype with Python and get to see if your idea is actually viable. This saves money since your programmers have to work fewer hours.
Even if the game concept is discarded, you don’t lose a whole lot of time or money at the end. Besides, you don’t need a super experienced programmer to write up some Python code since the language is so easy to learn.
Even your game designer or one of the artists can do some basic scripting with it. Python has so many libraries and plugins that help you create a barebones version of your game from scratch in almost no time.
What Advantage Does Python Offer Over Other Languages?
One of Python’s major advantages over other languages is it’s easy to understand syntax, which places an emphasis on readability. This makes it a great choice for people who wish to learn to code but are turned off by the complexity of C++ or Java.
Python also focuses on code reusability and modularity. As a programmer, you might be thinking “I can already do this stuff in other languages, so why to use Python?”. Well, Python lets you solve problems with fewer lines of code.
Memory management on Python is easier since it is a high-level language, and its cross-platform nature allows you to develop games for Windows, Mac, Linux, Android, and iOS ( basically every device out there).
Python also has a massive ecosystem consisting of frameworks, libraries, packages, etc. so chances are someone out there has already created a tool that you can use to simplify your project. And if you ever get stuck, there is a very helpful community of Python enthusiasts to help you out.
To summarize, Python is a general-purpose multi-platform language with a clean and elegant syntax. Its simplicity is what makes it the most popular, fastest-growing language that programmers are learning today. It is used in everything from game development, to machine learning and automation.
Why Python Has Speed Limitations
Sometimes, developers will write their own engines for a game and that is very rarely done with Python since it executes slower than compiled languages.
This is partly due to the interpreted nature of Python. Source code is converted into bytecode that is then compiled into machine language at runtime line by line using an interpreter instead of being precompiled into machine language like with C++.
Another major reason Python code executes slower is because of dynamic typing. Compare this to a language like Java where everything is statically typed, which means variable types are explicitly declared beforehand. Here’s a small example of the difference in execution time between Python and C++, using a simple loop.
Python is used to do very specific things, such as AI or certain parts of the game logic. Rarely will you see a big-budget game that functions on Python alone. Graphics engines are done with C or C++. Basically, Python is sort of a “glue” language when it comes to game development. Mainly in places that involve simple but highly repetitive tasks.
At the end of the day, it comes down to two things- how experienced you are with a language, and what your use case is. If you’re doing graphics, go with C++.
If you’re doing platform-independent simple stuff, Java is a good choice. Low-level device stuff is best done with C. Python is great for things like scripting in game engines, AI, rapid prototyping, etc.
It runs slower, yes. But you make up for that with the speeding up of your development process. A python prototype can be done in 1 day where the same prototype would take 4 days with C++. Your own level of experience and skill matters too.
Inefficient C++ code is going to be slower than the same code written in Python by a dev who knows what he is doing. Python is the language used by both beginners and pros.
It is a general-purpose solution to a variety of problems, and the vast array of 3rd party plugins help you fix its inherent weaknesses. If you’re learning to code with gaming, Python is a great choice due to its simplicity which lets you solve problems faster.