This post goes into the details of how you can add a “save game” feature to your games. Python’s built-in shelve module makes this very easy to do, but there are some pitfalls and tips that you might want to learn from this post before trying to code it up yourself.
If you want to skip ahead and see the Flippy version with the “save game” feature added, you can download the source code and image files used by the game. You need Pygame installed to run Flippy (but not Reversi). More… »
To figure out bugs in your code, you might put in print statements/print() calls to display the value of variables.
Don’t do this. Use the Python logging module.
The logging is better than printing because:
It’s easy to put a timestamp in each message, which is very handy.
You can have different levels of urgency for messages, and filter out less urgent messages.
When you want to later find/remove log messages, you won’t get them confused for real print() calls.
If you just print to a log file, it’s easy to leave the log function calls in and just ignore them when you don’t need them. (You don’t have to constantly pull out print() calls.)
Using print is for coders with too much time on their hands. Use logging instead. Also, learn to use the Python debugger to debug bugs and Pylint to prevent bugs and make your code readable.
To print log messages to the screen, copy and paste this code:
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('This is a log message.')
To write log messages to a file, you can copy and paste this code (the only difference is in bold):
import logging
logging.basicConfig(filename='log_filename.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('This is a log message.')
Later runs of the program will append to the end of the log file, rather than overwrite the file.
To log messages to a file AND printed to the screen, copy and paste the following: More… »
I made a twitter bot that checks every hour for someone who has asked the question, “Why do homeless people have dogs?” and automatically replies, “Because a dog will love you even though you are homeless.” It’s running right now at @YHobosHaveDogs.
Figuring out how to code this took a couple evenings and a little hair pulling, so I decided to document the process in this blog article to make it easier for the next programmer. This will be making a Twitter bot in Python using the python-twitter module (which runs on Python 2, not Python 3), and then running the bot from my Dreamhost server (but most likely any web host will work just fine. Or if you have a computer that is always online, you can run the bot from that). First we will run the bot from our machine to test it out, and then load it onto the Dreamhost web host. (I’m running a Windows box, but the steps should work on any OS.)
Download the python-twitter module (I tried some of the other modules but didn’t like them as much.)
Unzip this file and from the command line in the unzipped directory, run “python setup.py install” to install the twitter module.
Long before World of Warcraft, people played text-based MMORPGs called MUDs (Multi-User Dungeon). These were basically multiplayer text adventure games where people could wander through a virtual world fighting monsters and exploring. They had several RPG elements to them.
CircleMUD was a popular piece of server software for running a MUD, and it came with a sizeable virtual world (which the admin could modify/append to customize their fantasy world.) It would be pretty handy to use parts of this data if you were creating your own virtual world for a text adventure game, but the format of CircleMUD’s data files is kind of obtuse and not amenable to manipulation.
So I wrote a few scripts to convert these files into a single XML file which is 4MB when unzipped. You can parse this file and modify it to suit your needs. It contains 1979 rooms across 30 different areas (called zones in the file), with 46 shops and 569 different “mobs” (mobile objects, which are the monsters and NPCs). There are 678 different types of objects, including 116 weapons and 154 types of armor.
The scripts and original CircleMUD data (along with descriptions of the data formats) are included in the zip:
Math and programming have a somewhat misunderstood relationship. Many people think that you have to be good at math or made good grades in math class before you can even begin to learn programming. But how much math does a person need to know in order to program?
Not that much actually. This article will go into detail about the kinds of math you should know for programming. You probably know it already.
For general programming, you should know the following: More… »
I frequently see a problem when people (especially techies) try to teach programming to someone (especially non-techies). Many programming tutorials begin with basic programming principles: variables, loops, data types. This is both an obvious way to teach programming and almost certainly a wrong way to teach programming. It’s wrong because nobody wants to learn how to program.
If you are teaching a class of adults who are paying with their own money for an education, then this is an appropriate and direct way to teach programming. It’s their money. They expect that they’ll have to focus and slug through concepts to come out the other end with programming knowledge. The start-with-variables-loops-data-types approach is fine for this. But most likely they still don’t want to learn how to program.
But for the casually interested or schoolchildren with several activities competing for their attention, programming concepts like variables and loops and data types aren’t interesting in themselves. They don’t want to learn how to program just for the sake of programming. They don’t want to learn about algorithm complexity or implicit casting. They want to make Super Mario or Twitter or Angry Birds. This idea is best summed up in one of Ryan North’s Dinosaur comics (click to enlarge):
Here are my five pieces of advice to people who want to teach programming or create programming tutorials: More… »