Why Python and Pygame are a great pair for beginning programmers

We look at three reasons Pygame is a good choice for learning to program.
964 readers like this.
3 cool machine learning projects using TensorFlow and the Raspberry Pi

Opensource.com

Last month, Scott Nesbitt wrote about Mozilla awarding $500K to support open source projects. Phaser, a HTML/JavaScript game platform, was awarded $50,000. I’ve been teaching Phaser to my pre-teen daughter for a year, and it's one of the best and easiest HTML game development platforms to learn. Pygame, however, may be a better choice for beginners. Here's why.

1. One long block of code

Pygame is based on Python, the most popular language for introductory computer courses. Python is great for writing out ideas in one long block of code. Kids start off with a single file and with a single block of code. Before they can get to functions or classes, they start with code that will soon resemble spaghetti. It’s like finger-painting, as they throw thoughts onto the page.

This approach to learning works. Kids will naturally start to break things into functions and classes as their code gets more difficult to manage. By learning the syntax of a language like Python prior to learning about functions, the student will gain basic programming knowledge before using global and local scope.

Most HTML games separate the structure, style, and programming logic into HTML, CSS, and JavaScript to some degree and require knowledge of CSS and HTML. While the separation is better in the long term, it can be a barrier for beginners. Once kids realize that they can quickly build web pages with HTML and CSS, they may get distracted by the visual excitement of colors, fonts, and graphics. Even those who stay focused on JavaScript coding will still need to learn the basic document structure that the JavaScript code sits in.

2. Global variables are more obvious

Both Python and JavaScript use dynamically typed variables, meaning that a variable becomes a string, an integer, or float when it’s assigned; however, making mistakes is easier in JavaScript. Similar to typed variables, both JavaScript and Python have global and local variable scopes. In Python, global variables inside of a function are identified with the global keyword.

Let’s look at the basic Making your first Phaser game tutorial, by Alvin Ourrad and Richard Davey, to understand the challenge of using Phaser to teach programming to beginners. In JavaScript, global variables—variables that can be accessed anywhere in the program—are difficult to keep track of and often are the source of bugs that are challenging to solve. Richard and Alvin are expert programmers and use global variables intentionally to keep things concise.


var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });

function preload() {

    game.load.image('sky', 'assets/sky.png');

}

var player;
var platforms;

function create() {
    game.physics.startSystem(Phaser.Physics.ARCADE);
…

In their Phaser programming book Interphase, Richard Davey and Ilija Melentijevic explain that global variables are commonly used in many Phaser projects because they make it easier to get things done quickly.

“If you’ve ever worked on a game of any significant size then this approach is probably already making you cringe slightly... So why do we do it? The reason is simply because it’s the most concise and least complicated way to demonstrate what Phaser can do.”

Although structuring a Phaser application to use local variables and split things up nicely into separation of concerns is possible, that’s tough for kids to understand when they’re first learning to program.

If you’re set on teaching your kids to code with JavaScript, or if they already know how to code in another language like Python, a good Phaser course is The Complete Mobile Game Development Course, by Pablo Farias Navarro. Although the title focuses on mobile games, the actual course focuses on JavaScript and Phaser. The JavaScript and Phaser apps are moved to a mobile phone with PhoneGap.

3. Pygame comes with less assembly required

Thanks to Python Wheels, Pygame is now super easy to install. You can also install it on Fedora/Red Hat with the yum package manager:

sudo yum install python3-pygame

See the official Pygame installation documentation for more information.

Although Phaser itself is even easier to install, it does require more knowledge to use. As mentioned previously, the student will need to assemble their JavaScript code within an HTML document with some CSS. In addition to the three languages—HTML, CSS, and JavaScript—Phaser also requires the use of Firefox or Chrome development tools and an editor. The most common editors for JavaScript are Sublime, Atom, VS Code (probably in that order).

Phaser applications will not run if you open the HTML file in a browser directly, due to same-origin policy. You must run a web server and access the files by connecting to the web server. Fortunately, you don’t need to run Apache on your local computer; you can run something lightweight like httpster for most projects.

Advantages of Phaser and JavaScript

With all the challenges of JavaScript and Phaser, why am I teaching them? Honestly, I held off for a long time. I worried about students learning variable hoisting and scope. I developed my own curriculum based on Pygame and Python, then I developed one based on Phaser. Eventually, I decided to use Pablo’s pre-made curriculum as a starting point. 

There are really two reasons that I moved to JavaScript. First, JavaScript has emerged as a serious language used in serious applications. In addition to web applications, it’s used for mobile and server applications. JavaScript is everywhere, and it’s used widely in applications kids see every day. If their friends code in JavaScript, they'll likely want to as well. As I saw the momentum behind JavaScript, I looked into alternatives that could compile into JavaScript, primarily Dart and TypeScript. I didn’t mind the extra conversion step, but I still looked at JavaScript.

In the end, I chose to use Phaser and JavaScript because I realized that the problems could be solved with JavaScript and a bit of work. High-quality debugging tools and the work of some exceptionally smart people have made JavaScript a language that is both accessible and useful for teaching kids to code.

Final word: Python vs. JavaScript

When people ask me what language to start their kids with, I immediately suggest Python and Pygame. There are tons of great curriculum options, many of which are free. I used "Making Games with Python & Pygame" by Al Sweigart with my son. I also used Think Python: How to Think Like a Computer Scientist by Allen B. Downey. You can get Pygame on your Android phone with RAPT Pygame by Tom Rothamel.

Despite my recommendation, I always suspect that kids soon move to JavaScript. And that’s okay—JavaScript is a mature language with great tools. They’ll have fun with JavaScript and learn a lot. But after years of helping my daughter’s older brother create cool games in Python, I’ll always have an emotional attachment to Python and Pygame.

User profile image.
First elected president and co-founder of Tokyo Linux Users Group. Co-author of "Linux Japanese Environment" book published by O'Reilly Japan. Part of core team that established first ISP in Asia. Former VP of product management and product marketing for major Linux company. Partner at Oppkey, developer relations consulting firm in Silicon Valley.

4 Comments

Great article

Thanks for the article! I wasn’t aware of these teaching tools and they look like they’ll come in handy!

(By the way, your daughter’s older brother is your son.)

Thanks for this great article

A lighthouse for a close project.
Many thanks

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.