In the first week of August, the Chinese government branded online games electronic drugs. Considering that some young Chinese players have been known to spend more than eight hours at a stretch gaming - neglecting studies and family, and foregoing any attempt at a social life, it would be hard to say the government officials are totally wrong. In many ways, gaming addiction mirrors drug addiction and the effects can be just as devastating. So what does that mean for your Genshin Impact adventures? Probably nothing, On the other hand, in keeping with China's policy of harm reduction, development might be slowed down - new weapons and battles might be fewer and farther between, but maybe only for gamers in China. Genshin Impact is written in C++, the language for programming robots and operating a whole slew of other systems. We have to wonder, though... how hard would it be to write a game's code entirely in Python? It turns out to be not difficult at all. Read on to find out what Superprof discovered while playing around with Python.
What Is a Computer Game?
The Cross-Platform Question
Do you have an iPhone or is yours powered by Android? Do you like to game on a PC or is Mac more your style? Or maybe you're deeply concerned about privacy and security so you've opted for a Linux operating system. These operating systems and others are known as platforms. They give coders a major headache. There's a Google Play store for Android apps, the Apple store to download iOS apps and Microsoft has their app store to download apps for their PCs. Visit any of those 'stores' and you'll find all the same apps: Facebook, Snapchat, and many of the same games. That's because they are the same, but for their code. Game developers and coders in general must always be mindful of which platform(s) they want their applications to run on. If you're designing exclusively for Apple, you can count on losing the Android and Windows markets and, if you only design for Windows... as they have the lowest smartphone market share, you'll lose out on virtually the entire mobile device market. Older programming languages are not adaptable across platforms; you must code separately for each platform you want your game to run on. Newer, more adaptable languages, Python among them, work well across many different platforms, making it one of the more popular languages to write in. Granted, Python is not a new language; it's been around for over 30 years but it has recently come into its own, thanks in part to its versatility. Indeed, Python is now touted as one of the best languages for web development, alongside Vue.js and Angular. Find good python classes here on Superprof.
How to Write Computer Games in Python
Keep in mind that, while Python is easy to work with and has many applications, performance is not one of its top characteristics so, if your game is heavy on graphics and action, you will likely run into trouble if you code only in Python. However, if you're writing code for a 2D, low-graphics game like Noughts and Crosses or Snake and Apple, Python will more than fit the bill. Pygame is a library of Python modules written and compiled specifically to create computer games. It includes sound libraries and computer graphics, all ready to be dropped into any Python game program. How does it work?
- get familiar with Tkinter, Python's standard GUI
- learn to draw geometric shapes with its canvas widget
- use the bind widget to identify what action mouse clicks should take
- outline your game by drawing a game flow diagram
- code your game's initialization functions
- code your game's drawing functions
- code your game's logical functions
The logical functions actually run the game. They keep track of user inputs, the state/stage the game is in and they advance the game's progress. They keep track of scores, determine if the game is over and deliver the verdict - win or lose. Those pesky 'illegal move' messages also originate from the game's logical functions. This, of course, is just a brief outline of how to code a game using Pygames. Naturally, you'll want to embellish a bit by using different colours and you can even add effects, all of which can be found in Python's code library. Creating easy games is just one way you can use Python; discover some of the others...
Drawbacks to Writing Games in Python
Probably the biggest downside to writing game code in Python is low performance. Comparing some of the most popular games. like the aforementioned Genshin Impact with its lush graphics and fast action, Python simply can't measure up - either in speed or in the visuals. As versatile as Python is, it is not suited to the mobile environment. Few mobile devices use Python because its strengths lie in server-side development rather than running power-driven applications. Smartphones and tablets being commonly used for gaming means that, due to its nature and structure, Python games won't get a foothold. Finally, developing a game in Python can be a bit tedious. For all that its libraries are extensive, well-labelled and easy to use, the process of Python game creation is still quite convoluted. Most developers contend that the effort is not worth the simplistic yield. Still, a few Python-based games stand out, Frets of Fire and Save the Date among them. Note that Save the Date was 2013's Indicade's finalist entry. Indicade is like the Sundance Film Festival, only for computer games. Where Python really comes into its own is data science...
The platform that connects tutors and students