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:

screen-shot-2017-01-14-at-3-04-24-pm
Remember?

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!

More like this

... and so on and so on people argue. But they are both right, it is a snake, and it is a programming language. I want to talk about the programming language now. We'll deal with the snake another time. (and boy, do I have snake stories....) Python is an interpreted computer language, also…
Over in my post accepting my victory as the biggest geek on ScienceBlogs, an interesting discussion about beginners learning to program got started in the comments. It was triggered by someone mentioning David Brin's article in Salon about how terrible it is that computers no longer come with basic…
My first computer language was PL/1, but soon after I learned, among other languages, Basic, and I really liked Basic and I still do. Basic is linear, and I think in linear constructs when I do any kind of computer program. This is probably, in part, because user interfaces are the last thing I…
Your objective is to learn Python programming. Everybody has to learn Python. You are looking for a book that will make that easier for you. One possibility, one that I'll recommend for most people in this situation, is Python Crash Course: A Hands-On, Project-Based Introduction to Programming…

Back in the mid-70s I was in H.S., but a few of us used to go over to the local college (UW-Superior) and sneak onto their computer system to play Star Trek.

I don't even know what they had for a mainframe, but we didn't have a monitor, all output was printed on the old greenbar paper. A couple years later I was at MIT when the original text adventure was being written. Unfortunately I was oblivious to that going on; wish I'd been involved in it.

By Kevin O'Neill (not verified) on 14 Jan 2017 #permalink

"The Mariner spacecraft" was actually Mariner 1, the first of a line of especially successful spacecraft that conducted our early planetary missions.

JPL had built them as "twins" (just in case), and that turned out to be a good idea, since the first one had to be destroyed about 5 minutes after liftoff.

The second one had the problems corrected, and became the first spacecraft to fly by another planet.

In light of that (and probably to paper-over the initial embarrassing failure), Mariner 2 was quickly rechristened "Mariner Venus".

Three years later, "Mariner Mars" would eclipse that mission...

By Brainstorms (not verified) on 15 Jan 2017 #permalink

My first encounter with the lunar lander game was on a Digital PDP-8 in my father's lab in the early 1970's.

After keying in the boot loader from toggle switches on the front panel, he loaded the program from a roll of paper tape.

The game was then played from the noisy teletype sitting next to the rack holding the computer, with output on the green bar fan-fold paper.

Those were the days... Primitive and crude, but it must have inspired me anyway.

By Brainstorms (not verified) on 15 Jan 2017 #permalink

Much of Linux's "userland" software is a combo of shell scripts, Perl, and Python, as the interpreters for these three are pretty much installed by default on most distros.

Another typical scripting language that's not as commonly used is Tcl (formerly "tool control language"). Some distros install it by default, and most of the rest will easily install it from their package repositories with a simple command. It dates from 1987.

Perl also dates from 1987, having been developed by a guy named Larry Wall, who happened to be working at JPL at the time...

And then there's my favorite: Lua, named for the moon, in the Portuguese language of Brazil, where it was born in 1993. (The "Sol" companion language didn't make it.)

Lua is a great scripting language, with many innovative and intriguing features. While it's famous for its ability to interface with C code, and as an embedded scripting language for things like video games, it's a great language to use on its own.

By Brainstorms (not verified) on 15 Jan 2017 #permalink

I was still working on PDP 11-34s in 1997, they were still going 2002.

This, of course, was in government.

After all, buying new computers to do a job already being done by antequated kit would be a waste of taxpayer money...

And people STILL insist that governments are wastrels of "their" money...

expect is a tcl expansion that you can use to fake up user input.

Quite useful when it comes to testing secure links that require a password (again, government, so we weren't allowed to have a .rc file containing the password to let scripting work).

It was a downer when SSH started noticing a fake tty and refusing to accept the password fed it by expect.

It's really useful for automated testing on UNIX, since you can pretend a genuine user case is re-enacted, all you need is a record of the wire conversation and you can re-play that scenario to check that the bug hasn't resurfaced.

It's a HUGE pain to write, though, because it isn't easy syntax.

Python, though, confirms one apparently invariant truth of humans. EVERY time we find out how something was a bad idea and we stop doing it, someone, somewhere, some time later, will have the "amazing" idea to do it again.

I would have thought that makefiles would have proven once and for all that whitespace formatting being of syntatic use was a REALLY DUMB IDEA.

But, no, the whitespace of Makefiles meant people go off and write a replacement that isn't so goddamned stupid as to think that this must be a new command because there's a damn tab there, and after people have moved over to it, some complete dumbass then thinks "Hey, we want to make sure that we have good, clear formatting,indenting loops and the like. How about we USE that formatting style as PART OF THE LANGUAGE, therefore NOBODY will be able to poorly format our code and ugly unreadable formatting of code will be a thing of the past!".

Morons.

And Conservatives STILL insist that governments are wastrels of “their” money…

FTFY

By Brainstorms (not verified) on 15 Jan 2017 #permalink

There are reasons why I've never bothered with Python... Despite its positive aspects. (One can't cover everything, after all.)

