
Download pyganim.py and example programs. (Works on both Python 2 and 3.)
Details at http://inventwithpython.com/pyganim/
Pyganim is a module that you can import into your Pygame games to handle sprite animation. Creating an animation is simply a matter of supplying a list of image filenames (or pygame.Surface objects) for each frame of animation, along with the duration that each frame lasts. Then call the play() method to start the animation and then call blit() each time you draw the window. The blit() function automatically draws the correct frame based on the amount of real time that has passed since the play() method was called. There are many, many other methods to provide finer levels of control as well.
There is more information available on the Tutorial and Reference page.
Posted by Al Sweigart at 6:20 pm on December 9th, 2011.
Categories: Blog.
I’ve written a book on Python programming for kids and complete beginners. I’ve also done some classroom and one-on-one teaching with adults and teens. I have them use IDLE, the IDE that comes with Python, because it is simple and easier than having them set up some other IDE (like PyDev on Eclipse).
But over time I’ve noticed a lot of problems with IDLE that I wish someone would fix. (I’d like to do it myself but I don’t have the time currently.) I’m very, very grateful that Python comes batteries included vis-à-vis an IDE, but there are a lot of warts and user interface issues with IDLE. (These are different from the bugs listed on the python.org bug tracker.)
First off, my philosophy is that no serious Python programmer uses IDLE as their dev environment. With that assumption, we can get rid of any need to cater to them and make IDLE aimed at people who are not only new to Python but new to programming in general. IDLE should be an education tool more than a development tool. This means that the IDLE default options should all be set for the basic user, not the power user.
With that in mind, these are things I’d like to see changed about the design of IDLE (roughly in order of importance and prior
More… »
Posted by Al Sweigart at 11:08 am on November 29th, 2011.
Categories: Blog.
I wrote some programs to go through 6 GB of OpenStreetMap data from http://metro.teczno.com so that I could extract a list of street names for an upcoming game project. The game will use procedural generation to create cities, so I need to have a dataset of street names but couldn’t easily find one. So I’ve created this one and wanted to share it.
I did a lot of tweaking to remove duplicates. Each street name is on its own line, and you can just add “Rd”, “St”, “Ln”, “Ave”, “Blvd”, “Pkwy” or any other suffix to the end of it. The zip file has a file of street names from each city, and then an allstreets.txt that has all of them combined into one file (with duplicates removed). Streets with numbers have been removed (there is no “7th” but there might be “Seventh”).
The street data comes from Boston, Chicago, Leeds, London, Manchester, St. Paul, New York, Seattle, the San Francisco Bay Area, Sydney, and DC, so you can expect that they mostly have Anglo names.
Then after looking at the data for a while, I realized that these could also be used for Anglo last names. I’ve removed any words that appear in a dictionary file I have, so some common last names like “Smith” or “Hunting” won’t show up. I would consider this list of moderate quality. Here’s the list:
I can’t vouch for the quality of the lists, but from a cursory inspection they seem quite serviceable. Enjoy!
Posted by Al Sweigart at 10:00 am on September 28th, 2011.
Categories: Blog.
I’ve decided to make the incomplete rough drafts of my next two Python books available.
Become a Codebreaker with Python
Making Graphical Games with Pygame
The emphasis is on “rough” and “incomplete”, but I thought it would be better to give a preview of the direction I was going. These books are also available under a Creative Commons BY-NC-SA license like the first “Invent with Python” book.
The Code Breaker book is aimed at complete beginners who have never programmed before, and as such has a lot of the same content as Invent with Python. It covers various encryption programs, and also how to write programs that can break encryption. (It’s an intro to programming and cryptography at the same time.)
The Pygame book is aimed at people who have read the first book or have a moderate amount of Python experience, and want to learn how to use the Pygame library to make graphical games. So far, the book really only has the source code for the games that will be in the book (these are the same programs that have been featured on this blog before.)
Hope you enjoy them. Feel free to send any ideas on content or presentation to me (don’t bother with typos and such, these are incomplete drafts and those are probably known issues.) al@inventwithpython.com
Posted by Al Sweigart at 5:33 pm on September 7th, 2011.
Categories: Blog.
A Clever Programming Trick…
If you need to swap the values of two variables, this usually requires a third temporary variable (that is, if you’re not using a language like Python that supports the a, b = b, a syntax.) It looks something like this:
temp = a;
a = b;
b = temp;
But if these are integer variables, there’s a nifty trick to save yourself a little bit of memory. You can use arithmetic instead of a temporary variable:
a = a + b;
b = a - b;
a = a - b;
If the integers on your platform are 32-bits, your new swap will save four bytes of memory.
NOBODY CARES ABOUT FOUR BYTES OF MEMORY. More… »
Posted by Al Sweigart at 10:00 am on August 19th, 2011.
Categories: Blog.
UPDATE: Thanks for everyone who emailed their help. I’ll leave this post up here for now, but the code seems fairly solid now.
I’m currently looking for help editing the source code for the games that will go into my next book. This book will also be released under a Creative Commons license and be freely available.
Download the game source. (Requires Python & Pygame)
UPDATE: I’ve fixed a problem where the midi files for the Tetris game were left out of this zip. Redownload the zip file to get them.
More… »
Posted by Al Sweigart at 10:10 am on July 8th, 2011.
Categories: Blog.