Tag Archives: Learn Coding

Write Computer Games In Python

Ah yes, I remember it well.

“Hammurabi, Hammurabi, I beg to report to you,
In Year 1, 0 people have starved.
101 people came to the city
The population is now 124
We harvested 4.5 bushels per acre
We planted 998 acres of wheat
But rats at 300 bushels of wheat
You now have a surplus of 1443 bushels of wheat

How many acres do yo uwish to feed to the people?
How many acres do you wish to plant with seed?

Oh, and you have died of Cholera!”

Or, this one:


I went to a special high school, in an era when individuals and high schools alike did not have computers, but we did. Since we were a University normal school, we had account and terminal room access to the UNIVAC 1108 computer at the University (see photo above). There were no computer games in those days, so you had to write your own, and store them on tape. Paper tape, not magnetic tape (the magnetic tape was reserved for use by actual University students and faculty, for the most part).

So we wrote and fiddled with programs in BASIC, the intro language of the day. BASIC was a great language, but is widely regarded today as a horrible language. Truth is, it was easy to program in, had reliable interpreters, and eventually, advanced versions became fully OOPish and lost silly things like line numbers.

Today’s equivilant of BASIC, for the simple reason that it is one of the programming languages people often start on, but similar for other reasons as well, is Python.

Python was invented by Benevolent Dictator For Life Guido van Rossum. Guido was a big fan of Monty Python back in 1989 when he invented an interpreter to run a script language that didn’t exist yet but was knocking around in his head. A script, in computer world, is a series of commands in a file that can be run like it was a computer program, but where the code is not turned into an executable file to run independently, but rather, run by an “interpreter” which carries out the commands ad hoc each time the script is called. That is how BASIC originally ran, and that is how Python works.

Et magis est, ut in fabula.

Python has evolved over the years to become one of a small number of languages that can do pretty much anything. The language itself is fairly simply yet powerful and flexible. In writing Python programs (the language is too fancy to use the term “script” comfortably, though that is technically what the programs are) one has access to a large number of libraries of pre-existing code. These libraries are extensive, intensive, flexible, and powerful. The programs run very efficiently.

What software that you know about is written in Python? Well, DrobBox is written in Python, which is not surprising, since Benevelont Dictator van Rossum works for Dropbox (or did anyway, not sure if he is still there). Google uses Python for pretty much everything, so when you “google” something, you are using the Linux operating system running a Python script accessing data created and maintained by Python scrips. Also, Python was underwent much of its development with support from Google.

Many of the GNU Linux utilities and software in use today that is not from the original cadre of mainly C-xx (a different family of languages) applications are written in Python. So, again, the basic computer services we rely on, such as Google, ultimately use Python in many different ways.

And, Python has become one of very few widely used scientific software tools. If you are going to grow up and become a scientist, you will want Python skills.

And this is where we come to the new 4th edition, Invent Your Own Computer Games with Python.

This is an excellent way to learn Python, if you are a kid or not. Little kids can learn with their adult guide, and older kids will eat this book up in an afternoon or two.

The Table of Contents will give you an idea of what it covers:

First, on how to set up and use Python:

Chapter 1: The Interactive Shell
Chapter 2: Writing Programs

Then some very simple games:

Chapter 3: Guess the Number
Chapter 4: Jokes
Chapter 5: Dragon Realm

Then how to use a key feature to help you more easily write complex programs:

Chapter 6: Using the Debugger

Then a pretty complex program (but still very doable):

Chapter 7: Designing Hangman with Flowcharts
Chapter 8: Writing the Hangman Code
Chapter 9: Extending Hangman

Then many more programs of various levels of difficulty:

Chapter 10: Tic-Tac-Toe
Chapter 11: Bagels
Chapter 12: Cartesian Coordinates
Chapter 13: Sonar Treasure Hunt
Chapter 14: Caesar Cipher
Chapter 15: Reversi
Chapter 16: AI Simulation

Then some advanced programming and tools, and more games:

Chapter 17: Using Pygame and Graphics
Chapter 18: Animating Graphics
Chapter 19: Collision Detection and Input
Chapter 20: Sounds and Images
Chapter 21: Dodger

Many of the programs are designed to run on the command line, but still use cool (in a retro sort of way) graphics, but the book gets you started on using modern day window-deployed graphics.

Al Sweigart is a software developer who teaches programming to kids and adults. He is the author of Automate the Boring Stuff with Python: Practical Programming for Total Beginners, a book I’ve not yet laid eyes on, and Scratch Programming Playground: Learn to Program by Making Cool Games, which I review here. By the way, if you are looking for an intro programming guide for kids, consider scratch as well. Scratch is not at present a powerful programming tool kids will use when they grow up, but it teaches programming skills and it is fun. Having said that, I predict that a language like Scratch, which has an ancestry as old as any existing programming langauge yet is extremely modern and forward looking, may end up being a more widely used tools, allowing regular people to program the Internet of Things. Also, a kid heading for Robotics will probably be able, in the very near future, to use Scratch in that area as well.

Go to the No Starch Press web site to access the code and other resources, and to find a list of errors and updates. In a regular book about something, say, Abraham Lincoln, a typo is not a big deal. In a computer programming book, a typo can be a big deal.

For example, reading “In 1860, Lincoln secured the Republican Party presidential nomination as a moderate from a wing state,” instead of a “Swing state” is not going to cause a disaster. But in 1962, the Mariner spacecraft had to be destroyed moments after takeoff because a “-” was written instead of a ““.

Anyway, great book. Enjoy it!