Poor Tcl is under-rated, I should have noted. As with Lua, it's noted for its ability to function as an embedded scripting language in systems written in complied languages such as C, yet makes a fine language in its own right.

Tcl used to be something of a big deal in the CS world, but it seems to have started a slide into an undeserved obscurity. However, it's very much an alive language that's actively maintained and has a large community of devoted users and contributors. Shame that it's not a standard Linux install.

By Brainstorms (not verified) on 15 Jan 2017 #permalink

Sadly, I don't seem to remember it quite so well...

Decades ago, before PCs were a Thing and schools were just starting to buy a computer per class (or per form), I did a 'Computers in Education' elective in my DipEd, which was itself was really just an elective to back me up if grants were not renewed. Doing a computer course in education was probably a bit ambitious for someone like me because up to that point I'd never programmed before. As a budding biologist just about my only previous exposure to computing was a couple of months using Cricketgraph on the new Mackintosh Plus at work...

One of the assignments was to write an interactive program in an educational language for kids that was referred to by my lecturer as "Turtle", but on poking around just now it doesn't seem to be related to anything that has turtle-related themes in the programming nomenclature today. I suspect that it was still an implementation of Logo, but perhaps someone with a clue here could jog my memory?

Anyway, the day before the assignment was due I knocked up a program that would emulate the game "Mastermind", which is a number guessing game. A player would be given by the program a number to guess, and the program would give clues to indicate how the guessing was progressing. Four digits, twelves guesses, and with the same feedback that the board game version gives. I ran it twice only after I knocked out the first version, given the limited access that DipEd students had to computers in the late 80s, and handed in a floppy with my effort, and my fingers crossed.

My lecturer said that it was flawless, and written in the most efficient way that was possible in that language, and he was so impressed that a newb like me could pick up the concepts so quickly that he asked me to do a Masters or a PhD with him. Apparently I'd programmed at a level that his computing science students usually took six to twelve months of dedicated subjects to reach. I was more interested in immunology in those days so I declined the offer, which was probably the best decision because I've never had the fire for programming, even just in an educational game context, but in hindsight it might have helped to hone skills that I could have used elsewhere at the time.

The only thing that I really regret though is not keeping a copy of the program that I dashed out one midnight so long ago.

By Bernard J. (not verified) on 15 Jan 2017 #permalink

My early messing around was also on terminals, but they printed on plain white paper. The green striped paper was reserved for the line printers.

The thing about Python, as noted, is the number of libraries and their power.

I didn't mention perl, but perl is an interesting language. It is the polar opposite of phython in look and feel and neatness. Perl programmers are pretty sadistic when it comes to the one liner.

Yeah, but that's just willy waving and not really meant as serious programming. A bit like a life of "C obfuscation project".

You can decode DVD's DeCSS in one line of perl.

Makes no goddamned sense.

Perl is unfairly tarred and feathered as being an obscure language. I call bullshit.

Greg points out the reality: It is some Perl programmers who take a sadistic pleasure in abusing the language's syntax to achieve maximum obfuscation. (Yes, there are contests for just this.)

However, there is nothing inherent in the language that is any more confusing than any other mainstream language, and it is quite possible --and encouraged-- to write clear, understandable Perl code.

Consider it a consequence of being a very powerful language. Don't judge Perl by the extremes of its abuse, and the notoriety that follows.

And "the thing about Perl is the number of libraries and their power". Like Python.

By Brainstorms (not verified) on 16 Jan 2017 #permalink

Brainstorms: Right, perl can be abused or used correctly.

It was once great because of its libraries. I wonder if they've been kept up, or surpassed by Python, or whatever.

Here's the thing, though: Python enforces non-obscurity. Perl allows it. That is part of the problem with perl, I imagine.

Also, I don't like the Evangelical Christian link to perl.

Perl "allows it" due to the extensive amount of abbreviating it allows. Which makes it very easy, even perhaps tempting, to abuse it. Fair enough.

I've read about the Pearl connection, but I always think of Perl as "Larry Wall's Practical Extraction and Reporting Language", never the EC thing.

The guy was literally a rocket scientist at JPL. (I think he was in the Propulsion Section.) That should entitle him to some slack, shouldn't it? :^)

By Brainstorms (not verified) on 16 Jan 2017 #permalink

Sure, it's just a bit odd. Born again shell. Back in those days it was probably less annoying, when evangelicals were just funny sounding christians. Later, they became a threat to civilization, so the whole think can leave a bad taste!

"Here’s the thing, though: Python enforces non-obscurity."

Apart from the "bit that contains nothing means something" about Python you mean.

You can write code crappily in all languages.

Hell, I've written OO code in Fortran77. It didn't make it easy, but I could do it. You have to, in order to write an executable OO compiler.

Perl6 is going to be better, because part of what makes Perl5 easy to make unreadable is that it has expanded VASTLY beyond Programmed Regular Expression. For example the "bless" method of OO in perl.

Mind you, take a look sometime at PyGTK. There are things down as method calls, and things down as data methods. And quite why they do one in one place and the other elsewhere is genuinely opaque. The "best" way to write PyGTK programs is to open up the library source and read the goddamned code. Genuinely. Had to do it myself to debug a GTK call. Had to open up the library C source to see what it was doing under the hood to work out what the hell the ornamentation I was allowed to do on top of the hood was going to be.

And all of them have their own obfuscation competitions. Perl just tends to be harder to read (and doesn't produce a picture of Teri Hatcher when printed out on 132 colum fanfold...)

No, it's Bourne Again Shell. It's a play off the earlier Bourne Shell (the default for UNIX v7, named for Stephen Bourne).

I take that as a way to 'bash' the evangelicals, not a reason to look down on 'bash'.

And 'bash' is another good scripting language -- for us Linux types. (Yes, it's Turing Complete.)

By Brainstorms (not verified) on 16 Jan 2017 #permalink

Yes, but that is clearly a play on words .... the again part gives it a way.

I may or may not want to bash the evangelicals, but I do want to sh them.

I agree... They all come across as being programmed, ya know?

By Brainstorms (not verified) on 16 Jan 2017 #permalink

My first exposure to a computer was in high school Math Club, around 1964. One of the upper classmen brought in an analog "computer". Didn't do much. Might have been the GE kit. A few years later I was in college submitting Fortran card decks to be run overnight by admins...admins who I am sure learned to loath my ability to seemingly effortlessly create error ridden monstrosities that would do horrible things to their mainframe and their night schedules, while consuming ungodly amounts of computer paper to boot. It was a great improvement for all concerned years later that I was able to buy my own TRS-80 and learn to program in BASIC on my own. Over the years I've learned other languages, like a later version of Fortran, Pascal, Cobol, JCL..... all with varying degrees of ineptitude. Python, you say. That sounds like fun!

Try Lua. You can install it on Linux from the repos, or in Windows using Lua for Windows. It's small, fairly simple, consistent, and has only one form of structured data: tables. But the things you can do with tables are really cool... (Basically everything, including OOP with inheritance.)

By Brainstorms (not verified) on 16 Jan 2017 #permalink

Brainstorms ... "One of the assignments was to write an interactive program in an educational language for kids that was referred to by my lecturer as “Turtle”, but on poking around just now it doesn’t seem to be related to anything that has turtle-related themes in the programming nomenclature today. I suspect that it was still an implementation of Logo, but perhaps someone with a clue here could jog my memory?"

Yes, Logo was Seymour Papert's language (built on Lisp), Turtle Graphics meant as a learning tool for kids to learn programming.

Lua's nice. One of the reasons for its popularity as an embedded interpreter (as well as for Tcl, back in the day) is simply licensing: non-GPL so you don't need to release source for your product or the interpreter if you customize it.

Uh, GPL doesn't require that. Only if you change the GPL'd intrepreter *and give that change to someone else* do you have to give *only those changes* out and even then *only to that person*.

Please understand the license, not the license as you read on The Register in a piece by Orlowski. He's about as reliable as Willard.

PS do you think that you have to modify the interpreter to make Tcl usable?!?!?!

The cool thing about Tcl and Lua is that you can extend the interpreter using Tcl/Lua source code -- you don't have to modify the source code of the interpreter itself.

You can even make these mods at run-time: Having Tcl write Tcl code that's then executed as part of a Tcl program, for example.

And both of these interpreters interface nicely with C, allowing you to extend the interpreter that way, too -- without invoking "copyleft" licensing clauses.

By Brainstorms (not verified) on 18 Jan 2017 #permalink

Dhogaza, the syntax and output of Turtle Graphics as I can find it today is different to those of the 'Turtle' program I used in the late 80s.

Of course it may just be a version thing - and I haven't delved into TG to see if I could replicate the Mastermind game I scribbled back then.

By Bernard J. (not verified) on 18 Jan 2017 #permalink

"Only if you change the GPL’d intrepreter"

"customize" the interpreter implies change to the interpreter, in my vocabulary.

"and give that change to someone else"

Yes, the context of my comment was for folks customizing the interpreter and embedding it in a product which is distributed, which is my world.

Bernard J (sorry to have attributed your earlier comment to Brainstorms):

"Dhogaza, the syntax and output of Turtle Graphics as I can find it today is different to those of the ‘Turtle’ program I used in the late 80s."

Yes, I'm guessing it's due to ongoing research. Logo (and Turtle Graphics) were an outcome of Papert's research into education and early childhood learning, combined with his research interests in AI. I've not followed what's gone on in that world in recent decades (he just died a few months ago, didn't realize that).

Brainstorms:

"The cool thing about Tcl and Lua is that you can extend the interpreter using Tcl/Lua source code — you don’t have to modify the source code of the interpreter itself.

You can even make these mods at run-time: Having Tcl write Tcl code that’s then executed as part of a Tcl program, for example.

And both of these interpreters interface nicely with C, allowing you to extend the interpreter that way, too — without invoking “copyleft” licensing clauses."

Yep to all of the above. I have extensive experience with Tcl in this context dating from the late 1990s, lasting about a decade, and more recently with embedding lua in a product.

"Yes, the context of my comment"

Which was never mentioned....?

PS tell me how well changing the source code for MS C# works for you, bud. Hell, send me the github where you store your MS C# pimped out interpreter!

Or admit that you're talking bullshit since your "scenario" either never happens or is 100% as bad for every other language outside of BSD *as long as nobody patents any of that crap*.

"You can even make these mods at run-time: Having Tcl write Tcl code that’s then executed as part of a Tcl program, for example."

And you could do that without having to alter the TCL interpreter... which makes the license irrelevant, GPL or not.

"And both of these interpreters interface nicely with C, allowing you to extend the interpreter that way, too — without invoking “copyleft” licensing clauses."

GNU C is under the "copyleft" clause too, moron.

Woah! I think that means you don't understand what the fuck you're talking about, dude!

And you could do that without having to alter the TCL interpreter… which makes the license irrelevant, GPL or not.

That's what I communicated in #27. Read before you guffaw.

By Brainstorms (not verified) on 19 Jan 2017 #permalink

GNU C is under the “copyleft” clause too, moron.

Woah! I think that means you don’t understand what the fuck you’re talking about, dude!

Jezzus H... You moron. No one here is talking about modifying the GCC compliers. READ, then work on COMPREHENSION.

You're showing that you don’t understand what the fuck you’re talking about, dude!

By Brainstorms (not verified) on 19 Jan 2017 #permalink

"That’s what I communicated in #27. Read before you guffaw."

That's not what dhogza said that I was repsonding to.

Read before you ask me to read.

"Jezzus H… You moron. No one here is talking about modifying the GCC compliers."
Read what doghza is saying before you scream, you idiot.

The gcc compilers are interpreters. Who knew?

We were talking about Tcl and Lua interpreters. "You idiot."

Grow up, Wow. Save your schtick for RickA and MikeN.

By Brainstorms (not verified) on 19 Jan 2017 #permalink

The gcc is GPL licensed? Who knew! You?

We're talking about C and GPL "You Moron"

Grow up,Brainstorms, save your ignorance for when you're NOT opining outside your area of knowledge.

TIA, computer programmers.

"And then there’s my favorite: Lua,"

Lua, which has been the subject of work in the latex arena, leading to the release of a stable version of LuaTeX . A stable version was released at the 2016 ConTeXt meeting.

http://www.luatex.org/

How come those complaining that "GIMP won't cut it" because of its name don't snigger likewise at LaTeX?